@Api(tags = "APP用户会话管理")
@Slf4j
@RestController
@RequestMapping("/admin/app")
@Scope("singleton")
public class AppSessionController {
private OnlineSessionService onlineSessionService;
@Autowired
public void setOnlineSessionService(OnlineSessionServiceImpl onlineSessionService) {
log.info("AppSessionController{}",this);
log.info("注入{}",onlineSessionService);
this.onlineSessionService = onlineSessionService;
log.info("注入后{}",this.onlineSessionService);
}
/**
* @param pageRequest 当前页 0开始
* @return
*/
@ApiOperation("获取APP在线用户信息")
@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);
}
}
输出如下:
2022-03-09 09:40:13,052:INFO main (AppSessionController.java:32) - AppSessionControllercom.apex.app.admin.controller.AppSessionController@5a438c0a
2022-03-09 09:40:13,052:INFO main (AppSessionController.java:33) - 注入com.apex.app.common.service.impl.OnlineSessionServiceImpl@14cd8dfa
2022-03-09 09:40:13,052:INFO main (AppSessionController.java:36) - 注入后com.apex.app.common.service.impl.OnlineSessionServiceImpl@14cd8dfa
2022-03-09 09:40:31,447:INFO http-nio-8100-exec-5 (AppSessionController.java:48) - AppSessionControllercom.apex.app.admin.controller.AppSessionController@5a438c0a, null
2022-03-09 09:40:31,448:INFO http-nio-8100-exec-5 (AppSessionController.java:49) - this.onlineSessionService地址:null
2022-03-09 09:40:31,448:INFO http-nio-8100-exec-5 (AppSessionController.java:50) - onlineSessionService地址:null
出现问题的代码如上,为什么瘦身jar后onlineSessionService是null的,同级目录下其他controller没有出现这个问题。
如果我把onlineSessionService改成静态的然后注入是可以注入的。
想请问各位大佬为什么。
gradle瘦身方法:
// 将依赖包复制到lib目录
task copyJar(type: Copy) {
// 清除现有的lib目录
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'
}
// 配置bootJar进行打包
bootJar {
// jar包名称,默认是settings.gradle下的工程名 rootProject.name = 'alog-server'
//archiveBaseName = 'application'
// jar包版本号
enabled = true
archiveVersion = '1.0.0'
// 排除所有的jar
excludes = ["*.jar"]
// 排除配置文件,服务器上配置文件放在外部
// lib目录的清除和复制任务
dependsOn copyJar
// 配置目录的清除和复制任务
dependsOn copyConfigFile
// 指定依赖包的路径
manifest {
attributes "Manifest-Version": 1.0,
'Class-Path': configurations.runtimeClasspath.files.collect { "lib/$it.name" }.join(' ')
}
}