基于泛型自动装配:

package com.imooc.beanannotation.javabased;import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Scope;@Configuration
@ImportResource("classpath:config.xml")
public class StoreConfig {/*@Value("${url}")private String url;@Value("${jdbc.username}")private String username;@Value("${password}")private String password;@Beanpublic MyDriverManager myDriverManager(){return new MyDriverManager(url,username,password);}*//*@Bean(name="stringStore",initMethod="init",destroyMethod="destroy")public Store stringStore(){return new StringStore();}*//*@Bean(name="stringStore")@Scope(value="prototype")public Store stringStore(){return new StringStore();}*/@Autowiredprivate Store<String> s1;@Autowiredprivate Store<Integer> s2;@Beanpublic StringStore stringStore(){return new StringStore();}@Beanpublic IntegerStore integerStore(){return new IntegerStore();}@Bean(name="stringStoreTest")public Store stringStoreTest(){System.out.println("s1:"+s1.getClass().getName());System.out.println("s2:"+s2.getClass().getName());return new StringStore();
}
}
package com.imooc.test.beanannotation;import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;import com.imooc.beanannotation.javabased.MyDriverManager;
import com.imooc.beanannotation.javabased.Store;
import com.imooc.ioc.interfaces.UnitTestBase;@RunWith(BlockJUnit4ClassRunner.class)
public class TestJavabased extends UnitTestBase {public TestJavabased(){super("classpath*:spring-beanannotation.xml");
}@Testpublic void test(){Store store=super.getBean("stringStore");System.out.println(store.getClass().getName());;}@Testpublic void testMyDriverManager(){MyDriverManager manager=super.getBean("myDriverManager");System.out.println(manager.getClass().getName());;}@Testpublic void testScope(){Store store=super.getBean("stringStore");System.out.println(store.hashCode());store=super.getBean("stringStore");System.out.println(store.hashCode());}@Testpublic void testG(){Store store=super.getBean("stringStoreTest");
}
}
package com.imooc.beanannotation.javabased;public class StringStore implements Store<String> {public void init(){System.out.println("This is init.");}public void destroy(){System.out.println("This is destroy.");}
}
package com.imooc.beanannotation.javabased;public interface Store<T> {}
package com.imooc.beanannotation.javabased;public class IntegerStore implements Store<Integer> {}

运行结果:

基于集合方式自动装配:

