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后,這個字段需要按類型來進行注入。
@Component public class RoleImpl_1 implements RoleServer{ @Autowired private Role role = null; public ..... }
@Qualifier
?如果一個接口被兩次實現(xiàn),則使用@Autowired注解來進行該接口注入會產(chǎn)生異常,因為@Autowired無法確定要使用的是哪一個實現(xiàn)類??梢允褂聾Qualifier注解來進行歧義消除。
@Component public class RoleController{ @Autowired @Qualifier("roleImple_2") private RoleServer server = null; public ..... }
@Bean
?在注解都都是通過@component來進行裝配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配置文件來進行配置,我們就無法使用注解@Bean來進行裝配。這時注解@ImportResource可以進行混合裝配(將第三方的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>
@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文件,我們都想引用進來,可以在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(){ ...... } }
當啟動Java配置Profile時,可以發(fā)現(xiàn)兩個Bean并不會加載到IOC容器中,需要自行激活Profie。我們可以使用JVM啟動目錄或在集成測試環(huán)境中使用@ActiveProfiles進行定義
//使用@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中Synchronized(對象鎖)和Static Synchronized(類鎖)的區(qū)別
這篇文章主要介紹了Java中Synchronized(對象鎖)和Static Synchronized(類鎖)的區(qū)別,希望對大家有所幫助,一起跟隨小編過來看看吧2018-05-05Java接口和抽象類實現(xiàn)抽象和多態(tài)的方法示例
接口和抽象類是 Java 中兩種實現(xiàn)抽象和多態(tài)的方法。它們之間有一些區(qū)別,但也有一些相似之處。這一節(jié)我們將通過詳細的例子來更深入地了解接口和抽象類2023-05-05Mybatis?連接mysql數(shù)據(jù)庫底層運行的原理分析
這篇文章主要介紹了Mybatis?連接mysql數(shù)據(jù)庫底層運行的原理分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Spring Cloud Gateway 記錄請求應(yīng)答數(shù)據(jù)日志操作
這篇文章主要介紹了Spring Cloud Gateway 記錄請求應(yīng)答數(shù)據(jù)日志操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12