Spring 反射注入+全注解注入

来源:互联网 发布:squid软件 编辑:程序博客网 时间:2024/05/16 12:28

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);

           }

}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 天猫收到差评怎么办 天猫给差评骚扰怎么办 天猫客户差评怎么办 天猫没法给差评怎么办 天猫上限购一件怎么办 拍下商品不发货怎么办 天猫红包过期了怎么办 买天猫店被中介骗了钱怎么办 速卖通假货纠纷怎么办 天猫新开店被恶意拍下怎么办 新开的天猫店没有生意怎么办 银行的支票丢了怎么办 天猫积分用光了怎么办 淘宝店没有无线流量怎么办 京东卖家不发货怎么办会自动打款 一件代发顾客如果退款怎么办 京东以前的账号怎么办 旺旺对话框订单页面变宽了怎么办 阿里巴巴店铺上传图片很模糊怎么办 天猫跨店优惠券用了退货怎么办 淘宝店铺没流量没访客怎么办 京东微信和Q端黑号了怎么办? 买不了运费险了怎么办 拼多多5天不发货怎么办 天猫店手机发货成定制机怎么办 天猫退货上门取件退两件怎么办 买二手苹果手机没有账号怎么办 手机淘宝足迹不更新怎么办 淘宝申请退款不想退了怎么办 荣耀3c主板坏了怎么办 荣耀10天气删了怎么办 荣耀7i手机卡顿怎么办 荣耀v9总是自己拨号怎么办 华为荣耀5x很卡怎么办 华为荣耀6plus卡怎么办 华为手机触屏不灵敏怎么办 华为荣耀6x太卡怎么办 华为荣耀7太卡怎么办 荣耀手机开不开机怎么办 华为g750手机开不了机怎么办 华为手机开不了机怎么办