一篇文章教帶你瞭解Java Spring之自動裝配

在Spring中有三種裝配的方式:

  • 在xml中顯示的配置
  • 在java中顯示配置
  • 隱式的自動裝配bean

1.Bean的自動裝配

自動裝配是Spring滿足bean依賴的一種方式,Spring會在上下文中自動尋找,並自動給bean裝配屬性。

1.1 autowire=”byName” 實現自動裝配

byname會自動在容器上下文中查找,和自己對象set方法後面的值對應的bean id。

需要保證所有bean的id唯一,並且這個bean需要和自動註入的屬性的set方法的值一致。

People.java

package org.example;
public class People {
    private Cat cat;
    private Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

cat.java

package org.example;
public class Cat {
    public void shut(){
        System.out.println("喵喵喵……");
    }
}

Dog.java

package org.example;
public class Dog {
    public void shut(){
        System.out.println("汪汪汪……");
    }
}

applicationContext.xml

<?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.xsd">
    <bean id="cat" class="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" autowire="byName">
        <property name="name" value="小狂神"></property>
    </bean>
</beans>

測試類

package org.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
    public static void main( String[] args ) {
        //獲取ApplicationContext對象
        ApplicationContext application=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        //通過ApplicationContext獲得TestHello對象
        //getBean()方法中的參數即為配置文件中Bean的id的值
        People people=(People) application.getBean("people");
        people.getCat().shut();
        people.getDog().shut();
    }
}

1.2 autowire=”byType” 實現自動裝配

byType:會自動在容器上下文中查找,和自己對象屬性類型相同的bean。

需要保證所有bean的class唯一,並且這個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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="cat" class="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" autowire="byType">
        <property name="name" value="小狂神"></property>
    </bean>
</beans>

2.註解實現自動裝配

JDK1.5支持的註解,Spring2.5就支持註解瞭。

2.1 配置註解

隻需在applicationContext.xml文件中加入

<?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:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--開啟註解的支持-->
    <context:component-scan base-package="org.example"/>
</beans>

2.2 @Autowired註解

直接在屬性上使用即可,也可以在set方式上使用。使用@Autowired 可以不用編寫set方法瞭,前提是你這個自動裝配的屬性在IOC(Spring)容器中存在,且符合byname。

People.java

package org.example;
import org.springframework.beans.factory.annotation.Autowired;
public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
//set方法可以省略
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

applicationContext.xml

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="cat" class="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" ></bean>
    <context:component-scan base-package="org.example"/>
</beans>

2.3 @Resource註解

People.java

package org.example;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
public class People {
//如果沒有(name="cat")那麼就會找不到
    @Resource(name = "cat2")
    private Cat cat;
    @Resource
    private Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

applicationContext.xml

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="cat1" class="org.example.Cat"></bean>
    <bean id="cat2" class="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" ></bean>
    <context:component-scan base-package="org.example"/>
</beans>

2.4小結

@Autowired和@Resource的區別:

  • 都是用來自動裝配的,都可以放在屬性字段上
  • @Autowired通過byType的方式實現,而且必須要求這個對象存在
  • @Resource默認通過byname的方式實現,如果找不到名字,則通過byType實現。如果兩個都找不到的情況就會報錯。
  • 執行順序不同:@Autowired通過byType;@Resource默認通過byname的方式實現。

3.介紹一個idea中做筆記的小技巧

總結

本篇文章就到這裡瞭,希望能夠給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容!

推薦閱讀: