1. 程式人生 > >學習 Spring (十) 註解之 @Bean, @ImportResource, @Value

學習 Spring (十) 註解之 @Bean, @ImportResource, @Value

ner .data mys oot xtend ace data con 方法

Spring入門篇 學習筆記

@Bean

@Bean 標識一個用於配置和初始化一個由 Spring IoC 容器管理的新對象的方法,類似於 XML 配置文件的

可以在 Spring 的 @Configuration 註解的類中使用 @Bean 註解任何方法,在方法裏面創建對象返回

@Configuration
public class AppConfig{
    @Bean
    public MyService myService(){
        return new MyServiceImpl();
    }
}

示例

新建類:

public interface Store<T> {

}


public class StringStore implements Store<String> {
    
    public void init() {
        System.out.println("This is init.");
    }
    
    public void destroy() {
        System.out.println("This is destroy.");
    }
    
}

@Configuration
public class StoreConfig {

    @Bean(initMethod = "init", destroyMethod = "destroy")// 如果沒有指定 name,那麽name 為 方法的名稱
    public StringStore stringStore() {
        return new StringStore();
    }

}

添加測試:

@RunWith(BlockJUnit4ClassRunner.class)
public class TestJavabased extends UnitTestBase {
    
    public TestJavabased() {
        super("classpath*:spring-beanannotation.xml");
    }
    
    @Test
    public void test() {
        Store store = super.getBean("stringStore");
        System.out.println(store.getClass().getName());
    }
    
}

@Bean 默認是單例的,如果要改變作用域範圍,可以再添加 @Scope 註解

@ImportResource

<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.username}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
        
 </beans>

使用 @ImportResource 可以代替上面 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);
    }
}

示例:

添加類:

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);
    }

}

添加配置文件 config.properties:

jdbc.username=root
jdbc.password=root
jdbc.url=127.0.0.1

添加配置文件 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>

修改類 StoreConfig:

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

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

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

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

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


//  @Bean(initMethod = "init", destroyMethod = "destroy")// 如果沒有指定 name,那麽name 為 方法的名稱
//  public StringStore stringStore() {
//      return new StringStore();
//  }

}

添加測試:

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

源碼:learning-spring

學習 Spring (十) 註解之 @Bean, @ImportResource, @Value