Spring深入浅出(七),自动装配,byName/byType

Bean 的装配可以理解为依赖关系注入,Bean 的装配方式也就是 Bean 的依赖注入方式。Spring 容器支持多种装配 Bean 的方式,如基于 XML 的 Bean 装配、基于 Annotation 的 Bean 装配和自动装配等。

自动装配就是指 Spring 容器在不使用 <constructor-arg> 和<property> 标签的情况下,可以自动装配(autowire)相互协作的 Bean 之间的关联关系,将一个 Bean 注入其他 Bean 的 Property 中。

使用自动装配需要配置 <bean> 元素的 autowire 属性。常用的:no/byName/byType。什么是byType,什么是byName?

<bean id="userServiceImpl"
            class="cn.com.bochy.service.impl.UserServiceImpl"
            autowire="byName">
       </bean>  
      <bean id="userDao"                                         
             class="cn.com.bochy.dao.impl.UserDaoImpl">
</bean>

byName:根据属性名(Bean的id或者name)自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。 

byType:如果容器中存在一个与指定class类型相同的bean,那么将与该属性自动装配;如果存在多个该类型bean,那么抛出异常,并指出不能使用byType方式进行自动装配;如果没有找到相匹配的bean,则什么事都不发生。

一、自动装配 byName

1. 创建主业务类 

package com.clzhang.spring.demo;

public class TextEditor {
       private SpellChecker spellChecker;
       private String name;
       public void setSpellChecker( SpellChecker spellChecker ){
          this.spellChecker = spellChecker;
       }
       public SpellChecker getSpellChecker() {
          return spellChecker;
       }
       public void setName(String name) {
          this.name = name;
       }
       public String getName() {
          return name;
       }
       public void spellCheck() {
          spellChecker.checkSpelling();
       }
}

2. 创建业务逻辑实现类

package com.clzhang.spring.demo;

public class SpellChecker {
    public SpellChecker() {
        System.out.println("Inside SpellChecker constructor.");
    }

    public void checkSpelling() {
        System.out.println("Inside checkSpelling.");
    }
}

3. 创建主程序

package com.clzhang.spring.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        TextEditor te = (TextEditor) context.getBean("textEditor");
        te.spellCheck();
    }
}

4. 配置文件之前的写法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.clzhang.spring.demo.TextEditor" autowire="byName">
       <property name="spellChecker" ref="spellChecker" />
       <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.clzhang.spring.demo.SpellChecker">
   </bean>
</beans>

5. 配置文件自动装配的写法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.clzhang.spring.demo.TextEditor" autowire="byName">
       <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.clzhang.spring.demo.SpellChecker">
   </bean>
</beans>

6. 运行

Inside SpellChecker constructor.
Inside checkSpelling.

二、自动装配 byType

针对上面的代码,只需要更改配置文件,由:byName更改为byType即可。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.clzhang.spring.demo.TextEditor" autowire="byType">
       <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.clzhang.spring.demo.SpellChecker">
   </bean>
</beans>

本文参考:

https://www.w3cschool.cn/wkspring/8dhy1mmd.html

原文地址:https://www.cnblogs.com/nayitian/p/15002118.html