简介 目录 评价 推荐
  • 程序员慕虎 2020-09-09

    基于泛型的自动装配(Spring4新增的内容,和SPI使用方式相似,只不过Spring提供了更简便的使用方式。API:应用编程接口。SPI:针对服务框架开发或者一些工具的基础使用的特殊类型接口。)

    基于依赖对象泛型的自动装配案例

    步骤1:

    public class IntegerStore {

    }

    public class StringStore{

    @Autowired

    private IntegerStore s2;

    public void print(){

    System.out.println("s2:  "+s2);

    }

    }

    步骤2:

    @Configuration

    public class StoreConfig {

    @Bean

    public StringStore stringStore(){

    return new StringStore();

    }

    @Bean

    public IntegerStore integerStore(){

    return new IntegerStore();

    }

    }

    步骤4:测试

    @Test

    public void testStore(){

    ApplicationContext ac=new AnnotationConfigApplicationContext(StoreConfig.class);

    StringStore ss=ac.getBean("stringStore",StringStore.class);

    ss.print();

    }

    结果:(如果不加@Autowired注解,s2:为空)

    s2:  IntegerStore@3323f4c5

    基于集合泛型的自动装配(注意:不能使用@Autowired注解,要使用@Resource注解,并且使用@Resource(name="名字1"),去匹配@Bean(name="名字1")。

    步骤1:


    public class IntegerStore {

    }

    public class StringStore {

    public StringStore() {

    System.out.println("无参构造方法执行了");

    }

    private List<IntegerStore> list;

    @Resource(name = "ll")

    public void setList(List<IntegerStore> list) {

    this.list = list;

    }

    public void print() {

    for (IntegerStore store : list) {

    System.out.println(store);

    }

    }

    }

    步骤2:

    @Configuration

    public class StoreConfig {

    @Bean(name = "ll")

    public List<IntegerStore> ll() {

    List<IntegerStore> lis = new ArrayList<IntegerStore>();

    lis.add(new IntegerStore());

    lis.add(new IntegerStore());

    lis.add(new IntegerStore());

    return lis;

    }


    @Bean(name = "stringStore")

    public StringStore stringStore() {

    return new StringStore();

    }

    }

    测试:


    @Test

    public void testStore() {

    ApplicationContext ac = new AnnotationConfigApplicationContext(StoreConfig.class);

    StringStore ss = ac.getBean("stringStore", StringStore.class);

    ss.print();

    }

    }

    结果:


    无参构造方法执行了

    IntegerStore@2b2c8b14

    IntegerStore@795ee430

    IntegerStore@44d74990

    Autowired扩展——自定义qualifier注解(自定义自动化注入配置)

    https://img1.sycdn.imooc.com/5ce8c92a00015b4211260626.jpg


    0赞 · 0采集
  • Leyad 2020-06-17

    http://img3.mukewang.com/5ee9bf260001394f13660768.jpg

    这里最后bean返回Store类型必定是StringStore类型吧?只是在该方法中显示了S1和S2的类名称,而返回的这个实例类id为“stringStoreTest”。这里S1和S2都已经自动装载了,那为什么还要有两个方法提供这两种类型的实体类的获取方法

    这里的两个属性上的标注无法执行注入,必须由下方的的bean注解的方法赋值,之所以之前报错,是因为变量stringStore存在两个赋值方法,所以报错。(构造方法是以类型寻找)

    0赞 · 0采集
  • 慕莱坞9426565 2020-04-06

    CustomAutowireConfigurer

    是BeanFactoryPostProcessor的子类,通过它可以注册自己的Qualifier注解类型(即使没有使用Spring的@Qualifier注解)


    截图
    0赞 · 0采集
  • 慕莱坞9426565 2020-04-06

    基于泛型的自动装配

    截图
    0赞 · 0采集
  • 慕粉42788080 2020-03-07

    自定义自动装配配置类CustomAutowireConfigurer特殊场景可能会需要自己去自定义

    截图
    0赞 · 0采集
  • 慕粉42788080 2020-03-07

    基于泛型的自动装配

    截图
    0赞 · 0采集
  • 大鹏111 2019-05-25

    基于泛型的自动装配(Spring4新增的内容,和SPI使用方式相似,只不过Spring提供了更简便的使用方式。API:应用编程接口。SPI:针对服务框架开发或者一些工具的基础使用的特殊类型接口。)

    基于依赖对象泛型的自动装配案例

    步骤1:

    public class IntegerStore {

    }

    public class StringStore{

    @Autowired

    private IntegerStore s2;

    public void print(){

    System.out.println("s2:  "+s2);

    }

    }

    步骤2:

    @Configuration

    public class StoreConfig {

    @Bean

    public StringStore stringStore(){

    return new StringStore();

    }

    @Bean

    public IntegerStore integerStore(){

    return new IntegerStore();

    }

    }

    步骤4:测试

    @Test

    public void testStore(){

    ApplicationContext ac=new AnnotationConfigApplicationContext(StoreConfig.class);

    StringStore ss=ac.getBean("stringStore",StringStore.class);

    ss.print();

    }

    结果:(如果不加@Autowired注解,s2:为空)

    s2:  IntegerStore@3323f4c5

    基于集合泛型的自动装配(注意:不能使用@Autowired注解,要使用@Resource注解,并且使用@Resource(name="名字1"),去匹配@Bean(name="名字1")。

    步骤1:

    public class IntegerStore {

    }

    public class StringStore {

    public StringStore() {

    System.out.println("无参构造方法执行了");

    }

    private List<IntegerStore> list;

    @Resource(name = "ll")

    public void setList(List<IntegerStore> list) {

    this.list = list;

    }

    public void print() {

    for (IntegerStore store : list) {

    System.out.println(store);

    }

    }

    }

    步骤2:

    @Configuration

    public class StoreConfig {

    @Bean(name = "ll")

    public List<IntegerStore> ll() {

    List<IntegerStore> lis = new ArrayList<IntegerStore>();

    lis.add(new IntegerStore());

    lis.add(new IntegerStore());

    lis.add(new IntegerStore());

    return lis;

    }


    @Bean(name = "stringStore")

    public StringStore stringStore() {

    return new StringStore();

    }

    }

    测试:

    @Test

    public void testStore() {

    ApplicationContext ac = new AnnotationConfigApplicationContext(StoreConfig.class);

    StringStore ss = ac.getBean("stringStore", StringStore.class);

    ss.print();

    }

    }

    结果:

    无参构造方法执行了

    IntegerStore@2b2c8b14

    IntegerStore@795ee430

    IntegerStore@44d74990

    Autowired扩展——自定义qualifier注解(自定义自动化注入配置)

    https://img1.mukewang.com/5ce8c92a00015b4211260626.jpg






    0赞 · 6采集
  • 爱吃娃娃菜的阿菜 2019-04-22

    CustomAutowireConfigurer

    截图
    0赞 · 0采集
  • 爱吃娃娃菜的阿菜 2019-04-22

    基于泛型的自动装配

    截图
    0赞 · 0采集
  • 秋天的落叶1 2019-04-20

    @TestJavaBased.java

     

    @RunWith(BlockJUnit4ClassRunner.class)

    public   class TestJavaBased extends UnitTestBase{

          public TestJavaBased(){

                 super("classpath*:spring-beanannotation.xml");

          }

    @Test

          public void testG(){

                 StringStore   store=super.getBean("stringStoreTest");

          }

    }

    @Store.java

     

    package   com.imooc.annotation.javabased;

    public interface Store<T> {

    }

    @StringStore.java

     

    package   com.imooc.annotation.javabased;

    public class StringStore implements   Store<String> {

    }

    @IntegerStore.java

     

    package   com.imooc.annotation.javabased;

    public class IntegerStore implements   Store<Integer> {

    }

    @StoreConfig.java

     

    package   com.imooc.annotation.javabased;

    @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="stringStoreTest")

           public   Store stringStoreTest(){//相当于xml:<bean   id="stringStoreTest" class="com.imooc.annotation.javabased.Store"   ></bean>

                  syso("s1:"+s1.getClass().getName());

                  syso("s2:"+s2.getClass().getName());

                  return   new IntegerStore();

           }

    }

    @spring-beanannotation.xml

     

    <context:component-scan   base-package="com.imooc.annotation" ></context:component>

     CustomAutowireConfigurer(自定义qualifier注解类型,不常用)

    (1)CustomAutowireConfigurer是BeanFactoryPostProcessor的子类,通过它可以注册自己的qualifier注解类型(即使没有使用Spring的@Qualifier 注解)

    (2)该AutowireCandidateResolver决定自动装配的候选者:

    a)每个bean定义autowired-candidate值

    b)任何<bean/>中的default-autowire-candidates

    c)@Qualifier注解及使用CustomAutowireConfigurer的自定义类型


    0赞 · 0采集
  • 小啦同学 2019-04-16

    Spring支持基于泛型的自动装配

    截图
    0赞 · 0采集
  • hanhua 2019-02-20

    基于泛型的自动装配

    截图
    0赞 · 0采集
  • 韩DD 2019-02-18

    CustomAutowireConfigurer 自定义qualifier注解

    截图
    0赞 · 0采集
  • 韩DD 2019-02-18

    基于泛型的自动装配


    截图
    0赞 · 0采集
  • ellydebug 2018-11-20

    TODO: java SPI的使用,待学习!!

    截图
    0赞 · 0采集
  • scrsh 2018-10-25

    CustomAutowireConfigurer

    截图
    0赞 · 0采集
  • scrsh 2018-10-25

    基于泛型的自动装配

    截图
    0赞 · 1采集
  • 慕圣6698645 2018-10-04

    这节课没全理解

    截图
    0赞 · 0采集
  • IceCoke 2018-09-18

    这个例子autowire了两个同样类型的未指定名称,失败了

    截图
    0赞 · 0采集
  • IceCoke 2018-09-18

    泛型的装配

    截图
    0赞 · 0采集
  • qq_袮D影孑_03909390 2018-09-15

    CustomAutowireConfigurer

    截图
    0赞 · 0采集
  • 慕圣6698645 2018-08-13

    CustomAutowireConfigurer

    截图
    0赞 · 0采集
  • 慕无忌6134027 2018-07-25

    11111111

    截图
    0赞 · 0采集
  • 猪丶猪侠 2018-06-22

    public double number(int a) {

        return a;

    }

    返回值类型指的是public上的double类型,而不是a的类型。


    0赞 · 0采集
  • 慕粉7586221 2018-05-23

    CustomAutowireConfigurer:

    截图
    0赞 · 0采集
  • 我是猫_夏 2018-04-10

    暂时看不懂。。。等待中。。。

    0赞 · 0采集
  • 深山老龟 2018-03-31

    2

    截图
    0赞 · 0采集
  • 深山老龟 2018-03-31

    基于泛型的自动装配

    截图
    0赞 · 0采集
  • Chris_Lee0710 2018-03-17
    基于泛型的set自定义注解及CustomerAutowireConfigurer(自定义qualifier)
    截图
    0赞 · 0采集
  • kingdompeak 2018-03-08
    自定义注解的扩展
    截图
    0赞 · 0采集
数据加载中...
开始学习 免费