Spring Bean 注入 2 注解篇
1. 自动装配注解
配置applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解使用示例 -->
<!-- 开启注解 -->
<context:annotation-config /> </beans>
注解使用示例
/** Value 注解可以使用SpEL,对基本数据类型完成注入 */
@Value("#{americanA.getName}")
public void setName(String name) {
this.name = name;
} /**
* Autowired表示使用byType自动装配该属性,他除了用在set方法也可以用在其他方法
* required属性表示可以允许找不到匹配的bean,而将该属性置为null
* Qualifier表示制定装配该属性的bean id为"catA"
* */
@Autowired(required = false)
@Qualifier("catA")
public void setPet(Pet pet) {
this.pet = pet;
} /** Inject和Named的使用与Autowired和Qualifier基本相同,只是没有required属性 */
@Inject
@Named("catA")
public void setPet(Pet pet) {
this.pet = pet;
}
2. 自动注册bean注解
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启自动检测bean注解 -->
<!-- base-package表示会扫描com.qunar.bean及其所有子包,对使用了自动注册注解的类进行自动注册 -->
<context:component-scan base-package="com.qunar.bean">
<!-- 过滤哪些类需要被注册,type有5种类型 -->
<!-- assignable表示扫描派生与expression指定类型的类 -->
<!-- annotation表示扫描使用了指定注解标注的类 -->
<!-- aspectj表示扫描与expression指定的AspectJ表达式匹配的类 -->
<!-- custom表示使用自定义的TypeFilter实现类 -->
<!-- regex表示过滤雷鸣宇expression指定的正则表达式相匹配的类 -->
<context:include-filter type="assignable"
expression="com.qunar.bean.People" />
<!-- 过滤哪些类不能被注册 -->
<context:exclude-filter type="assignable"
expression="com.qunar.bean.Cat" />
</context:component-scan> </beans>
/**
* @Component 标志该类为Spring组件
* @Controller 标志该类为Spring MVC Controller
* @Repository 标志该类为数据仓库
* @Service 标志该类为服务
*/
@Component("myCat") // 将该类自动注册为bean并将id命名为myCat
public class Cat extends Pet {
public Cat() {
} @Override
public String bark() {
return "miao";
}
}
Spring Bean 注入 2 注解篇的更多相关文章
- [spring]Bean注入——使用注解代替xml配置
使用注解编程,主要是为了替代xml文件,使开发更加快速. 一.使用注解前提: <?xml version="1.0" encoding="UTF-8"?& ...
- Spring依赖注入:注解注入总结
更多11 spring 依赖注入 注解 java 注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.S ...
- Spring依赖注入:注解注入
注解注入顾名思义就是通过注解来实现注入, Spring和注入相关的常见注解有Autowired.Resource.Qualifier.Service.Controller.Repository.Com ...
- Spring 反射注入+全注解注入
Spring IoC容器会先把所有的Bean都进行实例化,不管是要用到的火鼠用不到的,如果你想暂时不进行Bean的实例化,要用到属性lazy-init="true". Spring ...
- [spring]Bean注入——在XML中配置
Bean注入的方式有两种: 一.在XML中配置 属性注入 构造函数注入 工厂方法注入 二.使用注解的方式注入@Autowired,@Resource,@Required 本文首先讲解在XML中配置的注 ...
- spring+cxf 开发webService(主要是记录遇到spring bean注入不进来的解决方法)
这里不介绍原理,只是记录自己spring+cxf的开发过程和遇到的问题 场景:第三方公司需要调用我们的业务系统,以xml报文的形式传递数据,之后我们解析报文存储到我们数据库生成业务单据: WebSer ...
- Spring bean注入方式
版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5619525.html Spring bean提供了3中注入方式:属 ...
- spring bean 注入
概念 http://developer.51cto.com/art/200610/33311.htm http://kb.cnblogs.com/page/45266/ ==https://www.c ...
- Spring依赖注入—@Resource注解使用
1.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowire ...
随机推荐
- sin()函数的实现
计算如下公式,并输出结果: 其中r.s的值由键盘输入.sin x的近似值按如下公式计算,计算精度为10-10: 程序说明: #include <math.h>和#include<cm ...
- 【PAT】1006. 换个格式输出整数 (15)
1006. 换个格式输出整数 (15) 让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为 ...
- [实战]MVC5+EF6+MySql企业网盘实战(14)——思考
写在前面 从上面更新编辑文件夹,就一直在思考一个问题,之前编辑文件夹名称,只是逻辑上的修改,但是保存的物理文件或者文件夹名称并没有进行修改,这样就导致一个问题,就是在文件或者文件夹修改名称后就会找不到 ...
- python模拟QQ聊天室(tcp加多线程)
python模拟QQ聊天室(tcp加多线程) 服务器代码: from socket import * from threading import * s = socket(AF_INET,SOCK_S ...
- openssl源码目录结构
openssl源代码主要由eay库.ssl库.工具源码.范例源码以及测试源码组成. eay库是基础的库函数,提供了很多功能.源代码放在crypto目录下.包括如下内容: 1) asn.1 DER编码解 ...
- 洛谷P1073 最优贸易 [图论,DP]
题目传送门 最优贸易 题目描述 C 国有n 个大城市和m 条道路,每条道路连接这n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这m 条道路中有一部分为单向通行的道路,一部分为双向 ...
- EF-CodeFirst模式的简单使用
要求: 引用EntityFramework SqlServer数据库 一个C#项目 项目结构: 将实体模型映射到数据库,只需要在下面三个地方进行配置. 实体类(数据库映射) DbContext上下文( ...
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 xor (根号分治)
xor There is a tree with nn nodes. For each node, there is an integer value a_iai, (1 \le a_i \le ...
- 【LeetCode】shell
195. Tenth Line 输出file.txt中的第十行 答案: # Read from the file file.txt and output the tenth line to stdou ...
- spring中使用@Value设置全局变量默认值
前几天在开发过程中遇到一个使用 spring 的 @Value 给类的全局变量设置默认值不成功的问题,最后通过查资料也是轻松解决,但是发现使用@Value也是有多种多样的方式,今天总算是将开发任务结束 ...