Bonjour,
J'aimerais pouvoir instancier automatiquement mes services avec Spring IoC dans une Servlet de base (càd sans Struts, Spring/MVC, etc...).
Voici mon web.xml :
Voici mon applicationContext.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="ProductServlet" class="com.catalog.servlet.ProductServlet"> <property name="productServices"> <ref bean="productServices"/> </property> </bean> <bean id="productServices" class="com.catalog.services.business.impl.ProductServicesImpl"> </bean> </beans>
ProductServlet est une HttpServlet, c'est cette servlet qui utilisera le service productServices.
Dans la Servlet j'ai le code suivant :
Le problème est que j'obtient un NullPointerException, donc mon productServices n'a pas été référencé automatiquement.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 public class ProductServlet extends HttpServlet { private IProductServices productServices; public void setProductServices(IProductServices productServices) { this.productServices = productServices; } protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Appel service productServices.getProductForId(id); } }
Il est bien instancié en tout cas :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 12:30:23,500 INFO [DefaultListableBeanFactory] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@10613aa: defining beans [productServices]; root of factory hierarchy 12:30:23,500 DEBUG [DefaultListableBeanFactory] Creating shared instance of singleton bean 'productServices' 12:30:23,500 DEBUG [DefaultListableBeanFactory] Creating instance of bean 'productServices' 12:30:23,547 DEBUG [DefaultListableBeanFactory] Eagerly caching bean 'productServices' to allow for resolving potential circular references 12:30:23,562 DEBUG [DefaultListableBeanFactory] Finished creating instance of bean 'productServices'
Une méthode que j'ai trouvé serait de faire :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); productServices = (IProductServices) wac.getBean("productServices");
Mais ca ne me plait pas trop, j'aurais aimé que le productServices soit référencé automatiquement lors de l'instanciation de la Servlet.
Est-ce possible ?
Merci
Partager