Spring?IOC?常用注解與使用實例詳解
@Component
注解@component代表spring ioc 會把這個類掃描生成Bean實例
@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后,這個字段需要按類型來進(jìn)行注入。
@Component
public class RoleImpl_1 implements RoleServer{
@Autowired
private Role role = null;
public .....
}@Qualifier
?如果一個接口被兩次實現(xiàn),則使用@Autowired注解來進(jìn)行該接口注入會產(chǎn)生異常,因為@Autowired無法確定要使用的是哪一個實現(xiàn)類??梢允褂聾Qualifier注解來進(jìn)行歧義消除。
@Component
public class RoleController{
@Autowired
@Qualifier("roleImple_2")
private RoleServer server = null;
public .....
}@Bean
?在注解都都是通過@component來進(jìn)行裝配Bean,但是@Component只能注解在類上,無法注解到方法上。而注解@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配置文件來進(jìn)行配置,我們就無法使用注解@Bean來進(jìn)行裝配。這時注解@ImportResource可以進(jìn)行混合裝配(將第三方的xml引入進(jìn)來進(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中來
public class ApplicationConfig{
}@Component
public class RoleController{
@Autowired
private DataSource dataSource = null;
public .....
}如果有多個xml文件,我們都想引用進(jìn)來,可以在dbSource.xml配置文件中使用import元素來加載它
<!--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)境的切換需求,例如開發(fā)環(huán)境和測試環(huán)境不同,我們來看代碼操作。
@Component
public class ProfileDataSource{
//開發(fā)環(huán)境
@Bean(name = "devDataSource")
@Profile("dev")
public DataSource getDevDataSource(){
......
}
//測試環(huán)境
@Bean(name = "testDataSource")
@Profile("test")
public DataSource getTestDataSource(){
......
}
}當(dāng)啟動Java配置Profile時,可以發(fā)現(xiàn)兩個Bean并不會加載到IOC容器中,需要自行激活Profie。我們可以使用JVM啟動目錄或在集成測試環(huán)境中使用@ActiveProfiles進(jìn)行定義
//使用@ActiveProfiles激活Profie
@RunWith(SpringJunit4ClassRunner.class)
@ContextConfiguration(classes=ProfileTest.class)
@ActiveProfiles("dev") //激活開發(fā)環(huán)境的Profile
public class ProfileTest{
}//使用JVM參數(shù)激活Profie JAVA_OPTS="-Dspring.profiles.active=test"
@PropertySource
可以使用注解@PropertySource來加載屬性文件(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳談Java中Object類中的方法以及finalize函數(shù)作用
下面小編就為大家?guī)硪黄斦凧ava中Object類中的方法以及finalize函數(shù)作用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
SpringBoot實現(xiàn)文件在線預(yù)覽功能的全過程
我們開發(fā)業(yè)務(wù)系統(tǒng)的時候,經(jīng)常有那種文檔文件在線預(yù)覽的需求,下面這篇文章主要給大家介紹了關(guān)于SpringBoot實現(xiàn)文件在線預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下2021-11-11
mybatis TypeHandler注入spring的依賴方式
這篇文章主要介紹了mybatis TypeHandler注入spring的依賴方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
使用jackson實現(xiàn)對象json之間的相互轉(zhuǎn)換(spring boot)
這篇文章主要介紹了使用jackson實現(xiàn)對象json之間的相互轉(zhuǎn)換(spring boot),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Java構(gòu)造方法 super 及自定義異常throw合集詳解用法
異常是程序中的一些錯誤,但不是所有錯誤都是異常,且錯誤有時候是可以避免的,super可以理解為是指向自己超(父)類對象的一個指針,而這個超類指的是離自己最近的一個父類,構(gòu)造器也叫構(gòu)造方法、構(gòu)造函數(shù),是一種特殊類型的方法,負(fù)責(zé)類中成員變量(域)的初始化2021-10-10
Spring Boot 3.4.0 結(jié)合 Mybatis-plus 實
本文詳細(xì)介紹了在 Spring Boot 3.4.0 項目中結(jié)合 Mybatis-plus 實現(xiàn)動態(tài)數(shù)據(jù)源切換的完整方案,通過自定義注解和AOP切面,我們可以優(yōu)雅地實現(xiàn)方法級別的數(shù)據(jù)源切換,滿足多數(shù)據(jù)源場景下的各種需求,感興趣的朋友一起看看吧2025-04-04

