1. 程式人生 > >Spring入門(基於Java的容器註解之@ImportResource和@Value)

Spring入門(基於Java的容器註解之@ImportResource和@Value)

如何使用@ImportResource和@Value進行資原始檔讀取。

首先看個例子,使用beans來定義一個配置

<beans>
    <context:annotation-config/>
    <context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>

    <bean class="com.acme.AppConfig"/>

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
>
<property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> </beans>

這個使用property-placeholder,在使用它的時候,它會對應一個資原始檔,對應一個location,location對應一個資原始檔的存放位置。<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>

這個這句話的作用是載入properties資原始檔。properties檔案是一種key、value的形式的檔案。
載入了這個檔案之後 ,在當前的檔案中,可以通過 ${} 的方式,{}裡邊是properties檔案中的key,通過這種方式來引用properties檔案中的內容。比如說常用的資料庫的配置方式。
我們的配置資訊通常會寫在資原始檔中,然後通過property-placeholder這種方式去把它載入進來。然後在當前的配置檔案中去引用它。比如說這裡指定了一種資料來源DriverManagerDataSource,然後可以指定這種資料來源的url、username和password,它們的來源就是這個資原始檔。

以上是使用xml檔案配置的方式,那麼如果使用註解要怎麼做?

@Configuration
@ImportResource("classpath:/com/acme/properties-config.xml")
public class AppConfig{

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource dataSource() {
        return new DriverManagerDataSource(url,username,password);
    }
}

仍然使用@Configuration,把這個類作為一個配置來使用。@ImportResource就是引入一個這種資源,然後資源對應一個xml檔案,和前個例子差不多,xml檔案也會對應一個property-placeholder。
用@Value這個註解從資原始檔中取出它的key賦值給成員變數,包括username、password等。然後再使用@Bean這個註解去建立DriverManagerDataSource的一個物件,也就是和第一種的方式一樣,去建立這個Bean的物件,同時把url、username、password傳入DriverManagerDataSource的構造器。
這樣就達到了從資原始檔中去載入資原始檔的配置,並應用到bean的建立中。

載入資原始檔的例子
建立一個類MyDriverManager,其構造器有三個引數:

public class MyDriverManager {

    public MyDriverManager(String url, String userName, String password) {
        System.out.println("url : " + url);
        System.out.println("userName: " + userName);
        System.out.println("password: " + password);
    }
}

在前一個例子中的StoreConfig類中加上@ImportResource(“classpath:config.xml”)。

@Configuration
@ImportResource("classpath:config.xml")
public class StoreConfig {
    //...
}

然後看一下這個config.xml檔案。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" >

    <context:property-placeholder location="classpath:/config.properties"/>

</beans>

裡邊使用property-placeholder去指定一個資原始檔的location,然後看一下這個資原始檔的內容。
這裡寫圖片描述

之後再看看在StoreConfig類中要如何配置它

@Value("${url}")
private String url;

@Value("${jdbc.username}")
private String username;

@Value("${password}")
private String password;

@Bean
public MyDriverManager myDriverManager() {
    return new MyDriverManager(url, username, password);
}

要注意username那裡不可以直接寫username,這樣會取到當前使用者的,通常會寫為jdbc.username。這個類中首先宣告成員變數,然後用@Value註解來得到。之後建立一個Bean:MyDriverManager,然後返回一個MyDriverManager的物件,給構造器傳入引數。

單元測試類的寫法如下:

@Test
public void testMyDriverManager() {
    MyDriverManager manager = super.getBean("myDriverManager");
    System.out.println(manager.getClass().getName());
}

大體的寫法就是這樣。