zoukankan      html  css  js  c++  java
  • Spring 反射注入+全注解注入

    Spring IoC容器会先把所有的Bean都进行实例化,不管是要用到的火鼠用不到的,如果你想暂时不进行Bean的实例化,要用到属性
    lazy-init="true".

    Spring的三种注入方式:

    ① 构造注入:通过构造器(constructor)注入

    ② 设值注入:通过Setter方法注入

    ③ 反射注入:通过注解(annotation)的方式注入

    Spring 对Bean的四种自动装配(注入)方式

    autowire= "byName" :通过Bean的名字对属性进行值注入

    autowire="byType":通过属性的类型来对属性进行值注入。<慎用>

    autowire="constructor":通过构造器来对属性进行值注入。<慎用>

    autowire="autodetect":容器自动对属性进行值注入,先用constructor的方式,如果没有构造器,再用byType的方式。<尽量不用>

    通过注解的方式对Bean的自动注入:

    @Autowired :是通过"byType"的方式对Bean属性进行自动注入的,如果Bean属性的类型有多个,那么就添加@Qualifier("beanName") 加以区分。

    @Resource:是通过"byType"的方式对Bean属性进行自动注入的,如果Bean属性的类型有多个,那么就用@Resource("beanName") ,

    @Resource("beanName") 是通过"byName"的方式对Bean属性进行自动注入的。

    Spring Bean的应用范围

    scope="singleton":单例(默认),对所有应用都只生成一个实例

    scope="prototype":对每个应用都生成一个实例

    scope="request":在web应用中有效,对每个请求都生成一个实例

    scope="session":在web应用中有效,对每个会话都生成一个实例

    scope="global-session":在web应用中有效,全局Http会话

    Spring的IoC组件

    @Repository:持久层组件,用于标注数据访问层组件,如DAO层组件。

    @Service:服务成组件,用于标注业务层组件,如Service层组件;会根据Bean的类型实例化一个首字母为小写的bean的实例,如果要修改bean name可以在@Service("custome beanName")。

    @Controller:用于标注控制层主键,如Strust的Action层组件。

    @Component:泛指组件,当组件不好归类的时候可以用这个标注。

    当用了上面的annotation的时候就不需要再在applicationContext.xml定义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:context="http://www.springframework.org/schema/context"
        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-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
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

              <!--全注解-->
              <context:annotation-config />
              <context:component-scan base-package="com.demo.service" />
              <context:component-scan base-package="com.demo.dao" />
              <context:component-scan base-package="com.demo.controller" />
       
             <tx:annotation-driven transaction-manager="transactionManager"/>
     
            <bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
                       <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
                       <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
            </bean> 

            <!-- 定义事务管理器(声明式的事务) --> 
            <bean id="transactionManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                       <property name="sessionFactory" ref="sessionFactory" />
            </bean>
       
            <bean id="hibernateTempate"  class="org.springframework.orm.hibernate3.HibernateTemplate">
                         <property name="sessionFactory" ref="sessionFactory"/>
            </bean>
    </beans>

    public class Customer {

               private Long customerId;

               private String name;

               //省略getter 和 setter

    }

    @Repository("customerDAO")
    public class CustomerDAO {

        @Resource
        private HibernateTemplate hibernateTemplate;
     
               public Customer findByPrimaryKey(long customerId) {
                         return (Customer) hibernateTemplate.get(Customer.class, customerId);
               }
     
               public void save(Customer customer) {
                         hibernateTemplate.save(customer);
               }
     
              public void update(Customer customer) {
                        hibernateTemplate.update(customer);
              } 
    }

    @Service
    @Transactional(readOnly=true)
    public class CustomerService {

        @Autowired
        @Qualifier("customerDAO")
        private CustomerDAO customerDAO;
     
        public Customer findByPrimaryKey(long customerId) {
            return customerDAO.findByPrimaryKey(customerId);
        }
      
        @Transactional(propagation=Propagation.REQUIRED)
        public void save(Customer customer) {
           customerDAO.save(customer);
        }
     
        @Transactional(propagation=Propagation.REQUIRED)
        public void update(Customer customer) {
           customerDAO.update(customer);
        }
     }

    @Controller
    public class CustomerController {
     
        @Resource
        CustomerService customerService;

        public void modifyCustomerAndProduct() {
      
                     Customer customer = customerService.findByPrimaryKey(1);
                     customer.setName("joe");
                     customerService.update(customer);

               }

    }

  • 相关阅读:
    整理打印PI值
    使用自连接、for xml path('')和stuff合并显示多行数据到一行中(转)
    ThinkPHP+jQuery EasyUI Datagrid查询数据的简单处理
    ThinkPHP学习(三)
    Apache+PHP配置PATHINFO的一个小问题
    ThinkPHP学习(二)
    接口签名工具类
    RSA加密和解密工具类
    kafka项目中踩到的一个坑(客户端和服务器端版本不一致问题)
    自己实现字符串转整数(不使用JDK的字符串转整数的方法)
  • 原文地址:https://www.cnblogs.com/interdrp/p/3665557.html
Copyright © 2011-2022 走看看