In Web applications using spring containers, dependencies between business objects can be configured using the context. xml file, and spring containers are responsible for dependency objects.
. To use spring containers to manage business objects in filters or Servlets, you usually need to use webapplicationcontextutils. getrequiredwebapplicationcontext (getservletcontext () to obtain the webapplicationcontext, and then call webapplicationcontext. getbean ("beanname") is used to obtain the object reference. In fact, dependency lookup is used to obtain the object, and the bean name of the Application object is hardcoded in the filter or servlet code.
To perceive the bean in spring in the filter or servlet, follow these steps:
1. Define the filter or Servlet as bean in the context. xml file and put it together with the bean definition to be applied;
2. Implement a filter proxy or servlet proxy. The proxy uses webapplicationcontext to obtain information in the context. filter or servlet object defined in XML, and delegate the task to context. filter or servlet defined in XML
3. Use contextloaderlistener in Web. XML to initialize spring
And use initialization parameters to define the context in the definition of the filter proxy or servlet proxy. the bean name of the filter or servlet in XML (or directly obtain the name of the filter or servlet using the proxy name ).
4. Define the mapping of the filter proxy or servlet proxy in Web. xml.
In this way, the dependency between the filter or Servlet and the business object is used in spring.
And the name of the object to be referenced is not hardcoded In the servlet.
The specific example is as follows:
1. Define filter in applicationcontext. xml
<bean id="loginFilter" class="com.ces.system.filter.LoginFilter"> <property name="userDao"><ref bean="usersDaoImpl" /></property> </bean>
2. Implement filter proxy
In fact, the filter proxy does not need to be implemented by ourselves. Spring provides two ready-made filter proxies.
Org. springframework. Security. util. filtertobeanproxy
Org. springframework. Web. Filter. delegatingfilterproxy
The two are slightly different in the web. xml configuration. Let's take a look at how to configure them in Web. xml.
3. Configure web. xml
3.1 initialize spring Context
<! -- Specify the spring configuration file --> <context-param> <param-Name> contextconfiglocation </param-Name> <param-value> classpath *: spring /*. XML </param-value> </context-param> <! -- Specify to start the spring container in listener mode --> <listener-class> org. springframework. Web. Context. contextloaderlistener </listener-class> </listener>
3.2 filter configuration:
Filtertobeanproxy:
<filter> <filter-name> loginFilter </filter-name> <filter-class> org.springframework.security.util.FilterToBeanProxy </filter-class> <init-param> <param-name>targetBean</param-name> <param-value>loginFilter</param-value> </init-param> </filter>
Note: The context parameter needs to be provided for filtertobeanproxy. Here we configure the targetbean attribute, which tells spring to find the bean name in the context. Therefore, after the request is intercepted by a filter, filtertobeanproxy will be in applicationcontext. the bean whose ID is loginfilter will be searched in XML. you can also configure the targetclass attribute to find the bean of this type.
Delegatingfilterproxy:
<filter> <filter-name>loginFilter</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> </filter>
Note: No parameters need to be configured when delegatingfilterproxy is used. Spring searches for beans based on the filter-name, so spring searches for the bean with the ID loginfilter.
4. Configure filter mapping
<filter-mapping><filter-name>loginFilter</filter-name><url-pattern>*.action</url-pattern></filter-mapping>
OK! Filter configuration is complete. We recommend that you use delegatingfilterproxy, which should be easier to configure.
Servlet configuration is very similar to filter configuration.
1. Define servlet in applicationcontext. xml
<bean id="springServlet" class="com.netqin.servlet.SpringServlet"> <property name="name"> <value>SpringServlet</value> </property> </bean>
COM: COM. netqin. servlet. springservlet inherits from javax. servlet. http. httpservlet
2. Implement servlet proxy
Unlike filter, spring does not provide Proxy implementation for servlet and needs to be created by ourselves. However, it is easy to create a servlet proxy. A specific implementation is as follows:
import java.io.IOException;import javax.servlet.GenericServlet;import javax.servlet.Servlet;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;public class ServletToBeanProxy extends GenericServlet { private String targetBean; private Servlet proxy; public void init() throws ServletException { this.targetBean = getInitParameter("targetBean"); getServletBean(); proxy.init(getServletConfig()); } public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { proxy.service(req, res); } private void getServletBean() { WebApplicationContext wac = WebApplicationContextUtils .getRequiredWebApplicationContext(getServletContext()); this.proxy = (Servlet) wac.getBean(targetBean); }}
Note: This class uses the targetbean attribute to find the corresponding servlet in spring, which is similar to the filtertobeanproxy method. Therefore, I named it servlettobeanproxy.
Of course, we can also use the formula defined in delegatingfilterproxy. just modify the code tag to this.tar getbean = This. getservletname (); and we will name it delegatingservletproxy.
3. Configure web. xml
3.1 initialize spring Context
It is consistent with the description in filter.
3.2
Servlettobeanproxy:
<servlet> <servlet-name>springServlet</servlet-name> <servlet-class> com.netqin.servlet.proxy.ServletToBeanProxy </servlet-class> <init-param> <param-name>targetBean</param-name> <param-value>springServlet</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
Delegatingservletproxy:
<servlet> <servlet-name>springServlet</servlet-name> <servlet-class> com.netqin.servlet.proxy.DelegatingServletProxy </servlet-class> <load-on-startup>1</load-on-startup> </servlet>
4. Configure Servlet Mapping
<filter-mapping> <filter-name>springServlet</filter-name> <url-pattern>/servlet/*</url-pattern> </filter-mapping>
OK! Servlet configuration is complete. We recommend that you use delegatingservletproxy, which should be easier to configure.