天天看點

依賴注入

使用屬性setter方法注入

使用Field注入

注入依賴對象可以采用手工裝配或自動裝配,在實際應用中建議使用手工裝配,因為自動裝配會産生未知情況,開發人員無法預見最終的裝配結果。

1.手工裝配依賴對象

手工裝配依賴對象,在這種方式中又有兩種程式設計方式

1)在xml配置檔案中,通過在bean節點下配置,如

<bean id="orderService" class="cn.itcast.service.OrderServicBean">

 <constructor-arg index="0" type="java.lang.String" value="xxx"/>//構造器注入

 <property name="name" value="zhao"/>//屬性setter方法注入

</bean>

2)在java代碼中使用 @Autowired或 @Resource注解方式進行裝配。但我們需要在xml配置檔案中配置以下資訊:

<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"

       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/context/spring-context-2.5xsd">

    <context:annotation-config/>

</beans>

這個配置隐式注冊了多個對注釋進行解析處理的處理器:

AutowiredAnnotationBeanPostProcessor,ConnonAnnotationBeanPosrProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor

注: @Resource注解在spring安裝目錄的lib\j2ee\common-annotations.jar

2.自動裝配依賴對象

Spring注解裝配

打context就出現提示資訊辦法:

window->MyEclipse->Files and Editors->XML->XML catalog->

使用JdbcTemplate擷取一條記錄

@Service @Transactional

public class PersonServiceBean implements PersonService{

 private JdbcTemplate jdbcTemplate;

 @Resource

 public void setDataSource(DataSource dataSource){

  this.jdbcTemplate=new JdbcTemplate(dataSource);

 }

 public Person getPerson(Integer id){

  RowMapper rowMapper=new RowMapper(){

   public ObjectmapRow(Result rs,int rowNum)throws SQLException{

    Person person=new Person();

    person.setId(rs.getInt("id"));

    person.setName(rs.getString("name"));

    return person;

   }

  }

  return (Person)jdbcTemplate.queryForObject("select * from person where id=?",new Object[]{id},new int[]{java.sql.Type.INTEGER},rowMapper);

}

采用注解方式配置事務

<bean id="txManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

 <propertyname="dataSource" ref="dataSource"/>

<!--采用 @Transaction注解方式使用事務-->

<tx:annotation-driventransaction-manager="txManager"/>

public class presonServiceBean implements PresonService{

依賴注入-手工裝配

在java代碼中使用 @Autowried或 @Resource注解方式進行裝配,這兩個注解的差別是:

@Autowried預設按類型裝配, @Resource預設按名稱裝配,當找不到與名稱比對的bean才會按類型裝配。

@Autowired

private PersonDao persondao;//用于字段上

public void setOrderDao(OrderDao orderDao){//用于屬性的setter方法上

 this.orderDao=orderDao;

 @Autowired 注解是按類型裝配依賴對象,預設情況下它要求依賴對象必須存在,如果允許null值,可以設定它required屬性為false。如果我們使用按名稱裝配,可以結合 @Qualifier注解一起使用。如下:

 @Autowried @Qualifier("personDaoBean")

 private personDao person;

 @Resource注解我 @Autowired一樣,也可以标注在字段或屬性的setter方法上,但它預設按名稱裝配。擔它預設按名稱裝配。名稱可以通過 @Resource的name屬性指定,如果沒有指定name屬性,當注解标注在字段上,即預設取字段的名稱作為bean名稱依賴對象,當注解标注在屬性的setter方法上,即預設取屬性名作為bean名稱尋找依賴對象。

 @ Resource(name="personDaoBean")

 private PersonDao personDao;//用于字段上

 注意:如果沒有指定name屬性,并且按照預設的名稱仍然找不到依賴對象時, @Resource注解會回退到按類型裝配。但一旦指定了name屬性,就隻能按名稱裝配了。

 private void annotationInject() {

  for(String beanName : sigletons.keySet()){//循環所有的bean對象

   Object bean = sigletons.get(beanName); //擷取bean對象

   if(bean!=null){//判斷bean對象是否存在

    try {

     PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();//擷取bean對象的屬性描述

     for(PropertyDescriptor properdesc : ps){

      Method setter = properdesc.getWriteMethod();//擷取屬性的setter方法

      if(setter!=null && setter.isAnnotationPresent(ItcastResource.class)){//判斷注解是否存在

       ItcastResource resource = setter.getAnnotation(ItcastResource.class);//取得注解

       Object value = null;

       if(resource.name()!=null && !"".equals(resource.name())){//得到name屬性

        value = sigletons.get(resource.name());//取得bean對象

       }else{

        value = sigletons.get(properdesc.getName());//取得屬性名稱

        if(value==null){

         for(String key : sigletons.keySet()){

          if(properdesc.getPropertyType().isAssignableFrom(sigletons.get(key).getClass())){//擷取屬性描述

           value = sigletons.get(key);

           break;

          }

         }

        }        

       }

       setter.setAccessible(true);

       setter.invoke(bean, value);//把引用對象注入到屬性

      }

     }

     Field[] fields = bean.getClass().getDeclaredFields();

     for(Field field : fields){

      if(field.isAnnotationPresent(ItcastResource.class)){

       ItcastResource resource = field.getAnnotation(ItcastResource.class);

       if(resource.name()!=null && !"".equals(resource.name())){

        value = sigletons.get(resource.name());

        value = sigletons.get(field.getName());

          if(field.getType().isAssignableFrom(sigletons.get(key).getClass())){

       field.setAccessible(true);//允許通路private字段

       field.set(bean, value);

    } catch (Exception e) {

     e.printStackTrace();

    }

上一篇: 依賴注入