Spring_5

Spring学习(16)--- 基于Java类的配置Bean 之 基于泛型的自动装配(spring4新增)

出处:http://www.cnblogs.com/JsonShare

例子:

定义泛型Store

1
2
3
4
5
package javabased;
 
public interface Store<T> {
 
}

 两个实现类StringStore,IntegerStore

1
2
3
4
5
package javabased;
 
public class IntegerStore implements Store<Integer> {
 
}

 

1
2
3
4
5
package javabased;
 
public class StringStore implements Store<String> {
     
}

 java config实现bean配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package javabased;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class StoreConfig {
     
    @Autowired
    private Store<String> s1;
     
    @Autowired
    private Store<Integer> s2;
     
    @Bean
    public StringStore stringStore() {
        return new StringStore();
    }
     
    @Bean
    public IntegerStore integerStore() {
        return new IntegerStore();
    }
     
    @Bean(name="test_generic")
    public String print(){    //测试
        System.out.println("s1 : "+s1.getClass().getName());
        System.out.println("s2 : "+s2.getClass().getName());
        return "";
    }
     
}

XML配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd">
         
        <context:component-scan base-package="javabased">
        </context:component-scan>
         
</beans>

 单元测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package javabased;
 
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 
public class UnitTest {
     
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beanannotation.xml"); 
        context.getBean("test_generic");
         
    }
}

 结果:

复制代码
2015-7-8 15:12:04 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@32bf7190: startup date [Wed Jul 08 15:12:04 CST 2015]; root of context hierarchy
2015-7-8 15:12:04 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-beanannotation.xml]
s1 : javabased.StringStore
s2 : javabased.IntegerStore
复制代码

 

也可参考(挺详细的):http://blog.csdn.net/yangxt/article/details/19970323

 

Spring学习(17)--- 三种装配Bean方式比较

 
 

基于XML配置

基于注解配置

基于Java类配置

Bean定义

<bean   id="..." class="..." />

@Component

衍生类@Repository

@Service   @Controller

@Configuration标注类,@Bean标注提供Bean方法

Bean名称

通过 id或name指定

@Component("person")

@Bean("person")

Bean注入

<property>   或者   通过p命名空间

@Autowired   按类型注入

@Qualifier按名称注入

在方法内部编写注入代码逻辑

生命过程、Bean作用范围

init-method

destroy-method

范围 scope属性

@PostConstruct   初始化

@PreDestroy   销毁

@Scope设置作用范围

在方法内部调用初始化方法

@Scope指定范围

适合场景

Bean来自第三方,使用其它命名空间

Bean的实现类由用户自己开发

实例化Bean的逻辑比较复杂

 
posted @ 2017-04-19 17:57  charles999  阅读(204)  评论(0编辑  收藏  举报