一篇文章教帶你了解Java Spring之自動裝配
在Spring中有三種裝配的方式:
- 在xml中顯示的配置
- 在java中顯示配置
- 隱式的自動裝配bean
1.Bean的自動裝配
自動裝配是Spring滿足bean依賴的一種方式,Spring會在上下文中自動尋找,并自動給bean裝配屬性。
1.1 autowire="byName" 實現(xiàn)自動裝配
byname會自動在容器上下文中查找,和自己對象set方法后面的值對應(yīng)的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()方法中的參數(shù)即為配置文件中Bean的id的值 People people=(People) application.getBean("people"); people.getCat().shut(); people.getDog().shut(); } }
1.2 autowire="byType" 實現(xiàn)自動裝配
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.注解實現(xiàn)自動裝配
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小結(jié)
@Autowired和@Resource的區(qū)別:
- 都是用來自動裝配的,都可以放在屬性字段上
- @Autowired通過byType的方式實現(xiàn),而且必須要求這個對象存在
- @Resource默認(rèn)通過byname的方式實現(xiàn),如果找不到名字,則通過byType實現(xiàn)。如果兩個都找不到的情況就會報錯。
- 執(zhí)行順序不同:@Autowired通過byType;@Resource默認(rèn)通過byname的方式實現(xiàn)。
3.介紹一個idea中做筆記的小技巧
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
springboot項目中沒有識別到y(tǒng)ml文件解決辦法
這篇文章主要給大家介紹了springboot項目中沒有識別到y(tǒng)ml文件解決辦法,文中通過代碼示例給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01使用Spring?Boot?2.x構(gòu)建Web服務(wù)的詳細(xì)代碼
這篇文章主要介紹了使用Spring?Boot?2.x構(gòu)建Web服務(wù)的詳細(xì)代碼,主要基于JWT的身份認(rèn)證,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03Java ThreadPoolExecutor 線程池的使用介紹
Executors 是一個Java中的工具類. 提供工廠方法來創(chuàng)建不同類型的線程池,這篇文章主要介紹了Java ThreadPoolExecutor 線程池的使用介紹,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Java用 Rhino/Nashorn 代替第三方 JSON 轉(zhuǎn)換庫
本篇文章主要介紹了Java用 Rhino/Nashorn 代替第三方 JSON 轉(zhuǎn)換庫,非常具有實用價值,需要的朋友可以參考下2017-05-05Spring?Boot實現(xiàn)第一次啟動時自動初始化數(shù)據(jù)庫流程詳解
在現(xiàn)在的后端開發(fā)中,只要是使用關(guān)系型數(shù)據(jù)庫,相信SSM架構(gòu)(Spring?Boot?+?MyBatis)已經(jīng)成為首選,本文就以Spring?Boot?+?MyBatis為例,使用MySQL作為數(shù)據(jù)庫,完成數(shù)據(jù)庫初始化功能,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-05-05Java使用Condition實現(xiàn)精準(zhǔn)喚醒線程詳解
這篇文章主要為大家詳細(xì)介紹了Java如何使用Condition實現(xiàn)精準(zhǔn)喚醒線程效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02一文帶你掌握J(rèn)ava?ReentrantLock加解鎖原理
這篇文章將為大家詳細(xì)介紹一下Java中ReentrantLock?加鎖和釋放鎖的原理,以及和?Synchronized?的對比。文中的示例代碼講解詳細(xì),希望對大家有所幫助2022-12-12Mybatis Order by動態(tài)參數(shù)防注入方式
這篇文章主要介紹了Mybatis Order by動態(tài)參數(shù)防注入方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04