易学笔记--第1章:spring注入/1.5 autowire

第1章:spring注入/1.5 autowire:自动装载/2.1 概念

  • 概念
  1. 指的是不用显式地指定绑定依赖项,而是由spring容易根据某些信息进行动态依赖绑定,具体的信息包括:参数类型、方法名称或者指定名称

第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.1 参数类型/2.2.1.1 XML配置方式

  • 参数类型:基础参照: 第1章:spring注入/1.1 元数据配置/1.2 XML配置方式/1.2.4 XML配置方式实例源代码(Maven工程,已编译通过)
  1. 概念:指的是某个bean的待注入的属性(用setter方法标识)通过其它相同的数据类型的bean进行注入,这里的相同指的是接口的实现类或者超类继承的子类或者类本身
  2. 格式:

    <bean id="bean的名称" class="含包名的类名"  autowire="byType">

  3. 说明:
    1. bean的名称、含包名的类名参照: 第1章:spring注入/1.1 元数据配置/1.2 XML配置方式/1.2.2 XML文件结构/1.2.2.3 Bean的定义
    2. autowire="byName":固定写法
  4. 举例:

     <!-- AccountServiceImpl类中有个setAccountDao属性,它是通过类型AccountDao进行注入的  -->

         <bean id="accountService" class="com.wiley.beginningspring.ch2.AccountServiceImpl" autowire="byType"/>

         <!-- accountDao这个bean的类型AccountDaoInMemoryImpl刚好是AccountDao这个接口的实现类 -->

         <bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl"/>

  5. 注意:如果有多个bean满足某个待注入属性则注入会失败
    1. 举例:

      <bean id="accountService" class="com.wiley.beginningspring.ch2.AccountServiceImpl" autowire="byType"/>

           <bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl"/>

           <bean id="accountDaoJdbc" class="com.wiley.beginningspring.ch2.AccountDaoJdbcImpl"/>

      1. AccountDaoInMemoryImpl、AccountDaoJdbcImpl都是accountService的实现类
    2. 报错:

      Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountService' defined in class path resource [com/wiley/beginningspring/ch2/ch2-beans.xml]: Unsatisfied dependency expressed through bean property 'accountDao': : No qualifying bean of type [com.wiley.beginningspring.ch2.AccountDao] is defined: expected single matching bean but found 2: accountDao,accountDaoJdbc; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.wiley.beginningspring.ch2.AccountDao] is defined: expected single matching bean but found 2: accountDao,accountDaoJdbc


第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.1 参数类型/2.2.1.2 Java配置方式

  • Java配置方式:基础参照: 第1章:spring注入/1.1 元数据配置/1.3 JAVA配置方式/1.3.4 Java配置方式实例源代码(Maven工程,已编译通过)
  1. 概念:指的是某个bean的待注入的属性(用setter方法标识)通过其它相同的数据类型的bean进行注入,这里的相同指的是接口的实现类或者超类继承的子类或者类本身
  2. 格式:

    @Bean(autowire=Autowire.BY_TYPE)

  3. 说明:
    1. Autowire是枚举类型,有三个值
      1. BY_TYPE:通过类型
      2. BY_NAME:通过名称
      3. NO:不自动装载
  4. 举例:

    @Bean(autowire=Autowire.BY_TYPE)

         public AccountService accountService() {

               AccountServiceImpl bean = new AccountServiceImpl();

               return bean;

         }

         

         @Bean

         public AccountDao accountDao() {

               AccountDaoInMemoryImpl bean = new AccountDaoInMemoryImpl();

               //depedencies of accountDao bean will be injected here...

               return bean;

         }

         

         //通过类型匹配的话,这个accountDao()方法和accountDaoJdbc()要二选一

    //   @Bean

    //   public AccountDao accountDaoJdbc() {

    //         AccountDaoJdbcImpl bean = new AccountDaoJdbcImpl();

    //         return bean;

    //   }

  5. 注意:如果有多个bean满足某个待注入属性则注入会失败
    1. 举例:

      @Bean(autowire=Autowire.BY_TYPE)

           public AccountService accountService() {

                 AccountServiceImpl bean = new AccountServiceImpl();

                 return bean;

           }

           

           @Bean

           public AccountDao accountDao() {

                 AccountDaoInMemoryImpl bean = new AccountDaoInMemoryImpl();

                 //depedencies of accountDao bean will be injected here...

                 return bean;

           }

           

           @Bean

           public AccountDao accountDaoJdbc() {

                 AccountDaoJdbcImpl bean = new AccountDaoJdbcImpl();

                 return bean;

           }

      1. accountDao、accountDaoJdbc 这两个类型都匹配 AccountServiceImpl.setAccountDao,所以注入失败
    2. 报错:

      Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.wiley.beginningspring.ch2.AccountDao] is defined: expected single matching bean but found 2: accountDao,accountDaoJdbc


第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.2 参数类型/2.2.1.3  注解方式

  • 注解方式:

 

  1. @Autowired的默认方式就是by_Type,所以这种方式完成可以参照:第1章:spring注入/1.1 元数据配置/1.4 注解方式/1.4.4 注解方式实例源代码(Maven工程,已编译通过)
  2. 注意:如果有多个bean满足某个待注入属性则注入会失败
    1. 举例:
      1.  

        @Autowired

             public void setAccountDao(AccountDao accountDao) {

                   this.accountDao = accountDao;

             }

      2. @Repository

        public class AccountDaoInMemoryImpl implements AccountDao {

      3. @Repository

        public class AccountDaoJdbcImpl implements AccountDao {

      4. 类AccountDaoInMemoryImpl、AccountDaoJdbcImpl都是AccountDao接口的实现类,而@Autowired默认就是以类型为注入标准的,所以会有两个注入满足条件,最后会报错
    2. 报错:

      Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.wiley.beginningspring.ch2.AccountDao] is defined: expected single matching bean but found 2: accountDaoInMemoryImpl,accountDaoJdbcImpl


第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.2 方法名称/2.2.2.1 XML配置方式

  • XML配置方式

 

  1. 参照基础: 第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.1 参数类型/2.2.1.1 XML配置方式
  2. 不同:
    1. autowire="byType"  修改为 autowire="byName"
    2. 名称对比指的是:setter方法的名称和bean的名称对比

第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.2 方法名称/2.2.2.2 Java配置方式

  • Java配置方式

 

  1. 参照基础:  第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.1 参数类型/2.2.1.2 Java配置方式
  2. 不同:
    1. autowire=Autowire.BY_TYPE  修改为 autowire=Autowire.BY_Name
    2. 名称对比指的是:setter方法的名称和bean的名称(用@Bean标注的方法名)对比

第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.2 方法名称/2.2.2.3 注解方式

  • 注解方式

 

  1. 参照基础:   第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.2 参数类型/2.2.1.3 注解方式
  2. 不同:
    1. 在@Autowired下面再增加注解:@Qualifier("指定的名称"),比如:@Qualifier("accountDao")

      @Autowired

           @Qualifier("accountDao")

           public void setAccountDao(AccountDao accountDao) {

                 this.accountDao = accountDao;

           }

    2. 在@Repository下面再增加注解:@Qualifier("指定的名称"),比如:@Qualifier("accountDao")

      @Repository

      @Qualifier("accountDao")

      public class AccountDaoInMemoryImpl implements AccountDao {

    3. 名称对比指的是:@Qualifier指定的名称对比
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

易学笔记(qq:1776565180)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值