package com.imooc.beanannotation.javabased;import org.springframework.stereotype.Component;@Component
public class IntegerStore implements Store<Integer> {}
package com.imooc.beanannotation.javabased;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class Com<T> {@Autowiredprivate List<Store<Integer>> list;public void say(){if(null!=list){for(Store<Integer> bean:list){System.out.println(bean.getClass().getName());//getClass().getName()是用来返回Class对象所代表的具体对象的名称。}}else{System.out.println("List<Store<Integer>> list is null");}}}
package com.imooc.beanannotation.javabased;public interface Store<T> {}
package com.imooc.test.beanannotation;import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;import com.imooc.beanannotation.javabased.Com;
import com.imooc.beanannotation.javabased.MyDriverManager;
import com.imooc.beanannotation.javabased.Store;
import com.imooc.ioc.interfaces.UnitTestBase;@RunWith(BlockJUnit4ClassRunner.class)
public class TestJavabased extends UnitTestBase {public TestJavabased(){super("classpath*:spring-beanannotation.xml");
}@Testpublic void test(){Store store=super.getBean("stringStore");System.out.println(store.getClass().getName());;}@Testpublic void testMyDriverManager(){MyDriverManager manager=super.getBean("myDriverManager");System.out.println(manager.getClass().getName());;}@Testpublic void testScope(){Store store=super.getBean("stringStore");System.out.println(store.hashCode());store=super.getBean("stringStore");System.out.println(store.hashCode());}@Testpublic void testG(){Com com=super.getBean("com");com.say();
}
}

运行结果:

自定义qualifier

观看慕课moocer老师视频Spring Bean装配之Autowire注解说明-7相关推荐

  1. 观看慕课moocer老师视频Spring Bean装配之Autowire注解说明-2

    数组及Map的自动注入: 1.@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把 ...

  2. 观看慕课moocer老师视频Spring Bean装配之Autowire注解说明-6

    默认@Bean是单例的,使用@Scope指定它的范围: package com.imooc.beanannotation.javabased;//import org.springframework. ...

  3. 观看慕课moocer老师视频Spring Bean装配之Autowire注解说明-5

    使用@ImportResource和@Value注解进行资源文件读取 package com.imooc.beanannotation.javabased;import org.springframe ...

  4. 观看慕课moocer老师视频Spring Bean装配之Autowire注解说明-3

    @Qualifier @Qualifier可缩小注解范围,也可指定唯一的bean. package com.imooc.beanannotation.multibean;import java.uti ...

  5. 观看慕课moocer老师视频Spring Bean装配之Autowire注解说明-8

    注解在set方法上: package com.imooc.beanannotation.jsr;import javax.annotation.Resource;import org.springfr ...

  6. 观看慕课moocer老师视频Spring Bean装配之Autowire注解说明-1

    @Autowired(放在成员变量上) package com.imooc.test.beanannotation;import org.junit.Test; import org.junit.ru ...

  7. 观看慕课moocer老师视频配置切面aspcet及配置切入点Pointcut

    配置切面: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  8. 观看慕课moocer老师视频AOP基本概念及特点

  9. Spring Bean装配(上)

    Bean:在spring的IOC里面,把配置到IOC容器里面的实体或者是对象都称为Bean Bean配置项 Bean的作用域 Bean的生命周期 Bean的自动装配 Resources&Res ...

最新文章

  1. mysql ibd文件还原_Mysql 通过ibd文件恢复数据
  2. 负数分解质因数java_Java经典案例之-“分解质因数”
  3. (46)分析 INT 0x2E 和 sysenter
  4. 每天学一点儿shell:正则表达式
  5. Redis概述、数据类型
  6. 小程序直播间报错:{“errmsg“:“the current room status does not allow this operation rid:“,“errcode“:300023}
  7. ffmpeg参数中文详细解释
  8. Unity(四):使用场景Ⅰ:建立类型映射
  9. 源码 解析_最详细集合源码解析之ArrayList集合源码解析
  10. css3兼容IE8的方案 各个ie的hack
  11. 毕设题目:Matlab表盘识别
  12. 二级 c语言真题及答案,3月计算机二级C语言真题及答案(完整版)
  13. linux安装时找不到硬盘分区,在安装linux时出现找不到硬盘如何解决
  14. 【mysql表查询】某门课程的前三名(包含并列)
  15. 视频类APP体验报告
  16. php汉字占几个字节,php一个汉字几个字节
  17. HTML中动态图片切换JQuery实现
  18. 非线性规划之分式规划
  19. 动力环境监控系统(动环监控功能特点)
  20. 《改变世界的机器》作者丹尼尔·T·琼斯获 ILSSI 终身成就奖

热门文章

  1. mac 卸载掉的还在启动台_一口气了解Mac触控板的使用小技巧,是时候扔掉鼠标了!...
  2. 【Java】IDEA开发GUI应用程序出现汉字变方框问题的解决方法
  3. 60级高阶督军套装属性_《魔兽世界》【60年代】元帅/督军职业套装的浅谈
  4. 一个改变开源软件的人:比尔盖茨
  5. 【java小练习】#for循环运用#打印*三角阵列
  6. 正则十八式-第二式:控鹤擒龙
  7. 数学之美:导数和极值的关系
  8. 【STM32H7】第8章 学习USB协议栈前要了解的基础知识
  9. 计算机专业过年回家,回家过年的温暖唯美句子 描写过年回家的优美句子
  10. Spark - 小实践(6)计算手机在基站停留时间