Spring?IOC?常用注解與使用實(shí)例詳解
@Component
注解@component代表spring ioc 會(huì)把這個(gè)類掃描生成Bean實(shí)例
@Component
public class Role{
@Value("1")
private Long id;
@Value("role_name_1")
private String roleName;
@Value("role_note_1")
private String note;
/***setter and getter****/
}@Autowired
注解@Autowired代表在spring ioc 定位所有的Bean后,這個(gè)字段需要按類型來(lái)進(jìn)行注入。
@Component
public class RoleImpl_1 implements RoleServer{
@Autowired
private Role role = null;
public .....
}@Qualifier
?如果一個(gè)接口被兩次實(shí)現(xiàn),則使用@Autowired注解來(lái)進(jìn)行該接口注入會(huì)產(chǎn)生異常,因?yàn)锧Autowired無(wú)法確定要使用的是哪一個(gè)實(shí)現(xiàn)類。可以使用@Qualifier注解來(lái)進(jìn)行歧義消除。
@Component
public class RoleController{
@Autowired
@Qualifier("roleImple_2")
private RoleServer server = null;
public .....
}@Bean
?在注解都都是通過(guò)@component來(lái)進(jìn)行裝配Bean,但是@Component只能注解在類上,無(wú)法注解到方法上。而注解@Bean可以注解到方法上
@Bean(name = "dataSource")
public DataSource getDataSource(){
Properties props = new Properties();
props.setProperty("driver","com.mysql.cj.jdbc.Driver");
props.setProperty("url","jdbc:mysql://localhost:3306/db");
...
try{
dataSource = BasicDataSourceFactory.createDataSource(props);
}catch(Execption e){
e.printStackTrace();
}
return dataSource;
}@Component
public class RoleController{
@Autowired(name = "dataSource")
private DataSource dataSource = null;
public .....
}@ImportResource
?如果我們將DataSource使用xml配置文件來(lái)進(jìn)行配置,我們就無(wú)法使用注解@Bean來(lái)進(jìn)行裝配。這時(shí)注解@ImportResource可以進(jìn)行混合裝配(將第三方的xml引入進(jìn)來(lái)進(jìn)行裝配)。
<!--dbSource.xml-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db"/>
.......
</bean>@ComponentScan(basePackages={"com.test"})
@ImportResource({"classpath:dbSource.xml"}) //將dbSource.xml配置文件裝配到Ioc中來(lái)
public class ApplicationConfig{
}@Component
public class RoleController{
@Autowired
private DataSource dataSource = null;
public .....
}如果有多個(gè)xml文件,我們都想引用進(jìn)來(lái),可以在dbSource.xml配置文件中使用import元素來(lái)加載它
<!--spring-dataSource.xml--> ...........
<!--dbSource.xml-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db"/>
.......
</bean>
<import resourse="spring-dataSource.xml"/>@Profile
?可以解決不同環(huán)境的切換需求,例如開(kāi)發(fā)環(huán)境和測(cè)試環(huán)境不同,我們來(lái)看代碼操作。
@Component
public class ProfileDataSource{
//開(kāi)發(fā)環(huán)境
@Bean(name = "devDataSource")
@Profile("dev")
public DataSource getDevDataSource(){
......
}
//測(cè)試環(huán)境
@Bean(name = "testDataSource")
@Profile("test")
public DataSource getTestDataSource(){
......
}
}當(dāng)啟動(dòng)Java配置Profile時(shí),可以發(fā)現(xiàn)兩個(gè)Bean并不會(huì)加載到IOC容器中,需要自行激活Profie。我們可以使用JVM啟動(dòng)目錄或在集成測(cè)試環(huán)境中使用@ActiveProfiles進(jìn)行定義
//使用@ActiveProfiles激活Profie
@RunWith(SpringJunit4ClassRunner.class)
@ContextConfiguration(classes=ProfileTest.class)
@ActiveProfiles("dev") //激活開(kāi)發(fā)環(huán)境的Profile
public class ProfileTest{
}//使用JVM參數(shù)激活Profie JAVA_OPTS="-Dspring.profiles.active=test"
@PropertySource
可以使用注解@PropertySource來(lái)加載屬性文件(properties)。
# dataSource.properties jdbc.database.driver=com.mysql.cj.jdbc.Driver jdbc.database.url=jdbc:mysql://localhost:3306/db .......
@Configuration
@PropertySource(value = {"classpath:dataSource.properties"},{ignoreResourceNotFound=true})
public class ApplicationConfig{
}ignoreResourceNotFound=true,如果找不到該屬性文件則忽略它。
到此這篇關(guān)于Spring IOC 常用注解與使用的文章就介紹到這了,更多相關(guān)Spring IOC 注解與使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳談Java中Object類中的方法以及finalize函數(shù)作用
下面小編就為大家?guī)?lái)一篇詳談Java中Object類中的方法以及finalize函數(shù)作用。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
SpringBoot實(shí)現(xiàn)文件在線預(yù)覽功能的全過(guò)程
我們開(kāi)發(fā)業(yè)務(wù)系統(tǒng)的時(shí)候,經(jīng)常有那種文檔文件在線預(yù)覽的需求,下面這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)現(xiàn)文件在線預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下2021-11-11
mybatis TypeHandler注入spring的依賴方式
這篇文章主要介紹了mybatis TypeHandler注入spring的依賴方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
使用jackson實(shí)現(xiàn)對(duì)象json之間的相互轉(zhuǎn)換(spring boot)
這篇文章主要介紹了使用jackson實(shí)現(xiàn)對(duì)象json之間的相互轉(zhuǎn)換(spring boot),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Java構(gòu)造方法 super 及自定義異常throw合集詳解用法
異常是程序中的一些錯(cuò)誤,但不是所有錯(cuò)誤都是異常,且錯(cuò)誤有時(shí)候是可以避免的,super可以理解為是指向自己超(父)類對(duì)象的一個(gè)指針,而這個(gè)超類指的是離自己最近的一個(gè)父類,構(gòu)造器也叫構(gòu)造方法、構(gòu)造函數(shù),是一種特殊類型的方法,負(fù)責(zé)類中成員變量(域)的初始化2021-10-10
Java的動(dòng)態(tài)代理模式之Cglib代理詳解
這篇文章主要介紹了Java的動(dòng)態(tài)代理模式之Cglib代理詳解,Cglib代理也叫作?子類代理,它是在內(nèi)存中構(gòu)建一個(gè)子類對(duì)象從而實(shí)現(xiàn)對(duì)目標(biāo)對(duì)象功能擴(kuò)展,?有些書(shū)也將Cglib代理歸屬到動(dòng)態(tài)代理,需要的朋友可以參考下2023-11-11
Spring Boot 3.4.0 結(jié)合 Mybatis-plus 實(shí)
本文詳細(xì)介紹了在 Spring Boot 3.4.0 項(xiàng)目中結(jié)合 Mybatis-plus 實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的完整方案,通過(guò)自定義注解和AOP切面,我們可以優(yōu)雅地實(shí)現(xiàn)方法級(jí)別的數(shù)據(jù)源切換,滿足多數(shù)據(jù)源場(chǎng)景下的各種需求,感興趣的朋友一起看看吧2025-04-04

