Spring三種方法的注解自動(dòng)注入問(wèn)題
Spring三種方法的注解自動(dòng)注入
1 @Autowired注解
@Autowired是Spring提供的自動(dòng)注入的方法,該注解可以放在變量和方法上,在bean+返回值類型的注解中,@Autowired還可以放在參數(shù)前;@Autowired默認(rèn)的注入是根據(jù)類型注入,如果相同類型的bean有多個(gè),可以配合@Qualifier使用,則會(huì)根據(jù)名字自動(dòng)注入;除了配合@Qualifier使用之外,還可以在相同類型的多個(gè)bean中的其中一個(gè)加上@Primary注解,那么根據(jù)類型注入就會(huì)第一注入有@Primary注解的bean。
示例代碼:
@Repository("stuDao1")
public class StudentDaoImpl1 implements StudentDao {
}@Primary
@Repository("stuDao2")
public class StudentDaoImpl2 implements StudentDao {
}@Service("stuService")
public class StudentService {
@Autowired //從IOC容器中找到StudentDao類型的bean加入到studentDao中,AutoWired注入不調(diào)用set方法
@Qualifier("stuDao")
private StudentDao studentDao;
//@Autowired //也可以把AutoWired注解放到set方法上,并且會(huì)調(diào)用set方法
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
}2 @Resource
@Resource注解是Java規(guī)范(JSR250)提供的方法,該注解默認(rèn)是根據(jù)bean的名字自動(dòng)注入,如果沒(méi)有找到對(duì)應(yīng)的名字,則會(huì)自動(dòng)根據(jù)類型查找并注入,可以使用name和type來(lái)指定根據(jù)名字還是類型來(lái)查找;@Resource注解同樣可以使用@Primary。
示例代碼:
public class StudentService {
// @Resource(name="stuDao1") //根據(jù)名字查找bean
@Resource(type=StudentDao.class) //根據(jù)類型查找bean
private StudentDao studentDao;
// @Resource //放在方法上
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
}3 @Inject
@Inject注解也是Java規(guī)范(JSR330)提供的方法,該注解默認(rèn)是根據(jù)bean的類型自動(dòng)注入,不過(guò)使用此注解需要導(dǎo)入javax-Inject.jar包;使用方法和@Autowired差不多一樣,也可以配合@Qualifier和@Primary使用。
示例代碼
@Service("stuService")
public class StudentService {
// @Inject
// @Qualifier("stuDao2")
private StudentDao studentDao;
@Inject
@Qualifier("stuDao2")
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
}Spring 注解版 屬性賦值 自動(dòng)注入
spring的屬性賦值,給一個(gè)bean的屬性進(jìn)行賦值,可以使用@Value注解。
該注解可以注入基本數(shù)值,字符串什么的@Value("zhangsan"),也可以結(jié)合SpEL表達(dá)式@Value("#{18+1}"),還可以讀取配置文件中的屬性@Value("${person.nickname}")(person.nickname,是外部配置文件的一個(gè)屬性名)。
Person.java(一個(gè)普通的bean)
package com.sixteen.entity;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
@Data
public class Person {
? ? /**
? ? ?* @Value注解
? ? ?* 1. 基本數(shù)值都可注入
? ? ?* 2. SPEl表達(dá)式 #{}
? ? ?* 3. ${} 獲取配置文件中的值
? ? ?*/
? ? // 配置文件中的值默認(rèn)加載進(jìn)環(huán)境變量
? ? /**可以這樣在環(huán)境中取出
? ? ?* ApplicationContext context = new AnnotationConfigApplicationContext(SpringPropertiesValueConfig.class);
? ? ?* Environment environment = context.getEnvironment();
? ? ?* String property = environment.getProperty("person.nickname");
? ? ?* System.out.println(property);
? ? ?*/
? ? @Value("zhangsan")
? ? private String name;
? ? @Value("#{18+1}")
? ? private Integer age;
? ? @Value("${person.nickname}")
? ? private String nickename;
}SpringPropertiesValueConfig.java(配置類)
package com.sixteen.config;
import com.sixteen.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
//使用@PropertySource導(dǎo)入外部配置文件,在bean里用@Value注解(@Value("${person.nickname}"))進(jìn)行屬性賦值
@PropertySource(value = {"classpath:person.properties"})
@Configuration
public class SpringPropertiesValueConfig {
? ? @Bean
? ? public Person person(){
? ? ? ? return new Person();
? ? }
}person.properties(外部配置文件)
person.nickname="xiaosan"
這樣就可以給實(shí)體類(bean)的屬性進(jìn)行賦值了,在springboot中讀取外部配置文件的值時(shí),還可以使用@ConfigurationProperties,這個(gè)注解之后再詳細(xì)說(shuō),也可以百度查詢資料,不過(guò)一般用于需要賦值的屬性過(guò)多時(shí)可以考慮,因?yàn)檫@一個(gè)注解就可以將所有屬性賦值,而不像@Value需要在每個(gè)屬性上加上。
自動(dòng)注入(又被叫做DI注入)即在一個(gè)組件中如果想用另一個(gè)組件就可以注入想用的組件(因?yàn)閟pring接管了所有的bean和組件,所以想要用別的bean或者組件就得從IOC容器中獲?。?。最典型的例子就是,在service層需要調(diào)用dao層方法(或者叫做mapper)這個(gè)時(shí)候就可以采用自動(dòng)注入。
此時(shí)使用的配置文件和上面的配置文件不一樣
SpringAutowiredConfig,java
package com.sixteen.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
?* @Autowired 自動(dòng)注入
?*
?* @Autowired
?* private YyyMapper mapper;
?* 1) 默認(rèn)按照類型去容器中找對(duì)應(yīng)的組件,找到就賦值 context.getBean(SpringService.class);
?* 2) 如果找到多個(gè)相同類型的組件,再將屬性的名稱(mapper)作為組件id去容器查找 context.getBean("mapper")
?* @Qualifier(),傳入bean的id,可以指定注入的bean,而不是用屬性名
?*
?* 自動(dòng)裝配默認(rèn)裝配好,如果找不到組件,就報(bào)錯(cuò)
?* 也可以設(shè)置@Autowired(require=false),如果找不到裝配的組件就不裝配,也不會(huì)報(bào)錯(cuò)
?*
?* 可以使用@Primary注解,使裝配首選為該bean,使用此注解時(shí)可再用@Qualifier()
?* ?XxxService{
?* ? ? ?YyyMapeer mapper;
?* ?}
?*/
@Configuration
@ComponentScan(basePackages = {"com.sixteen.service","com.sixteen.mapper"})
public class SpringAutowiredConfig {
}SpringService.java(service層)
package com.sixteen.service;
import com.sixteen.mapper.SpringMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SpringService {
? ? @Autowired
? ? private SpringMapper mapper;
}SpringMapper.java
package com.sixteen.mapper;
import org.springframework.stereotype.Repository;
@Repository
public class SpringMapper {
}一般情況這樣注入是沒(méi)問(wèn)題的,但如果你想自動(dòng)注入的組件/bean有多個(gè)在IOC容器時(shí),那么就有可能注入的不是你所想要的,具體規(guī)則如下:
/**
?* @Autowired 自動(dòng)注入
?*
?* @Autowired
?* private YyyMapper mapper;
?* 1) 默認(rèn)按照類型去容器中找對(duì)應(yīng)的組件,找到就賦值 context.getBean(SpringService.class);
?* 2) 如果找到多個(gè)相同類型的組件,再將屬性的名稱(mapper)作為組件id去容器查找 context.getBean("mapper")
?* @Qualifier(),傳入bean的id,可以指定注入的bean,而不是用屬性名
?*
?* 自動(dòng)裝配默認(rèn)裝配好,如果找不到組件,就報(bào)錯(cuò)
?* 也可以設(shè)置@Autowired(require=false),如果找不到裝配的組件就不裝配,也不會(huì)報(bào)錯(cuò)
?*
?* 可以使用@Primary注解,使裝配首選為該bean,使用此注解時(shí)可再用@Qualifier()
?* ?XxxService{
?* ? ? ?YyyMapeer mapper;
?* ?}
?*/像service層中屬性這樣寫(xiě)的話,@Autowired private SpringMapper mapper;,如果IOC容器中有不止一個(gè)SpringMapper這種組件,那么就會(huì)根據(jù)mapper這個(gè)臨時(shí)起的屬性名,作為bean的id去IOC容器中查找是否有組件,所以在必要時(shí),一個(gè)屬性的名稱不要隨意起變量名。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring(java,js,html) 截圖上傳圖片實(shí)例詳解
這篇文章主要介紹了spring(java,js,html) 截圖上傳圖片實(shí)例詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
java yaml轉(zhuǎn)properties工具類方式
這篇文章主要介紹了java yaml轉(zhuǎn)properties工具類方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
基于Ok+Rxjava實(shí)現(xiàn)斷點(diǎn)續(xù)傳下載
這篇文章主要為大家詳細(xì)介紹了基于Ok+Rxjava實(shí)現(xiàn)斷點(diǎn)續(xù)傳下載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
java中form以post、get方式提交數(shù)據(jù)中文亂碼問(wèn)題總結(jié)
這篇文章主要介紹了java中form以post、get方式提交數(shù)據(jù)中文亂碼問(wèn)題總結(jié),需要的朋友可以參考下2014-10-10
Java設(shè)計(jì)模式之監(jiān)聽(tīng)器模式實(shí)例詳解
這篇文章主要介紹了Java設(shè)計(jì)模式之監(jiān)聽(tīng)器模式,結(jié)合實(shí)例形式較為詳細(xì)的分析了java設(shè)計(jì)模式中監(jiān)聽(tīng)器模式的概念、原理及相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2018-02-02
Spring?注入集合實(shí)現(xiàn)過(guò)程示例詳解
這篇文章主要為大家介紹了Spring?注入集合實(shí)現(xiàn)過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
通過(guò)實(shí)例講解springboot整合WebSocket
這篇文章主要介紹了通過(guò)實(shí)例講解springboot整合WebSocket,WebSocket為游覽器和服務(wù)器提供了雙工異步通信的功能,即游覽器可以向服務(wù)器發(fā)送消息,服務(wù)器也可以向游覽器發(fā)送消息。,需要的朋友可以參考下2019-06-06

