Spring@Autowired注解
2016-05-30 21:36
393 查看
@Autowired注解可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。
注意:@Autowired默认是按照类型来注入的。
看下面的例子:例子是以对成员变量(field)为例进行的
另外一个Customer类有一个Person类型的成员,现用@Autowired注入:
在配置文件中加入
测试程序:
@Autowired默认情况下,总是认为field是可以成功注入的,如果没有成功注入(例如没有匹配的类型)则会跑出异常,如果要使得在没有成功注入的情况下不抛出异常,那么可以和required属性一起使用,将required的属性设置为false:
@Autowired默认是按照类型来匹配的,那么当容器中有两个相同类型的bean时怎样区分要注入是哪一个呢?这时候@Qualifier注解便起作用了:
这时候怎样决定要注入person1呢还是person2呢?
如果想注入person1,那么可以这样做:
最后注意一个细节:如果使用了springMVC自动扫描组件(@Component或者是@Service等),在配置文件中加入了
那么配置文件中的
可以省略。
注意:@Autowired默认是按照类型来注入的。
看下面的例子:例子是以对成员变量(field)为例进行的
public class Person { private String name; private String address; private int age; public void setName(String name) { this.name = name; } public String getName() { return name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void setAddress(String address) { this.address = address; } public String getAddress() { return address; } @Override public String toString() { return "[name:"+getName()+ ",address:" + getAddress()+ ",age:" +getAge()+ "]"; }
另外一个Customer类有一个Person类型的成员,现用@Autowired注入:
public class Customer { @Autowired private Person person; private int type; private String action; public String getAction() { return action; } public void setAction(String action) { this.action = action; } public int getType() { return type; } public void setType(int type) { this.type = type; } @Override public String toString() { return "[Type:"+getType()+ ",action:"+getAction()+ ","+"person:"+ person.toString(); } }
在配置文件中加入
<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!--@Autowired--> <context:annotation-config/> <bean id="CustomerBean" class="com.mkyong.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.mkyong.common.Person"> <property name="name" value="mkyong" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
测试程序:
public class Main { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"}); Customer customer = (Customer)context.getBean("CustomerBean"); System.out.println(customer); } }
@Autowired默认情况下,总是认为field是可以成功注入的,如果没有成功注入(例如没有匹配的类型)则会跑出异常,如果要使得在没有成功注入的情况下不抛出异常,那么可以和required属性一起使用,将required的属性设置为false:
public class Customer { **@Autowired(required=false)** private Person person; private int type; private String action; //getter and setter methods }
@Autowired默认是按照类型来匹配的,那么当容器中有两个相同类型的bean时怎样区分要注入是哪一个呢?这时候@Qualifier注解便起作用了:
<bean id="person1" class="com.hzsunpeng.Person"> <property name="name" value="name1"/> <property name="address" value="adderss1"/> <property name="age" value="23"/> </bean> <bean id="person2" class="com.hzsunpeng.Person"> <property name="name" value="name2"/> <property name="address" value="adderss2"/> <property name="age" value="22"/> </bean>
这时候怎样决定要注入person1呢还是person2呢?
如果想注入person1,那么可以这样做:
public class Customer { @Autowired @Qualifier("person1") private Person person; private int type; private String action; //setter and getter }
最后注意一个细节:如果使用了springMVC自动扫描组件(@Component或者是@Service等),在配置文件中加入了
<context:component-scan base-package="***.***.***" />
那么配置文件中的
<context:annotation-config/>
可以省略。
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- springmvc 发送ajax出现中文乱码的解决方法汇总
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- 浅析Java中的set集合类型及其接口的用法