4-8 Spring Bean装配之基于Java的容器-基于泛型的自动装配

基于泛型的自动装配

  • 基于泛型的自动装配是spring4中新增的内容。

泛型自动装配例子:

在这里插入图片描述

public interface Store<T> {
}
public class IntegerStore implements Store<Integer> {

}
public class StringStore implements Store<String> {

}
package com.torey.beanannotation.javabased;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName:StoreConfig
 * @Description:
 * @author: Torey
 */
@Configuration
public class StoreConfig {
    @Autowired
    private Store<String> s1;
    @Autowired
    private Store<Integer> s2;

    @Bean
    public IntegerStore s2(){
        return new IntegerStore();
    }
    @Bean
    public StringStore s1(){
        StringStore ss=new StringStore();
        System.out.println("StringStore:"+ss.hashCode());
        return ss;
    }
    @Bean
    public Store stringStoreTest(){
        System.out.println("s1:" + s1.getClass().getName()+";s1.hashCode:"+s1.hashCode());
        System.out.println("s2:" + s2.getClass().getName());
        StringStore ss= new StringStore();
        System.out.println("stringStoreTest:"+ss.hashCode());
        return ss;

    }
}
package com.torey.springtest.base;

import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

/**
 * @ClassName:UnitTestBase
 * @Description:
 * @author: Torey
 */
public class UnitTestBase {
    public UnitTestBase(){}
    private String springXmlPath;
    private ClassPathXmlApplicationContext applicationContext;
    public UnitTestBase(String springXmlpath){
        this.springXmlPath=springXmlpath;
    }
    @Before
    public void before(){
        if (StringUtils.isEmpty(springXmlPath)) {
            springXmlPath="classpath*:spring-*.xml";
        }
        applicationContext= new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]"));
        applicationContext.start();
    }
    @After
   public void after(){
        applicationContext.destroy();
   }
   public Object getBean(String beanId){
       return applicationContext.getBean(beanId);
   }
}

package com.torey.springtest.bean;

import com.torey.beanannotation.javabased.Store;
import com.torey.springtest.base.UnitTestBase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

/**
 * @ClassName:TestResources
 * @Description:
 * @author: Torey
 */
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase {
    public TestBeanAnnotation(){
        super("classpath:spring-beanannotation.xml");
    }
  @Test
  public void testG(){
      Store store=(Store) super.getBean("stringStoreTest");
  }
}

在这里插入图片描述

Autowire扩展:自定义qualifier注解

CustomAutowireConfigurer

  • CustomAutowireConfigurer是BeanFactoryPostProcessor的子类,通过它可以注册自己的qualifier注解类型(即使没有使用Spring的@Qualifier注解)
 <bean id="customAutowireConfigurer" class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer">
        <property name="customQualifierTypes">
            <set>
                <value>example.CustomQualifier</value>
            </set>
        </property>
    </bean>
  • 该AutowireCandidateResolver决定自动装配的候选者:
    • 每个bean定义的autowire-candidate值
    • 任何< bean/ >中的default-autowire-candidates
    • @Qualifier注解及使用CustomAutowireConfigurer的自定义类型

根据慕课网 apollo_0001老师的spring课程 整理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值