ssm框架学习之(二) --srping3集成

现在spring 3集成
1 首先加入spring3所需要的包,在之前的struts包中有包含
   spring-asm-3.0.5.RELEASE.jar
   spring-beans-3.0.5.RELEASE.jar
   spring-context-3.0.5.RELEASE.jar
   spring-core-3.0.5.RELEASE.jar
   spring-expression-3.0.5.RELEASE.jar
   spring-web-3.0.5.RELEASE.jar
   struts2-spring-plugin-2.3.4.1.jar
2 web.xml 增加如下配置
 
   <!-- spring监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- spring监听器,使用scrope=request 时必须加上 -->
	<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:/config/applicationContext-beans.xml</param-value>
	</context-param>
  

3. 配置 applicationContext-beans.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd      
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd      
	http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd      
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd      
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
	<!-- 注解探测器 -->
	<context:component-scan base-package="com.rabbit.demo.*" />


</beans>
    

4 将LoginAction 托管给spring管理
  在类的最上边加上如下注解
  @Controller
  @Scope("request")
  同时新增一service 并注入该对象
  @Autowired
  private BonusBiz  biz;

  BonusBiz 代码片断...
 
 
         @Override
      public List<Bonus> browse() {
		// TODO Auto-generated method stub
		
		System.out.println("come here");
		return null;
    }
  

5 LoginAction 的登录方法中执行 biz.brower()..
  如果控制台中输出come here.则表示我们的spring配置成功
6 经测试OK,接下来在集成mybatis
7 上源码,[email protected]

猜你喜欢

转载自hackpro.iteye.com/blog/1726150