欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?IOC?常用注解與使用實(shí)例詳解

 更新時(shí)間:2022年06月06日 08:32:21   作者:Aircoinst  
這篇文章主要介紹了Spring?IOC?常用注解與使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

@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è)字段需要按類型來進(jìn)行注入。

@Component
public class RoleImpl_1 implements RoleServer{
    @Autowired
    private Role role = null;
    
    public .....
}

@Qualifier

?如果一個(gè)接口被兩次實(shí)現(xiàn),則使用@Autowired注解來進(jìn)行該接口注入會(huì)產(chǎn)生異常,因?yàn)锧Autowired無法確定要使用的是哪一個(gè)實(shí)現(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)行裝配。這時(shí)注解@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 .....
}

如果有多個(gè)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)境和測(cè)試環(huán)境不同,我們來看代碼操作。

@Component
public class ProfileDataSource{
    //開發(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")  //激活開發(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • IDEA怎么生成UML類圖的實(shí)現(xiàn)

    IDEA怎么生成UML類圖的實(shí)現(xiàn)

    這篇文章主要介紹了IDEA怎么生成UML類圖的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 透徹理解Java中Synchronized(對(duì)象鎖)和Static Synchronized(類鎖)的區(qū)別

    透徹理解Java中Synchronized(對(duì)象鎖)和Static Synchronized(類鎖)的區(qū)別

    這篇文章主要介紹了Java中Synchronized(對(duì)象鎖)和Static Synchronized(類鎖)的區(qū)別,希望對(duì)大家有所幫助,一起跟隨小編過來看看吧
    2018-05-05
  • IDEA項(xiàng)目如何取消git版本管控并添加svn版本控制

    IDEA項(xiàng)目如何取消git版本管控并添加svn版本控制

    在公司內(nèi)部服務(wù)器環(huán)境下,將代碼倉庫從Gitee的Git遷移到SVN可以避免外部版本控制的風(fēng)險(xiǎn),遷移過程中,先刪除項(xiàng)目的.git文件夾,再通過Eclipse的設(shè)置界面刪除原Git配置并添加SVN配置,之后,將項(xiàng)目提交到SVN倉庫,確保使用ignore列表過濾不必要的文件
    2024-10-10
  • 關(guān)于線程池異步線程中再次獲取線程池資源的問題

    關(guān)于線程池異步線程中再次獲取線程池資源的問題

    這篇文章主要介紹了關(guān)于線程池異步線程中再次獲取線程池資源的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java接口和抽象類實(shí)現(xiàn)抽象和多態(tài)的方法示例

    Java接口和抽象類實(shí)現(xiàn)抽象和多態(tài)的方法示例

    接口和抽象類是 Java 中兩種實(shí)現(xiàn)抽象和多態(tài)的方法。它們之間有一些區(qū)別,但也有一些相似之處。這一節(jié)我們將通過詳細(xì)的例子來更深入地了解接口和抽象類
    2023-05-05
  • Mybatis?連接mysql數(shù)據(jù)庫底層運(yùn)行的原理分析

    Mybatis?連接mysql數(shù)據(jù)庫底層運(yùn)行的原理分析

    這篇文章主要介紹了Mybatis?連接mysql數(shù)據(jù)庫底層運(yùn)行的原理分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • mybatis if標(biāo)簽判斷不生效的解決方法

    mybatis if標(biāo)簽判斷不生效的解決方法

    這篇文章主要介紹了mybatis if標(biāo)簽判斷不生效的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • MybatisPlus中的insert操作詳解

    MybatisPlus中的insert操作詳解

    這篇文章主要介紹了MybatisPlus中的insert操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Spring Cloud Gateway 記錄請(qǐng)求應(yīng)答數(shù)據(jù)日志操作

    Spring Cloud Gateway 記錄請(qǐng)求應(yīng)答數(shù)據(jù)日志操作

    這篇文章主要介紹了Spring Cloud Gateway 記錄請(qǐng)求應(yīng)答數(shù)據(jù)日志操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Java獲取當(dāng)前時(shí)間戳案例詳解

    Java獲取當(dāng)前時(shí)間戳案例詳解

    這篇文章主要介紹了Java獲取當(dāng)前時(shí)間戳案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08

最新評(píng)論