亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring?IOC?常用注解與使用實例詳解

 更新時間:2022年06月06日 08:32:21   作者:Aircoinst  
這篇文章主要介紹了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)文章

最新評論