Spring IOC使用(3) 基于注解管理bean
来源:互联网 发布:淘宝阿里旺旺打不开 编辑:程序博客网 时间:2024/05/16 12:40
1. context:component-scan标签
<!-- 用于扫描类,并注册bean --><!-- base-package属性,可以扫描一些包(包括子包)name-generator属性,自定义命名策略,实现BeanNameGenerator接口的类,必须包含无参数构造器scope-resolver属性,自定义scope策略,实现ScopeMetadataResolver接口--><context:component-scan base-package="com.emmairving.spring.annotation"> <!-- type有五种:annotation, assignable, aspectj, regex, custom --> <context:include-filter type="regex" expression="com.emmairving.spring.annotation.*"/> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> <context:exclude-filter type="regex" expression="com.emmairving.spring.annotation.son.*"/></context:component-scan><!-- 有了<context:component-scan>之后,基本都不使用以下配置 --><context:annotation-config/>
2. Autowired注解
//可以指定bean的id@Service("master")public class Master { //Autowired注解默认是通过类型来自动装载,如果需要指定名称,可以与Qualifier注解配合使用 //Autowired注解的required默认为true,如果没有能注入对应的bean就会报错。设置为false之后,就不会报错。 //Autowired可以修饰构造器、方法 @Autowired(required=false) //Qualifier注解可以指定要注入bean的名称 @Qualifier("slave") private Slave slave; //可以用Autowired注解中所周知的解析依赖接口,如ApplicationContext @Autowired private ApplicationContext ctx; //如果有多个符合条件的bean,可以用数组、集合类型等 //如果不是集合类型,也不会报错,会选择众多符合条件bean中的一个注入 @Autowired private Slave[] slaves; public void test() { Member member = ctx.getBean("member1",Member.class); System.out.println(member); System.out.println(slaves.length); System.out.println("My Slave "+slave); }}@Controller("slave")public class Slave { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Slave() { name="lucy"; } @Override public String toString() { return "Slave [name=" + name + "]"; }}
3. 参考资料
- 慕课网与极客学院的视频课程
0 0
- Spring IOC使用(3) 基于注解管理bean
- Spring bean管理【基于注解】
- Spring IOC--Bean的装配(使用注解定义Bean)
- Spring IOC--Bean的装配(使用注解定义Bean)
- 【Spring】IOC之基于注解的配置bean(上)
- Spring IOC 基于注解的使用
- 使用Java注解进行Spring bean管理
- Spring学习(九)使用ioc注解方式配置bean
- Spring学习(九)使用ioc注解方式配置bean
- 04-Spring-使用ioc注解方式配置bean
- Spring(3、基于注解装配Bean)
- Spring框架(3)---IOC装配Bean(注解方式)
- Spring框架(3)---IOC装配Bean(注解方式)
- Spring框架(3)---IOC装配Bean(注解方式)
- Spring使用IOC注解
- Spring-IOC装配Bean(注解方式)
- Spring--IoC--基于注解的DI-基本使用
- spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- MySQL (m-s)主从配置文档
- 1.zookeeper原理解析-数据存储之Zookeeper内存结构(zookeeper系列文章)
- [Android] Button 文字被截断解决方案
- JavaScript对数组的操作
- 【BZOJ1188】分裂游戏,博弈
- Spring IOC使用(3) 基于注解管理bean
- 字符串循环左移
- 线下模型的数据清洗建模
- Selenium学习11--精确比较网页截图图片
- ubuntu下解决sublime支持中文输入
- 为任意可滑动的View添加快速滑动,是的,任意。
- android屏幕适配理解及常见单位
- 进程间通信笔记(7)—SystemV信号量
- web.xml 常用标签