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:是通过"byName"的方式对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);

           }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值