So I have seen this question:
Spring dependency injection to other instance
and was wondering if my method will work out.
1) Declare beans in my Spring application context
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="${jdbc.initialSize}" />
<property name="validationQuery" value="${jdbc.validationQuery}" />
<property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
</bean>
<bean id="apiData" class="com.mydomain.api.data.ApiData">
<property name="dataSource" ref="dataSource" />
<property name="apiLogger" ref="apiLogger" />
</bean>
<bean id="apiLogging" class="com.mydomain.api.data.ApiLogger">
<property name="dataSource" ref="dataSource" />
</bean>
2) Override my servlet's init method as shown:
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
this.apiData = (ApiData)ac.getBean("apiData");
this.apiLogger = (ApiLogger)ac.getBean("apiLogger");
}
Will this work or is Spring not yet ready to deliver beans to my servlet at this point in the web applications deployment? Do I have to do something more traditional like putting the beans in web.xml
?
Spring is independent of Servlet startup. Right after spring reads the bean xml it will be ready to deliver the beans. So right after below statement, beans are already available
Also as pointed by @LuiggiMendoza each
ApplicationContext
will create/maintain their own beans so its always good to createApplicationContext
once and reuse it from different servlets (as opposed to creating them inside theinit()
method of a Servlet)What you are trying to do will make every
Servlet
have its ownApplicationContext
instance. Maybe this is what you want, but I doubt it. AnApplicationContext
should be unique to an application.The appropriate way to do this is to setup your
ApplicationContext
in aServletContextListener
.Now all your servlets have access to the same
ApplicationContext
through theServletContext
attributes.The answers here so far only worked partly for me. Especially classes with @Configuration annotation were ignored and I did not want to use an xml configuration file. Here is what I have done to get injection working working soley with an Spring (4.3.1) annotation based setup:
Bootstrap an AnnotationConfigWebApplicationContext in web.xml under web-app. As parameter you need the contextClass and the contextConfigLocation (your annotated config class):
Then overwrite the init method in the servlet. I use an abstract class which extends HttpServlet, so I don't have to repeat it in every servlet:
and finally I have my main configuration in the AppConfig class mentioned in web.xml:
The dependend classes are annotated:
and injected via autowiring in my servlet:
I wanted to leverage on the solution provided by Sotirios Delimanolis but adding transparent autowiring to the mix. The idea is to turn plain servlets into autowire-aware objects.
So I created a parent abstract servlet class that retrieves the Spring context, gets and autowiring-capable factory and uses that factory to autowire the servlet instances (the subclasess, actually). I also store the factory as an instance variable in case the subclasses need it.
So the parent abstract servlet looks like this:
And a sevlet subclass looks like this:
Notice the only thing EchoServlet needs to do is to declare a bean just in common Spring practice. The magic is done in the init() method of the superclass.
I haven't tested it thoroughly. But it worked with a simple bean MyService that also gets a property autowired from a Spring-managed properties file.
Enjoy!
Note:
It's best to load the application context with Spring's own context listener like this:
Then retrieve it like this:
Only spring-web library needs to be imported, not spring-mvc.