Problem with @Autowired field initializations
Hello!
I have acquired a project in a new company where I am first time using Spring Boot and IntelliJ IDEA. Because of some out-of-date code, there are restrictions here, so, I can use the version 2022.2.5. Work is ongoing on this project for several years, and we have currently dozens of customers.
In my code, there is an issue with autowired fields. Originally, the code I use was as follows:
/**
* The type OFLOW license parameter consumer.
*/
@Component
@Configurable(preConstruction = false, autowire = Autowire.BY_TYPE)
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class OflowLicenseParamConsumerImpl extends CPSLicenseParamBaseImpl {
/**
* The set manager object.
*/
@Autowired
AppSettingsManager setMgr;
/**
* The app context object.
*/
@Autowired
OflowAppContext appCtx;
@Override
public KeyStoreParam getKeyStoreParam() {
return new OflowConsumerKeyStoreParamImpl();
}
…
An NullPointerException
was thrown at the getKeyStoreParam()
call. The debugger showed that appCtx and setMgr are both null. To get rid of this issue, I modified the code as follows:
/**
* The type OFLOW license parameter consumer.
*/
@Component
@Configurable(preConstruction = false, autowire = Autowire.BY_TYPE)
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class OflowLicenseParamConsumerImpl extends CPSLicenseParamBaseImpl {
// @Autowired
// AppSettingsManager setMgr;
// @Autowired
// OflowAppContext appCtx;
@Autowired
public OflowLicenseParamConsumerImpl(final AppSettingsManager setMgr, final OflowAppContext appCtx) {
super(setMgr, appCtx);
}
@Override
public KeyStoreParam getKeyStoreParam() {
return new OflowConsumerKeyStoreParamImpl();
}
…
This time, I had to add a constructor without parameters, as the program is making calls to OflowLicenseParamConsumerImpl()
:/**
* The type OFLOW license parameter consumer.
*/
@Component
@Configurable(preConstruction = false, autowire = Autowire.BY_TYPE)
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class OflowLicenseParamConsumerImpl extends CPSLicenseParamBaseImpl {
// @Autowired
// AppSettingsManager setMgr;
// @Autowired
// OflowAppContext appCtx;
/**
* Standard constructor
*/
public OflowLicenseParamConsumerImpl() {
}
@Autowired
public OflowLicenseParamConsumerImpl(final AppSettingsManager setMgr, final OflowAppContext appCtx) {
super(setMgr, appCtx);
}
…
But the standard constructor had uninitialized autowired variables, which is not surprising, of course. As the parent class CPSLicenseParamBaseImpl
is read-only, I added another abstract class inbetween:
/**
* The type CPS license parameter.
*/
public abstract class CPSLicenseParamImpl extends CPSLicenseParamBaseImpl {
/**
* The application settings manager
*/
protected AppSettingsManager setMgr;
/**
* The Orlando Workflow context
*/
protected OflowAppContext appCtx;
/**
* Standard constructor only to be called after appCtx and setMgr are autowired.
*/
public CPSLicenseParamImpl() {
final String methodName = "CPSLicenseParamImpl";
assertNotNull(Strings.printNotNull(methodName, "appCtx"), appCtx);
assertNotNull(Strings.printNotNull(methodName, "setMgr"), setMgr);
}
/**
* Instantiates a new CPS license parameters.
*
* @param setMgr the set mgr
* @param appCtx the app ctx
*/
public CPSLicenseParamImpl(final AppSettingsManager setMgr, final OflowAppContext appCtx) {
this.appCtx = appCtx;
this.setMgr = setMgr;
}
}
and adopted the code as follows:/**
* The type OFLOW license parameter consumer.
*/
@Component
@Configurable(preConstruction = false, autowire = Autowire.BY_TYPE)
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class OflowLicenseParamConsumerImpl extends CPSLicenseParamImpl {
// @Autowired
// AppSettingsManager setMgr;
// @Autowired
// OflowAppContext appCtx;
/**
* Standard constructor
*/
public OflowLicenseParamConsumerImpl() {
super();
}
@Autowired
public OflowLicenseParamConsumerImpl(final AppSettingsManager setMgr, final OflowAppContext appCtx) {
super(setMgr, appCtx);
}
…
Additionally, as my predecessor advised me, I started the build.bat
CLS
ECHO ./build
SET JAVA_HOME=D:\tools\oracle\java
SET mvn=D:\tools\apache-maven-3.2.5\bin\mvn.bat
SET model=D:\source\workflow\orlando-domain-model
SET oflow=D:\source\workflow\vertragsverwaltung
SET portal=D:\source\workflow\portal-commons
cmd /c D: & cd %oflow% & call %mvn% clean -DskipTests
cmd /c D: & cd %portal% & call %mvn% clean compile install -DskipTests
cmd /c D: & cd %model% & call %mvn% clean compile install -DskipTests
cmd /c D: & cd %oflow% & call %mvn% compile install -DskipTests
cd %oflow%
first and called “File” > “Invalidate Cashes…” to re-open IntelliJ. But still, I get over and over again the same error from the modified assertNotNull(Strings.printNotNull(methodName, "appCtx"), appCtx)
from above:
…
2024-09-30 08:53:41,322 [RMI TCP Connection(3)-127.0.0.1] ERROR [o.s.w.c.ContextLoader] : Context initialization failed
java.lang.AssertionError: CPSLicenseParamImpl(?): appCtx darf niemals null sein!
at org.junit.Assert.fail(Assert.java:91) ~[com.springsource.org.junit-4.7.0.jar:na]
at org.junit.Assert.assertTrue(Assert.java:43) ~[com.springsource.org.junit-4.7.0.jar:na]
at org.junit.Assert.assertNotNull(Assert.java:524) ~[com.springsource.org.junit-4.7.0.jar:na]
at at.cps.oflow.licensing.consumer.CPSLicenseParamImpl.<init>(CPSLicenseParamImpl.java from InputFileObject:29) ~[CPSLicenseParamImpl.class:na]
at at.cps.oflow.licensing.consumer.OflowLicenseParamConsumerImpl.<init>(OflowLicenseParamConsumerImpl.java:42) ~[OflowLicenseParamConsumerImpl.class:na]
at at.cps.oflow.OflowAppContext.getLicenseParamImpl(OflowAppContext.java:346) ~[OflowAppContext.class:na]
at at.cps.portal.PortalAppContext.verifyLicense(PortalAppContext.java:98) ~[portal-commons-1.2.0.jar:na]
…
CPSLicenseParamImpl.java from InputFileObject: line 29
is the line where the assertNotNull
resides.
Has anybody ever encountered this problem? Do you have some tips for me?
Here is the content of the log file:
D:\tools\apache-tomcat-6.0.53\bin\catalina.bat run
[2024-09-30 10:37:14,297] Artifact oflow:war exploded: Waiting for server connection to start artifact deployment...
Using CATALINA_BASE: "C:\Users\<username>\AppData\Local\JetBrains\IntelliJIdea2022.2\tomcat\70124700-6990-4c5e-a690-605e8b503e65"
Using CATALINA_HOME: "D:\tools\apache-tomcat-6.0.53"
Using CATALINA_TMPDIR: "D:\tools\apache-tomcat-6.0.53\temp"
Using JRE_HOME: "D:\tools\Oracle\java"
Using CLASSPATH: "D:\tools\apache-tomcat-6.0.53\bin\bootstrap.jar"
Connected to the target VM, address: '127.0.0.1:51633', transport: 'socket'
30.09.2024 10:37:14 org.apache.catalina.core.AprLifecycleListener init
INFO: Loaded APR based Apache Tomcat Native library 1.2.12 using APR version 1.5.2.
30.09.2024 10:37:14 org.apache.catalina.core.AprLifecycleListener init
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
30.09.2024 10:37:15 org.apache.catalina.core.AprLifecycleListener initializeSSL
INFO: OpenSSL successfully initialized with version OpenSSL 1.0.2k 26 Jan 2017
30.09.2024 10:37:15 org.apache.coyote.http11.Http11AprProtocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
30.09.2024 10:37:15 org.apache.coyote.ajp.AjpAprProtocol init
INFO: Initializing Coyote AJP/1.3 on ajp-8009
30.09.2024 10:37:15 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 543 ms
30.09.2024 10:37:15 org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
30.09.2024 10:37:15 org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.53
30.09.2024 10:37:15 org.apache.coyote.http11.Http11AprProtocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
30.09.2024 10:37:15 org.apache.coyote.ajp.AjpAprProtocol start
INFO: Starting Coyote AJP/1.3 on ajp-8009
30.09.2024 10:37:15 org.apache.catalina.startup.Catalina start
INFO: Server startup in 40 ms
Connected to server
[2024-09-30 10:37:15,453] Artifact oflow:war exploded: Artifact is being deployed, please wait...
30.09.2024 10:37:15 org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(D:\source\workflow\vertragsverwaltung\target\oflow-1.7.18\WEB-INF\lib\javax.servlet-api-3.1.0.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
2024-09-30 10:37:22,677 [RMI TCP Connection(3)-127.0.0.1] INFO [org.zkoss] : Loading system default
2024-09-30 10:37:25,027 [RMI TCP Connection(3)-127.0.0.1] INFO [a.c.o.u.ApplicationUpdateManager] : ApplicationVersion: 1.7.18(10718)10718
30.09.2024 10:37:25 org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory manager
2024-09-30 10:37:43,626 [RMI TCP Connection(3)-127.0.0.1] ERROR [o.s.w.c.ContextLoader] : Context initialization failed
java.lang.AssertionError: CPSLicenseParamImpl(?): appCtx darf niemals null sein!
at org.junit.Assert.fail(Assert.java:91) ~[com.springsource.org.junit-4.7.0.jar:na]
at org.junit.Assert.assertTrue(Assert.java:43) ~[com.springsource.org.junit-4.7.0.jar:na]
at org.junit.Assert.assertNotNull(Assert.java:524) ~[com.springsource.org.junit-4.7.0.jar:na]
at at.cps.oflow.licensing.consumer.CPSLicenseParamImpl.<init>(CPSLicenseParamImpl.java from InputFileObject:31) ~[CPSLicenseParamImpl.class:na]
at at.cps.oflow.licensing.consumer.OflowLicenseParamConsumerImpl.<init>(OflowLicenseParamConsumerImpl.java from InputFileObject:43) ~[OflowLicenseParamConsumerImpl.class:na]
at at.cps.oflow.OflowAppContext.getLicenseParamImpl(OflowAppContext.java:346) ~[OflowAppContext.class:na]
at at.cps.portal.PortalAppContext.verifyLicense(PortalAppContext.java:98) ~[portal-commons-1.2.0.jar:na]
at at.cps.oflow.OflowAppContext.onApplicationEvent(OflowAppContext.java:238) ~[OflowAppContext.class:na]
at at.cps.oflow.OflowAppContext.onApplicationEvent(OflowAppContext.java:56) ~[OflowAppContext.class:na]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:96) ~[spring-context-3.2.6.RELEASE.jar:3.2.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:334) ~[spring-context-3.2.6.RELEASE.jar:3.2.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:948) ~[spring-context-3.2.6.RELEASE.jar:3.2.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) ~[spring-context-3.2.6.RELEASE.jar:3.2.6.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) ~[spring-web-3.2.6.RELEASE.jar:3.2.6.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) ~[spring-web-3.2.6.RELEASE.jar:3.2.6.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) [spring-web-3.2.6.RELEASE.jar:3.2.6.RELEASE]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4276) [catalina.jar:6.0.53]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4779) [catalina.jar:6.0.53]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:803) [catalina.jar:6.0.53]
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:780) [catalina.jar:6.0.53]
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:583) [catalina.jar:6.0.53]
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1432) [catalina.jar:6.0.53]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_45]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_45]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_45]
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:297) [tomcat-coyote.jar:6.0.53]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:836) [na:1.6.0_45]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:762) [na:1.6.0_45]
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:631) [catalina.jar:6.0.53]
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:568) [catalina.jar:6.0.53]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_45]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_45]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_45]
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:295) [tomcat-coyote.jar:6.0.53]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:836) [na:1.6.0_45]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:762) [na:1.6.0_45]
at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:447) [na:1.6.0_45]
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1454) [na:1.6.0_45]
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:74) [na:1.6.0_45]
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1295) [na:1.6.0_45]
at java.security.AccessController.doPrivileged(Native Method) [na:1.6.0_45]
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1394) [na:1.6.0_45]
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:818) [na:1.6.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_45]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_45]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_45]
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:303) [na:1.6.0_45]
at sun.rmi.transport.Transport$1.run(Transport.java:159) [na:1.6.0_45]
at java.security.AccessController.doPrivileged(Native Method) [na:1.6.0_45]
at sun.rmi.transport.Transport.serviceCall(Transport.java:155) [na:1.6.0_45]
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535) [na:1.6.0_45]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790) [na:1.6.0_45]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649) [na:1.6.0_45]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) [na:1.6.0_45]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) [na:1.6.0_45]
at java.lang.Thread.run(Thread.java:662) [na:1.6.0_45]
30.09.2024 10:37:51 org.apache.catalina.core.StandardContext start
SCHWERWIEGEND: Error listenerStart
30.09.2024 10:37:53 org.apache.catalina.core.StandardContext start
SCHWERWIEGEND: Context [/oflow] startup failed due to previous errors
30.09.2024 10:37:53 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SCHWERWIEGEND: The web application [/oflow] registered the JDBC driver [org.postgresql.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
30.09.2024 10:37:53 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SCHWERWIEGEND: The web application [/oflow] registered the JDBC driver [com.pervasive.jdbc.v2.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
30.09.2024 10:37:53 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SCHWERWIEGEND: The web application [/oflow] registered the JDBC driver [com.pervasive.jdbc.v2.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
30.09.2024 10:37:53 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SCHWERWIEGEND: The web application [/oflow] registered the JDBC driver [pervasive.jdbc.PervasiveDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Please sign in to leave a comment.