Spring 反射注入+全注解注入

http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380143fd3d1027fa3c215cc79031c1061e5bc23251100ce95223a54b2081ab9b66d232a0927b69ece8d4fdcbb902b2f8927347716804214d212b2df037881769f4d99aa0e97bce74398b9a5d5c85523dd22766df0f79c2b0203be19e71541f4d6e95f662b07bb9d2715f84e062f882230a136fbf7456c108086ca2a48d45cd27610e7b843b52961b504d4690c5344b74dc11f272327934f308e4e2a73e2fc5c973d093034c14ba4b8b1a19a439ba89926eef8dcdc2a&p=cb6adc0c8c934eae5bf5c771075e&newp=882a9446809b17fe13be9b744f5292695803ed6338d5864d2985d8&user=baidu&fm=sc&query=%B7%B4%C9%E4%CA%B5%C0%FD%BB%AF%B5%C4%C0%E0%D6%D0%D7%A2%BD%E2%D7%A2%C8%EB%BB%E1%C9%FA%D0%A7&qid=&p1=1
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);

           }

}

 
原文地址:https://www.cnblogs.com/svennee/p/4080693.html