zoukankan      html  css  js  c++  java
  • Spring学习(16)--- 基于Java类的配置Bean 之 基于泛型的自动装配(spring4新增)

    例子:

    定义泛型Store

    package javabased;
    
    public interface Store<T> {
    
    }
    

     两个实现类StringStore,IntegerStore

    package javabased;
    
    public class IntegerStore implements Store<Integer> {
    
    }
    
    package javabased;
    
    public class StringStore implements Store<String> {
    	
    }
    

     java config实现bean配置

    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配置:

    <?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>
    

     单元测试:

    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

  • 相关阅读:
    setAnimationTransition:forView:cache: 运行动画时背景色问题
    架构师速成4.6-软技能和硬技能
    Java获取某年某周的第一天
    openssl之BIO系列之12---文件描写叙述符(fd)类型BIO
    centos 使用 CP 命令 不提示 覆盖
    [3 Jun 2015 ~ 9 Jun 2015] Deep Learning in arxiv
    P1314 聪明的质监员
    P2858 [USACO06FEB]奶牛零食Treats for the Cows
    1163 访问艺术馆
    P1352 没有上司的舞会
  • 原文地址:https://www.cnblogs.com/JsonShare/p/4629511.html
Copyright © 2011-2022 走看看