框架整合小小总结【SSH】注解式

Spring 注解式注册 bean:

大致分为以下几步:

  1. 开启 context 空间支持
  2. 开启自动扫描功能,指定扫描包路径
  3. 使用注解配置 bean (使用@Component 注解)
  4. 给 bean 注入属性(基本类型和引用类型)
  5. 设置 bean 的生命周期
  6. 设置 bean 的作用域(默认为单例)


详细:

1 . 开启 context 空间支持

2 . 开启自动扫描功能,指定扫描路径(说明:扫描路径可以设置多个,中间使用,隔开,扫描路径包含指定包和子包内所有的类)

<beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:context="http://www.springframework.org/schema/context" 
               xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context                                     
            http://www.springframework.org/schema/context/spring-context.xsd
            ">
<!-- 这里指定的是带有注解配置的bean,基本所有的类都要配置,dao,service -->
<context:component-scan base-package="com.msym.dao,com.msym.service"></context:component-scan>
</beans>

对于不同的层有三种不同的 bean 注解:

  • @Repository 用于数据层实现类标注
  • @Service用于业务层的实现类的注解
  • @Controller用于控制层实现类的注解

4.1 为 bean 注入简单类型:

  1. 在属性名上方声明该属性自动装配 @Autowired
  2. 在属性名上方声明该属性注入的值 @Value(value)

【注意:注解自动装配属性值无需提供对应属性的setter方法】

例如:

@Autowired
@Value("msym")
private String msg;

这样设置之后就无需再提供该属性的 get/set 方法了,

4.2 为 bean 注入引用数据类型:

  1. 在属性名上方声明该属性自动装配 @Aotuwired
  2. 在属性名上方声明该属性注入的值 @Qualifier(bean 引用的名字)

【注意:注解自动装配属性值无需提供对应属性的setter方法】

例如:

@Autowired
@Qualifier("userDao")
private UserDao userDao;

5 . 设置 bean 的生命周期(即配置创建 bean 时要执行的方法,和 bean 销毁时需要执行的方法)

@PostConstruct

功能:为当前 Bean 指定 init-method 参数

格式:定义在成员方法的上方,兼容静态方法

@PreDestroy

功能:为当前 Bean 指定 destory-method 参数

格式:定义在成员方法的上方,兼容静态方法

注意:要求当前类被注册为 Bean,否则无效果

6 . 设置 bean 的作用域(设置 scope 属性)

在类的定义上方添加 @Scope 指定 Bean 的作用域(特别要注意给 action 注解时要指定为 prototype)

常用:@Scope("prototype")

默认:@Scope("singleton")

 

Spring 注解式整合 JUnit:

大致步骤:

  1. 导包
  2. 设置类运行器【注解添加到运行程序类的上方 @RunWith(SpringJUnit4Class.class)】
  3. 设置读取 Spring 的配置文件的路径【也在运行程序类上方 @ContextConfiguration(location={“classpath:/applicationContext.xml”})  】

例子:

@RunWith(SpringJUnit4ClassRunner.class)        //设置JUnit运行器为Spring
@ContextConfiguration(locations={"classpath:/applicationContext.xml"})    //加载配置
public class App {
        //要测试的Bean必须称为测试类的属性注入进来,然后对其进行测试
         @Autowired
        @Qualifier("testBean")
        private Bean1 testBean;
        
        @Test
        public void testJunit(){
            testBean.fn();
        }
}

 

注意:使用 junit 整合 spring 时,必须保障运行类中要注入要测试的 Bean。整合完毕后,当前的测试类将作为 Spring 的 Bean 出现,而测试的 Bean 对象作为测试类的注入资源进行运行。(也就是被测试的对象要注入进来)

 

 

Spring 注解式实现 AOP:

大致步骤:

  1. 在核心配置文件中开启 aop 命名空间
  2. 在核心配置文件中配置支持@aspectj
  3. 将所有参与 AOP 的类配置为 bean
  4. 在切面类上添加切面声明注解@Aspect
  5. 将切面类中的方法配置为指定类型的通知(之前,之后,环绕,异常和返回通知5种),并配置该通知方法的切入点表达式

详细:

1 . 在核心配置文件中开启 aop 命名空间

2 . 开启支持 @aspectj 注解,

