web工程引入Spring

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值