The Bean is populated by the instance, but its fields are zero when destroyed

I have a class like this:

public class Loader implements DisposableBean { Class1 c1; Class2 c2; Loader(Class c1, Class c2) { this.c1 = c1; this.c2 = c2; } @Override public void destroy() throws Exception { c1.do(); c2.do(); } } 

It is created as @ Bean in the @Configuration class.

 @Bean(autowire = Autowire.BY_NAME, name = "loader") public Loader getLoader() { return new Loader(getC1(), getC2()); } 

And it gets the instance through the context loader listener in my web.xml

 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-loader.xml</param-value> </context-param> 

With this application context having the following line:

 <context:component-scan base-package="name.of.package.loader.is.in" /> 

I put a breakpoint in the constructor after the outbreak of war in tomcat, and I can verify that both c1 and c2 are filling as expected. However, when I exit tomcat and it goes into the destroy method, both c1 and c2 are zero and thus NPE is called. Why do these values ​​no longer exist?


Ah, Darnith, had to dig a little. It cleared up in a section of code that I was not familiar with, and finally I traced it.

+6
source share

Source: https://habr.com/ru/post/914921/


All Articles