3 . 将所有参加 AOP 的类配置为 bean

<?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"
       xsi:schemaLocation="
        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
        ">
    <!-- 首先上面需要引入 aop 的命名空间,然后开启注解式AOP功能 -->    
    <aop:aspectj-autoproxy/>
    <!-- 将需要参加 AOP 的类配置为 bean -->
    <!-- 这里指定的是带有注解配置的bean,基本所有的类都要配置,dao,service --> 
    <context:component-scan base-package="com.msym.dao,com.msym.service"></context:component-scan>
</beans>

3 . 在切面类中添加@Aspect注解

@Aspect
public class MyAdvice {…}

 

4 . 将切面类中的方法配置为通知,使用五个注解之一,并指定切入点表达式

@Aspect
public class MyAdvice {
    @Before("execution(* con.msym.service.UserImpl.add())")
    public void before(JoinPoint jp) {
        System.out.println("before");
    }
    . . . 
}

Spring 提供的注解式事务【结合 AOP】:

大致步骤:

  1. 在配置文件中开启 aop 和 tx 命名空间
  2. 定义事务管理器,并注入数据源 datasource (要保证这个数据源和 dao 层的数据源一致,主要是需要数据库连接的信息)
  3. 在配置文件中开启注解式事务驱动管理,并为其指定事务管理器
  4. 对要添加事务的接口方法上添加声明 @Transactional ,也就是切入点。【推荐在接口上声明事务,这样改接口的所有实现类中的方法都将携带事务 】

对于第二点,常见的事务管理器有五种:

  • DataSourceTransactionManager【使用Spring JDBC 或者 Mybatis 进行持久化时使用这个 】
  • HibernateTransactionManager【使用 Hibernate3.0 持久化时使用这个 】
  • JpaTransactionManager【使用 JPA 进行持久化时使用这个 】
  • JdoTransactionManager【使用 Jdo 进行持久换时使用 】
  • JtaTransactionManager【使用 JTA 实现事务管理时,在一个事务跨多个资源时必须使用 】

详细:

1 . 开启 aop 和 tx 命名空间

2 . 定义事务管理器,注入数据源

3 . 开启注解事务驱动,指定事务管理器

<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"
       xsi:schemaLocation="
        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/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        ">
        <!-- 开启注解式事务驱动 -->
        <tx:annotation-driven transaction-manager="txManager"/>

        <!-- 指定事务管理器,并注入数据源 -->
        <bean id=" txManager " class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
             <property name="dataSource" ref="dataSource"></property>
        </bean>

        <!-- DataSource -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
             <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
             <property name="url" value="jdbc:mysql://localhost:3306/springdb"/>
             <property name="username" value="root"/>
             <property name="password" value="root"/>
       </bean>
</beans>

4 . 在需要事务的地方添加 @Transactional【一般在接口上】(我在这里是写在了方法上)

@Transactional
public void transfer(String out, String in , Double money){
    accountDao.outMoney(out, money);
    accountDao.inMoney(in, money);
}

在上面的 @Transactional 注解中可以添加事务相关的属性。详见【http://www.cnblogs.com/daimajun/p/7136422.html】。

Spring 为 Hibernate 准备了两个模板类:

【jdbcTemplate 和 HibernateTemplate】

 

jdbc.properties文件:【用于存储数据库连接信息】

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/springdb
jdbc_username=root
password=root
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        ">
    <!-- 导入jdbc.properties文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 将自己的Dao层类配置为 bean ,并注入jdbcTemplate, -->
    <bean id="userDao" class="com.msym.spring.jdbc.UserDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    
    <!-- jdbcTemplate 模板类,为其注入数据源-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource3"/>
    </bean>
<!-- 以下是三种数据源的配置 -->
    <!-- dataSource -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driverClass}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>
    
    <!-- dbcp -->
    <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driverClass}"/> 
        <property name="url" value="${url}"/> 
        <property name="username" value="${username}"/> 
        <property name="password" value="${password}"/>
    </bean>
    
    <!-- c3p0 -->
    <bean id="dataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClassName" value="${driverClass}"/> 
        <property name="url" value="${url}"/> 
        <property name="username" value="${username}"/> 
        <property name="password" value="${password}"/>    
    </bean>
