Spring三種方法的注解自動(dòng)注入問題
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)注入,如果沒有找到對應(yīng)的名字,則會(huì)自動(dòng)根據(jù)類型查找并注入,可以使用name和type來指定根據(jù)名字還是類型來查找;@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)注入,不過使用此注解需要導(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ì)說,也可以百度查詢資料,不過一般用于需要賦值的屬性過多時(shí)可以考慮,因?yàn)檫@一個(gè)注解就可以將所有屬性賦值,而不像@Value需要在每個(gè)屬性上加上。
自動(dòng)注入(又被叫做DI注入)即在一個(gè)組件中如果想用另一個(gè)組件就可以注入想用的組件(因?yàn)閟pring接管了所有的bean和組件,所以想要用別的bean或者組件就得從IOC容器中獲?。W畹湫偷睦泳褪?,在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)按照類型去容器中找對應(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 {
}一般情況這樣注入是沒問題的,但如果你想自動(dòng)注入的組件/bean有多個(gè)在IOC容器時(shí),那么就有可能注入的不是你所想要的,具體規(guī)則如下:
/**
?* @Autowired 自動(dòng)注入
?*
?* @Autowired
?* private YyyMapper mapper;
?* 1) 默認(rèn)按照類型去容器中找對應(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層中屬性這樣寫的話,@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)文章
OpenTelemetry初識(shí)及調(diào)用鏈Trace詳解
這篇文章主要為為大家介紹了OpenTelemetry初識(shí)及調(diào)用鏈Trace詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Java枚舉_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
enum 的全稱為 enumeration, 是 JDK 5 中引入的新特性,存放在 java.lang 包中。這篇文章給大家介紹Java枚舉相關(guān)知識(shí),需要的的朋友參考下2017-04-04
Springboot和Jpa實(shí)現(xiàn)學(xué)生CRUD操作代碼實(shí)例
這篇文章主要介紹了Springboot和Jpa實(shí)現(xiàn)學(xué)生CRUD操作代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
java hibernate使用注解來定義聯(lián)合主鍵
這篇文章主要介紹了java hibernate使用注解來定義聯(lián)合主鍵的相關(guān)資料,需要的朋友可以參考下2017-01-01
Spring?Boot實(shí)現(xiàn)web.xml功能示例詳解
這篇文章主要介紹了Spring?Boot實(shí)現(xiàn)web.xml功能,通過本文介紹我們了解到,在Spring Boot應(yīng)用中,我們可以通過注解和編程兩種方式實(shí)現(xiàn)web.xml的功能,包括如何創(chuàng)建及注冊Servlet、Filter以及Listener等,需要的朋友可以參考下2023-09-09
利用EasyPOI實(shí)現(xiàn)多sheet和列數(shù)的動(dòng)態(tài)生成
EasyPoi功能如同名字,主打的功能就是容易,讓一個(gè)沒見接觸過poi的人員就可以方便的寫出Excel導(dǎo)出,Excel導(dǎo)入等功能,本文主要來講講如何利用EasyPOI實(shí)現(xiàn)多sheet和列數(shù)的動(dòng)態(tài)生成,需要的可以了解下2025-03-03
eclipse創(chuàng)建項(xiàng)目沒有dynamic web的解決方法
最近上課要用到eclipse,要用到Dynamic web project.但是我下載的版本上沒有.接下來就帶大家了解 eclipse創(chuàng)建項(xiàng)目沒有dynamic web的解決方法,文中有非常詳細(xì)的圖文示例,需要的朋友可以參考下2021-06-06
springboot編程式事務(wù)TransactionTemplate的使用說明
這篇文章主要介紹了springboot編程式事務(wù)TransactionTemplate的使用說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Java數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組(動(dòng)力節(jié)點(diǎn)之Java學(xué)院整理)
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組(動(dòng)力節(jié)點(diǎn)之Java學(xué)院整理)的相關(guān)資料,包括創(chuàng)建和內(nèi)存分配,數(shù)組封裝后的使用等,需要的朋友參考下吧2017-04-04

