Spring入门(6)-使用注解装配
Spring入门(6)-使用注解装配
本文介绍如何使用注解装配。
0. 目录
- 使用Autowired
- 可选的自动装配
- 使用Qualifier选择
1. 使用Autowired
package com.chzhao.springtest;
import org.springframework.beans.factory.annotation.Autowired;
public class PersonBll implements IPersonBll {
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Autowired
private Person person;
public void show() {
System.out.println(this.person.getName());
}
}
注:@Autowired也可以放在setPerson上面。
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
"
>
<context:annotation-config/>
<bean name="laoli" class="com.chzhao.springtest.Person">
<property name="name" value="老李" />
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll"/>
</beans>
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>
<context:annotation-config/>
<bean id="bll" class="com.chzhao.springtest.PersonBll"></bean>
</beans>
如上配置文件中没有定义Bean,程序运行后,会抛出以下异常。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bll': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.chzhao.springtest.Person com.chzhao.springtest.PersonBll.person; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.chzhao.springtest.Person] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
只需要增加required=false即可,当然程序会抛出空指针异常。
package com.chzhao.springtest;
import org.springframework.beans.factory.annotation.Autowired;
public class PersonBll implements IPersonBll {
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Autowired(required=false)
private Person person;
public void show() {
System.out.println(this.person.getName());
}
}
3. 使用Qualifier选择
如果定义了两个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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>
<context:annotation-config/>
<bean id="laowang" class="com.chzhao.springtest.Person">
<property name="name" value="老王"></property>
</bean>
<bean id="laoli" class="com.chzhao.springtest.Person">
<property name="name" value="老李"></property>
</bean>
<bean id="bll" class="com.chzhao.springtest.PersonBll"></bean>
</beans>
如上,定义了两个person,laowang和laoli,自动注入的时候会选择哪个呢?
实际上,会抛出如下异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bll': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.chzhao.springtest.Person com.chzhao.springtest.PersonBll.person; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.chzhao.springtest.Person] is defined: expected single matching bean but found 2: laowang,laoli
通过Qualifier指定装配哪个Bean就可以了。
package com.chzhao.springtest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class PersonBll implements IPersonBll {
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Autowired
@Qualifier("laoli")
private Person person;
public void show() {
System.out.println(this.person.getName());
}
}
Spring入门(6)-使用注解装配的更多相关文章
- (转)java之Spring(IOC)注解装配Bean详解
java之Spring(IOC)注解装配Bean详解 在这里我们要详细说明一下利用Annotation-注解来装配Bean. 因为如果你学会了注解,你就再也不愿意去手动配置xml文件了,下面就看看 ...
- Spring入门2. IoC中装配Bean
Spring入门2. IoC中装配Bean 20131125 前言: 上一节学习了Spring在JavaProject中的配置,通过配置文件利用BeanFactory和ApplicationConte ...
- Spring入门(二):自动化装配bean
Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...
- java之Spring(IOC)注解装配Bean详解
在这里我们要详细说明一下利用Annotation-注解来装配Bean. 因为如果你学会了注解,你就再也不愿意去手动配置xml文件了,下面就看看Annotation的魅力所在吧. 先来看看之前的bean ...
- Spring学习笔记--使用注解装配
使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...
- Spring入门(八):自动装配的歧义性
1. 什么是自动装配的歧义性? 在Spring中,装配bean有以下3种方式: 自动装配 Java配置 xml配置 在这3种方式中,自动装配为我们带来了很大的便利,大大的降低了我们需要手动装配bean ...
- Spring入门之通过注解 处理 数据库事务
用Spring 中的事务写的银行转帐的例子:(环境同上一个贴子) 一.表结构: (create table (id int,username varchar(10),salary int);) 二.文 ...
- spring入门(二) 使用注解代替xml配置
1.导包(略) 2.applicationContext.xml如下: <?xml version="1.0" encoding="UTF-8"?> ...
- Spring 自动注册及自动装配
Spring支持三种注册Bean及装配Bean的方式: 显式地在Java代码中注册及装配 显示地在Xml文件中注册及装配 隐式地装配,即自动注册及装配 这三种方式可以混合使用.选择哪种更多地是看个人品 ...
- spring在IoC容器中装配Bean详解
1.Spring配置概述 1.1.概述 Spring容器从xml配置.java注解.spring注解中读取bean配置信息,形成bean定义注册表: 根据bean定义注册表实例化bean: 将bean ...
随机推荐
- Java连接MySQL数据库及简单操作代码
1.Java连接MySQL数据库 Java连接MySql需要下载JDBC驱动MySQL-connector-java-5.0.5.zip(举例,现有新版本).然后将其解压缩到任一目录.我是解压到D盘, ...
- jetty ZipException: invalid entry size
The issue, as I suspected, was due a corrupt JAR file. The solution for me was to clear my local rep ...
- decode-string(挺麻烦的)
Java String作为参数传参是不会改变的,这个与常识的感觉不同. public String decodeString(String s) { s = ""; return ...
- Android消息推送完美解决方案全析
推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原因,Android消息推送我们不得不大费周折.本文就是用来和大家共同探讨一种Android消息推 ...
- uva12169 Disgruntled Judge
扩展欧几里得. 枚举a,根据x1,x3和递推式可得. (a+1)*b-k*mod=f[3]-a*a*b. 通过扩展欧几里得求出b. 带入原式进行计算. #include<cstdio> # ...
- C#手动回收内存的简单方法
C#有自动回收内存的机制,但是有时自动回收有一定滞后,需要在变量使用后迅速回收,节约内存,这里介绍一个最简单的方法. 1.先对对象赋值 null; 2.System.GC.Collect(); 代码样 ...
- 关于ie6对齐
先来没有任何对齐时的样子: 1.一种是在父级没有高度的情况下居中. 给每个独立的元素都加上vertical-align:middle; 针对文字可以不加,加与不加都可以居中对齐.但是无法做到绝对的居中 ...
- Java [Leetcode 326]Power of Three
题目描述: Given an integer, write a function to determine if it is a power of three. Follow up:Could you ...
- c & c++中const
1 const的基本用法 常变量: const 类型说明符 变量名 #在c语言中,仍是一个变量.c++中则变为一个常量,比如可以设置为数组的长度 常引用: const 类型说明符 &引用名 ...
- vc/mfc获取rgb图像数据后动态显示及保存图片的方法
vc/mfc获取rgb图像数据后动态显示及保存图片的方法 该情况可用于视频通信中获取的位图数据回放显示或显示摄像头捕获的本地图像 第一种方法 #include<vfw.h> 加载 vfw3 ...