</beans>

虽然有两个模板类,但我还是使用自己实现的 DAO,注入 SessionFactory 的方式,这篇博客我的Hibernate实现的DAO层】,然后将 Dao层的对象配置成 bean即可。

 

SSH整合详细:

1 . Jar包整合:

Struts2:必备包+整合包【也称为插件包】

基本包:(11个),可以去这里找 : apps\struts2-blank\WEB-INF\lib\*.jar

插件包:(3个),

  • struts2-spring-plugin-x.x.x.jar【struts整合spring需要】
  • struts2-Ajax-plugin-x.x.x/jar【struts2整合AJAX需要】
  • struts2-convention-plugin-x.x.x.jar【注解开发需要】

配置文件:struts.xml ,web.xml

Spring3:

核心包:(4个)【没写版本和后缀】

  • spring-beans
  • spring-core
  • spring-context
  • spring-expression

日志包:(2个)

  • com.springsource.org.apache.commons.loging
  • com.springsource.org.apache.log4j

AOP包:(4个)

  • spring-aop
  • spring-aspects
  • com.springsource.org.aopalliance
  • com.spring.org.aspectj.weaver

JDBC包:(2个)

  • spring-jdbc
  • spring-tx

整合ORM框架需要的包:(1个)

  • spring-orm

WEB集成:(1个)

  • spring-web

配置文件:

  • applicationContext.xml
  • web.xml 文件中添加spring 的 IoC 容器加载监听器
  • log4j.properties文件

Hibernate:

核心jar包:(1个)

  • Hibernate3.jar

必须的 jar 包:(6个)

  • lib\required\目录下所有的 jar 包
  • antlr-x.x.x.jar
  • commons-collections-x.x.x.jar
  • dom4j-x.x.x.jar
  • javassist-x.x.x.jar
  • jta-x.x.x.jar
  • slf4j-api-x.x.x.jar

jpa 的 jar 包:(1个)

  • hibernate-jpa-2.0-api-1.0.1.Final.jar

slf4j 整合log4j 的 jar 包(1个)【log4j已在spring中导入】

  • slf4j-log4j-1.7.2.jar

配置文件:

  • hibernate.cgf.xml
  • Model.hbm.xml

2 . 框架间细节的整合

     web.xml 文件中添加三个配置:

  • Struts2 核心过滤器【StrutsPrepareAndExecuteFilter】
  • Spring 容器监听器【ContextLoaderListener】
  • Hibernate 的延迟加载问题【OpenSessionInViewFilter】
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     <!-- 
        applicationContext对象加载仅加载一次,服务器启动时加载,
        使用 web中的监听器机制,必须配置在最上面,因为不管什么对象都需要Spring来管理
      -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

   <!-- 将事务开启到业务层,需要配置 OpenSessionInViewFilter过滤器,而且必须在 Struts核心过滤器之上 -->
    <filter>
         <filter-name>openSessionInView</filter-name>
         <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
         <filter-name>openSessionInView</filter-name>
         <url-pattern>/*</url-pattern>
    </filter-mapping>     

     <!-- struts核心过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>            
</web-app>

 

Struts.xml 文件:

整合spring 和 Struts2 时,直接将 struts2 的action配置为 bean,并且将其 scope 属性设置为 prototype

 

整合 Hibernate 一般推荐将 hibernate.cfg.xml 文件的配置信息搬到 applicationContext.xml 里面

<!-- 导入jdbc.properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- SessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <!-- 注入数据源-->
    <property name="dataSource" ref="dataDource"></property>
    <!-- 可选配置,hibernate前缀不能丢-->
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format">true</prop>
        </props>
    </property>
    
    <!-- 资源注册 -->
    <property name="mappingResource">
        <list>
            <value>com/msym/user/UserModel.hbm.xml</value>
            <!-- 推荐使用通配符的形式进行资源注册 -->
            <!--
                <value>com/msym/*/*.hbm.xml</value>
            -->
        </list>
    </property>
</bean>

<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="diverClass">${diverClass}</property>
    <property name="url">${url}</property>
    <property name="username">${username}</property>
    <property name="password">${password}</property>
</bean>
posted @ 2017-06-07 15:00  码上猿梦  阅读(430)  评论(0编辑  收藏  举报