struts和spring集成

Spring开发环境搭建:
1.jar包:lib\aspectj下的aspectjrt.jar和aspectjweaver.jar
lib\log4j下的log4j-1.2.14.jar
lib\jakarta-commons下的commons-logging.jar
dist\spring.jar

Struts和Spring的集成
在Action中创建BeanFactory
(1)BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-beans.xml");
UserManager userManager = (UserManager) factory.getBean("userManager");
(2)初始化就加载,使用listener
配置:web.xml <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
注:从samples\jpetstore\war\WEB-INF\web.xml中找
Action中的代码:
BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
UserManager userManager = (UserManager)factory.getBean("userManager");
userManager.login(username, password);

你可能感兴趣的:(spring,Web,log4j,xml,struts)