问题1 springbootapplicationcontextnull的代码
我的代码
privatestaticapplicationcontextapplicationcontext publicstaticidentityapplicationserviceidentityapplicationservice ({ return (identityapplicationservice )应用程序上下文文本) 公共通信服务通知应用服务(return ) @ overridepublicsynchronizedvoidsetapplicationcontext (applicationcontext applicationcontext ) )。 throwsbeansexception { if (applicationserviceregistry.application context==null ) }
publicclassuserhibernatecontroller { privateidentityapplicationserviceidentityapplicationservice=applicationsereregistry pry nservicenotificationapplicationservice=applicationserviceregistry.notificationapplicationservice @ get mapping (/域名/域名) username ) ) publicuserdomainuser ) @pathvariable ) (tenantid ) ) striing @pathvariable(username ) ) String aUsername ser ) atenantid,ausentityapplicationservice this.notificationapplicationservice ().publishNotifications; //system.out.println (user.tostring ) ); 返回用户; }执行错误报告application上下文空
我首先查了网,觉得bean的声明方式是错误的。 @Component @Service已尝试禁用
将@ComponentScan({} )添加到启动类仍然无效。 顺便说一下,spring默认添加了ComponentScan,如果没有特别明确的需求,请不要盲目添加。 我添加后,班级有时可以扫描。 控制器又不能识别。 果然有问题。 果然学艺不全啊。 在查阅百思资料时,偶然发现@Component @Service等角色与beanxml的bean id=' domain registry ' class=' ' /的角色一致。 如果认为@Component @Service和beanxml的作用一致,而不是突然注入方式的问题,那么既然启动类以xml方式导入了类,就没有必要使用service或Component注释。 实现最有可能实现和使用ApplicationContextAware的网上样品是非静态实现,我列中是静态实现,这是唯一的,但不能实现静态吗? 网上也确实有springunit的安装类,从方式来看是静态的。 再次运行代码并查看日志时,此处列出了报告错误的位置,并且在未加载任务bean类时报告了错误
privatenotificationapplicationservicenotificationapplicationservicationserviceservice=applicationserviceregistry.notionapppplicationsestionaplionationsestionservestionsetions 未加载cationserviceidentityapplicationservice ({ return (identityapplicationservice ) applicationcontextext } )任务bean类时出错
在未加载任务bean类的情况下报告了错误。
看到问题了吧,
privatenotificationapplicationservicenotificationapplicationservicationserviceservice=applicationserviceregistry.notionapppplicationsestionaplionationsestionservestionsetions
问题是此语句在初始化时调用了静态方法。 此时的bean肯定没有加载。 ApplicationContextAware的子类还没有实现。
因此适用修正
代码
publicclassuserhibernatecontroller//privateidentityapplicationserviceidentityapplicationservice=applicationsereregisisisice privatenotificationapplicationservicenotificationapplicationservicationservicationserviceregistry.notificationappplicationationannapppppation 域/用户) tenantid )/username ) ) publicuserdomainuser ) @pathvariable ) (tenantid ) ) striing @pathvariable(username ) ) String aUsername ser ) atenantid,ausentityapplicationservice this.notificationapplicationservice ().publishNotifications; //system.out.println (user.tostring ) ); 返回用户; } protectedidentityapplicationserviceidentityapplicationservicationservistry.identityapplicationservice () retureturnappplicationserviceregistry.} protectednotificationapplicationservicenotificationapplicationserviceservice (returnapplicationseregistry.notificationapppplice
呃,对spring的结构还不熟悉呢。 慢慢踩坑吧。 但是,这次上升之后,spring注释方式和beanxml注入方式可以很好地结合起来了。 新老项目的迁移应该可以节省很多。
此外,还有两种在SpringBoot中检索应用程序上下文的方法
直接使用自动注入
@ componentpublicclassbook1{ @ autowiredprivateapplicationcontextapplicationcontext; 公共void show ((system.out.println ) applicationcontext.getclass ) ); 实现spring提供的接口ApplicationContextAware
初始化bean后,spring会确定它是否是ApplicationContextAware的子类,并在调用setApplicationContext ()方法时传递容器中的ApplicationContext
@ componentpublicclassbook3implementsapplicationcontextaware { privateapplicationcontextapplicationcontext; 公共void show ((system.out.println ) applicationcontext.getclass ) ); } @ overridepublicvoidsetapplicationcontext (applicationcontext ) throwsbeansexception ) this.application }