Spring2.5注解(Annotations)的使用说明

    2013-05-04 20:01:59   来源:   评论:0 点击:

    一、使用spring2.5注解的准备:需要在spring的applicationContext.xml配置文件中添加有关注解的命名空间。 示例:xmlns:context="http://www.springframework.org/schema/context"http://www.springframework.o...
     

     
    一、使用spring2.5注解的准备
    • :需要在spring的applicationContext.xml配置文件中添加有关注解的命名空间。
       示例:
    xmlns:context="http://www.springframework.org/schema/context"
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd


      
    完整配置如下图:

    spring包含注解,aop的完整命名空间申明
    图:spring包含注解,aop的完整命名空间申明
      

    2:需要在applicationContext.xml配置文件中显式配置注解解析器
           <!-- 使Spring关注Annotation -->  
           <context:annotation-config />  
     
    二、spring的注解关键字的说明
     
    1:@Resource注解
    推荐使用@Resource而不是使用@Autowired的原因是:因为@Autowired注解是Spring提供的,而@Resource注解是J2EE提供的在JDK6中就已经包含@Resource注解了,所以它没有跟Spring紧密耦合 。
      @Resource注解先根据
    (1)指定的name属性查找
     (2)属性名称,
    (3)属性类型
    如果按这个顺序依次查找,不匹配则报错。
      说明: 基本类型不能用注解赋值,只有引用类型能够用注解赋值
     
    2:@Service用于标注业务层组件
    3:@Repository用于标注数据访问组件,即DAO组件
    4:@Controller用于标注控制层组件,如Struts中的Action或者springmvc的controller
    5:@Component泛指组件,当组件不好归类时,可以使用这个注解进行标注
    6:@Entity 实体类注解标签
    7:@Table  实体类对应的数据库实际的表名,不设置表名默认数据库表名与类名一致。
     

    三、hibernate使用注解声明实体类
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    @Entity
    public class Users {
    @Id
    @GeneratedValue
    private Integer id;
    private String username;
     
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getUsername() {
    return username;
    }
    public void setUsername(String username) {
    this.username = username;
    }
     
    }
     
     
    四、spring的applicationContext.xml配置文件配置注解后的变化   

          注意:spring2.5以后注解开启则将LocalSessionFactoryBean换成AnnotationSessionFactoryBean的方式了。
    以下是applicationContext.xml文件的完整示例:包括数据源的配置,事务的配置,aop的配置,重点关注红色字体部分。

    <?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:jee="http://www.springframework.org/schema/jee"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
             http://www.springframework.org/schema/tx
             http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
             http://www.springframework.org/schema/jee
             http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-2.5.xsd
                       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
             default-lazy-init="true">
             <description>Spring公共配置 </description>
             <bean  id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                       <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
                       <property name="ignoreResourceNotFound" value="true" />
                       <property name="locations">
                                <list>
                                  <value>classpath*:/conf/hibernate/hibernate.properties</value> 
                                </list>
                       </property>
             </bean>
       <!-- 使Spring关注Annotation -->  
        <context:annotation-config />  
             <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
       <context:component-scan base-package="com.company.*" /> 
     
             <!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
             <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
                       <!-- Connection Info -->
                       <property name="driverClassName" value="${jdbc.driver}" />
                       <property name="url" value="${jdbc.url}" />
                       <property name="username" value="${jdbc.username}" />
                       <property name="password" value="${jdbc.password}" />
                       <!-- Connection Pooling Info -->
                       <property name="initialSize" value="${jdbc.maxActive}" />
                       <property name="maxActive" value="${jdbc.maxActive}" />
                       <property name="maxIdle" value="${jdbc.maxIdle}" />
                       <property name="maxWait" value="${jdbc.maxWait}" />
                       <property name="poolPreparedStatements" value="${jdbc.poolPreparedStatements}" />
                       <property name="defaultAutoCommit" value="false" />
                       <property name="removeAbandoned" value="${jdbc.removeAbandoned}" />  
                       <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}" /> 
                       <property name="timeBetweenEvictionRunsMillis" value="3600000" />
                       <property name="minEvictableIdleTimeMillis" value="3600000" />
                      
             </bean>
     
             <!-- Hibernate配置 -->
             <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                       <property name="dataSource" ref="dataSource" />
                       <property name="configLocations">
                                <list>
                                         <value>classpath*:/conf/hibernate/hibernate.cfg.*.xml</value>
                                </list>
                       </property>
                       <property name="hibernateProperties">
                                <props>
                                         <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                                         <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                                         <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                                         <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                                         <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
                                         <prop key="current_session_context_class">thread</prop> 
            <prop key="hibernate.cache.use_query_cache">true</prop> 
                                </props>
                       </property>
                       <property name="packagesToScan" value="org.springside.examples.miniweb.entity.account" />
             </bean>
     
             <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
          <property name="dataSource" ref="dataSource"/>
       </bean> 
             <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                       <property name="sessionFactory" ref="sessionFactory" />
             </bean> 
                       <!-- 使用annotation定义事务 (注册事务注解处理器 )-->
              <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
       <!-- 配置事务的传播特性 -->  
       <tx:advice id="txAdvice" transaction-manager="transactionManager">  
            <tx:attributes>  
               <!-- <tx:method name="create*" propagation="REQUIRED" /> 
                <tx:method name="save*" propagation="REQUIRED" /> 
                <tx:method name="update*" propagation="REQUIRED" />  
                <tx:method name="delete*" propagation="REQUIRED" />  -->
                <!-- 所有以list开始的方法是只读 -->
                <tx:method name="list*" read-only="true" />  
                <tx:method name="get*" read-only="true" />      
                <tx:method name="find*" read-only="true" /> 
                <tx:method name="*"  />    
            </tx:attributes>  
        </tx:advice>  
       
      
       <aop:config>
          <!-- <aop:advisor pointcut="execution(* com.company.example.logic.*Logic.*(..))" advice-ref="txAdvice"/> -->
                         <aop:advisor pointcut="execution(* com.company.core.orm.hibernate.*Dao.*(..))" advice-ref="txAdvice"/>
              <aop:advisor pointcut="execution(* com.company..*Logic.*(..))" advice-ref="txAdvice"/>
       </aop:config>  
            
    </beans>
     
     
     

    相关热词搜索:Spring2.5 注解 Annotations 使用说明

    上一篇:spring3.2的简单介绍
    下一篇:spring依赖注入(dependency injection)的几种实现方式

    分享到: 收藏
    评论排行