web工程引入Spring

来源:互联网 发布:mac上好用的vpn 知乎 编辑:程序博客网 时间:2024/06/04 19:46

1.新增application-context.xml配置文件,文件内容如下:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">        <context:component-scan base-package="com.package.path" />    <context:annotation-config/>        <bean id="InAppBeanFactory" class="com.util.InAppBeanFactory" /></beans>

InAppBeanFactory类的定义如下,用于从Spring context中获取bean:

public class InAppBeanFactory implements ApplicationContextAware{    private static ApplicationContext applicationContext;        @Override    public void setApplicationContext(ApplicationContext context)        throws BeansException    {        applicationContext = context;    }        /**     * 根据bean类型获取bean     * 要求bean的名称与类型一致     * @return T [返回类型说明]     * @exception throws [违例类型] [违例说明]     */    @SuppressWarnings("unchecked")    public static <T> T getBean(Class<T> cls)    {        return (T)applicationContext.getBean(cls.getSimpleName());    }    /**     * 根据bean名称获取bean     *      * @return Object [返回类型说明]     * @exception throws [违例类型] [违例说明]     */    public static Object getBean(String beanName)    {        return applicationContext.getBean(beanName);    }}

2.修改web.xml文件,增加如下内容:
 
<!-- Spring 配置 -->    <listener><span style="white-space:pre"></span><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:application-context.xml</param-value>    </context-param>

若要在spring容器初始化前做些操作,可以自定义一个监听器,继承ContextLoaderListener类,重写contextInitialized方法:

InAppContextLoaderListener内容如下:

public class InAppContextLoaderListener extends ContextLoaderListener{    public void contextInitialized(ServletContextEvent event)    {        initInAppCfg();                super.contextInitialized(event);    }        /**     * 初始化http url以及inapp配置     * 在Spring容器之前初始化,因为部分类中定义了静态成员需要获取http url     *      * @return void [返回类型说明]     * @exception throws [违例类型] [违例说明]     */    private void initInAppCfg()    { // customed initialization    }}

3.此时工程中已经可以使用Spring的注解了,但要在servlet中使用Spring的自动装配,需要在servlet中复写init方法:

BaseServlet中增加如下方法:

 /**     * 为Servlet中的bean实现自动注入     * @param config     * @throws ServletException     */    public void init(ServletConfig config) throws ServletException    {        super.init(config);        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());    }


0 0
原创粉丝点击