在Spring中,可以使用 @Autowired 注解通过setter方法,构造函数或字段自动装配Bean。此外,它可以在一个特定的bean属性自动装配。
注 @Autowired注解是通过匹配数据类型自动装配Bean。
 
package Autowired;

/**
* Created by luozhitao on 2017/8/9.
*/
public class person1 {
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAdress() {
return adress;
} public void setAdress(String adress) {
this.adress = adress;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} private String name;
private String adress;
private int age; }
package Autowired;

import org.springframework.beans.factory.annotation.Autowired;

/**
* Created by luozhitao on 2017/8/9.
*/
public class Customer1 { public int getType() {
return type;
} public void setType(int type) {
this.type = type;
} public String getAction() {
return action;
} public void setAction(String action) {
this.action = action;
} public person1 getPerson() {
return person;
} @Autowired
public void setPerson(person1 person) {
this.person = person;
} private int type;
private String action;
private person1 person; }
<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"> <context:annotation-config /> <bean id="person" class="Autowired.person1">
<property name="name" value="猫儿"/>
<property name="adress" value="beijing"/>
<property name="age" value="1"/>
</bean> <bean id="customer" class="Autowired.Customer1">
<property name="type" value="2"/>
<property name="action" value="buy"/>
</bean>
</beans>

通过构造方法注入:

package Autowired;

import org.springframework.beans.factory.annotation.Autowired;

/**
* Created by luozhitao on 2017/8/9.
*/
public class Customer2 {
public int getType() {
return type;
} public void setType(int type) {
this.type = type;
} public String getAction() {
return action;
} public void setAction(String action) {
this.action = action;
} public person1 getPerson() {
return person;
} public void setPerson(person1 person) {
this.person = person;
} private int type;
private String action;
private person1 person; @Autowired
public Customer2(person1 person){ this.person=person;
} }

通过字段进行注入:

package Autowired;

import org.springframework.beans.factory.annotation.Autowired;

/**
* Created by luozhitao on 2017/8/9.
*/
public class Customer3 { public int getType() {
return type;
} public void setType(int type) {
this.type = type;
} public String getAction() {
return action;
} public void setAction(String action) {
this.action = action;
} public person1 getPerson() {
return person;
} private int type;
private String action;
@Autowired
private person1 person; }

spring--Autowired setter 方法的更多相关文章

  1. SpringBoot 构造器注入、Setter方法注入和Field注入对比

    0. 引入 今天在看项目代码的时候发现在依赖注入的时候使用了构造器注入,之前使用过 Field 注入和 Setter 方法注入,对构造器注入不是很了解.经过查阅资料看到,Spring 推荐使用构造器注 ...

  2. spring构造函数注入、setter方法注入和接口注入

    Spring开发指南中所说的三种注入方式: Type1 接口注入 我们常常借助接口来将调用者与实现者分离.如: public class ClassA { private InterfaceB clz ...

  3. Spring第六弹—-依赖注入之使用构造器注入与使用属性setter方法注入

    所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中. 使用构造器注入   1 2 3 4 <constructor-arg index=“0” type=“java.lang. ...

  4. spring 构造方法注入和setter方法注入的XML表达

    1.构造方法注入 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC ...

  5. spring setter方法注入

    <bean id="dao" class="Dao"></bean> <bean id="service" c ...

  6. Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法

    XML配置文件代码如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  7. Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  8. 【转】Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  9. spring @Autowired或@Resource 的区别

    1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属于spring的),默认情况下必 ...

  10. spring @Autowired与@Resource的区别

    1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...

随机推荐

  1. Python学习之:pycharm配置

    最近需要做一些小工具,听说Python不错,就学习一下.工欲善其事必先利其器,一个好的IDE对于学习一门新知识是很有帮助的,边写代码边换IDE,纠结了几天,最终还是选择了pycharm,之前觉得不够好 ...

  2. Linux系统中使用netcat命令的奇技淫巧

    netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶. netcat所做的 ...

  3. Python中求阶乘(factorial)

    1. math.factorial(x) import math value = math.factorial(x) 2. reduce函数 def factorial(n): return redu ...

  4. 进程管理工具supervisor

    1. 简介 supervisor有两个组件:supervisord和supervisorctl,组成了client/server结构. supervisord负责读入配置文件,然后supervisor ...

  5. eclipse部署的web项目没有添加到Tomcat的webapps目录下解决方法

    eclipse没有像myeclipse那样,添加web项目时会自动部署到Tomcat的webapps目录下. 而是部署到了eclipse的.metadata\.plugins\org.eclipse. ...

  6. win7 vmware虚拟机上网设置

    1.上网方式设成HOST-ONLY 2.将主机的网络共享VMnet1(完成第一步设置后,VMware自动分配虚拟网络VMnet1) 3.win7下查看VMnet1网络ip 4.根据3查看的IP地址在v ...

  7. plot 含缺失值的图

    x = np.linspace(1,10,25) y = (x-4)**2 index = random.sample(range(25),5) # 从1-24中不放回随机抽取5个数 y[index ...

  8. dpdk对虚拟化的支持调研

    目录: 虚拟化 dpdk的实现研究 virtio vhost SR-IOV 热迁移相关 研究拓展 本文记录近期对dpdk在虚拟化和云计算领域应用的研究成果,内容梳理如下. 虚拟化 虚拟化,抽象来说,就 ...

  9. node 模块部分介绍

    chai  断言框架 mocha mochawesome  对mocha 定制报告,生成完整成熟的报告. node-fetch  服务器版fetch superagent  是node 客户端请求代理 ...

  10. Android ADT远程主机强迫关闭了一个现有的连接 Connection attempts: 1 解决方法

    adb有一个限制, 也可以说是bug.  当手机上同时运行的进程数大于64时, 就会引发adb奔溃. 更深层次的原因, 就是windows API的WaitForMultipleObjects所支持的 ...