springboot thin jar, causing problems with injection

Viewed 34
@Slf4j
@RestController
@RequestMapping("/admin/app")
public class AppSessionController {
    private OnlineSessionService onlineSessionService;

    @Autowired
    public void setOnlineSessionService(OnlineSessionServiceImpl onlineSessionService) {
        log.info("AppSessionController{}",this);
        log.info("inject{}",onlineSessionService);
        this.onlineSessionService = onlineSessionService;
        log.info("after inject{}",this.onlineSessionService);
    }

    @PreAuthorize("hasAuthority('app:user:getOnlineUserList')")
    @PostMapping("/getOnlineUserList")
    Result<PageInfo<AuthData<?>>> getOnlineUserList(@RequestBody PageRequest pageRequest) {
        log.info("AppSessionController{}, {}",this, this.ss);
        log.info("this.onlineSessionService :"+this.onlineSessionService);
        log.info("onlineSessionService :"+onlineSessionService);
        PageInfo<AuthData<?>> pageInfo = onlineSessionService.getOnlineUserList(pageRequest.getPageNum(), pageRequest.getPageSize());
        PageInfoFixUtil.fixPageInfo(pageInfo);
        return Result.ok(pageInfo);
    }
}

The output:

INFO main (AppSessionController.java:32) - AppSessionControllercom.apex.app.admin.controller.AppSessionController@5a438c0a
INFO main (AppSessionController.java:33) - inject com.apex.app.common.service.impl.OnlineSessionServiceImpl@14cd8dfa
INFO main (AppSessionController.java:36) - after inject com.apex.app.common.service.impl.OnlineSessionServiceImpl@14cd8dfa
INFO http-nio-8100-exec-5 (AppSessionController.java:48) - AppSessionControllercom.apex.app.admin.controller.AppSessionController@5a438c0a, null
INFO http-nio-8100-exec-5 (AppSessionController.java:49) - this.onlineSessionService :null
INFO http-nio-8100-exec-5 (AppSessionController.java:50) - onlineSessionService :null

The code with the problem is as above, why the onlineSessionService is null after the jar is thinned, and other controllers in the same level directory do not have this problem.

If I change the onlineSessionService to static then the injection can be injected.

gradle thin jar method:

// Copy the dependencies to the lib directory
task copyJar(type: Copy) {
    // delete lib directory
    delete "$buildDir\\libs\\lib"
    from configurations.runtimeClasspath
    into "$buildDir\\libs\\lib"
    from configurations.compileClasspath
    into "$buildDir\\libs\\lib"
}

task copyConfigFile(type: Copy) {

    delete "$buildDir\\libs\\config"
    from('src/main/resources')
    into 'build/libs/config'
}
// copy configuration file
bootJar {

    //archiveBaseName = 'application'

    enabled = true
    archiveVersion = '1.0.0'
    // exclude all jars
    excludes = ["*.jar"]

    dependsOn copyJar

    dependsOn copyConfigFile

    manifest {
        attributes "Manifest-Version": 1.0,
                'Class-Path': configurations.runtimeClasspath.files.collect { "lib/$it.name" }.join(' ')
    }
}
1 Answers

You should inject OnlineSessionService in constructor or by @Autowired as field rather than inject in method:

public AppSessionController(OnlineSessionService onlineSessionService){
   this.onlineSessionService = onlineSessionService;
}

//Or like this
@Autowired
private OnlineSessionService onlineSessionService;

And I think the gradle config is wired, maybe use org.springframework.boot plugin to generate jar file is better than your tasks, please reference https://start.spring.io/ for more detail.

Related