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
- web工程引入Spring
- Web项目中引入Spring
- Maven-创建Web工程,引入struts
- spring mvc web引入js等文件
- Tomcat+Spring web不能引入jquery
- Spring MVC Web 引入静态文件
- 使用maven为web工程引入jstl包时报错
- Myeclipse 10下Maven Web工程用自己引入的tomcat不能完全发布工程
- 库 lib 工程引入 dll 工程引入
- web.xml中引入SPRING配置文件classpath的含义
- 用spring和hibernate构建web工程
- spring在web工程中的运用
- maven创建web工程找不到Spring配置文件
- Initializing Spring root WebApplicationContext - Eclipse WEB工程
- Spring 配置 工程Web.xml中encodingFilter
- 非web工程下 spring+Hibernate整合
- 新手如何搭建spring web工程
- eclipse搭建简易maven spring web工程
- monkeytalk 启动、关闭 android应用程序 解决方法
- 优化memcache占用CPU太高
- redis集群
- 将eclipse代码导入到Android Studio
- 在 Eclipse 中通过 SSH 密钥登录远程服务器
- web工程引入Spring
- SiteMesh+Freemarker
- Android中如何禁用其他应用,并且不在桌面显示
- java基础总结 --- 泛型 擦除、边界、通配符、
- HDU 5626 Clarke and points 求平面最远哈曼顿距离
- CSS中bottom与margin-bottom的区别
- CentOS 6.5下Git服务器搭建
- 图解:麦肯锡工作术!
- SQL 中 SELECT 语句的执行顺序