在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. Mysql导出数据结构 or 数据

    如果我们单单只想导出mysql数据表结构,通过navcat工具还不行,这时我们可以用mysqldump工具 在mysql server的安装目录:C:\Program Files\MySQL\MySQ ...

  2. Java Swing窗体小工具实例 - 原创

    Java Swing窗体小工具实例 1.本地webserice发布,代码如下: 1.1 JdkWebService.java package server; import java.net.InetA ...

  3. springmvc跨域

    //mvc默认是text/plain;charset=ISO-8859-1@RequestMapping(value = "/xxx", produces = "appl ...

  4. Rotate Image,N*N矩阵顺时针旋转90度

    public class RotateImage { public void rotate(int[][] matrix) { if(matrix.length == 1 && mat ...

  5. Centos6.5 安装gitlab 并使用自带的nginx

    Centos6.5 安装gitlab 并使用自带的nginx 1.安装依赖 yum -y install policycoreutils openssh-server openssh-clients ...

  6. <<Hadoop基础教程》之初识Hadoop【转】

    Hadoop一直是我想学习的技术,正巧最近项目组要做电子商城,我就开始研究Hadoop,虽然最后鉴定Hadoop不适用我们的项目,但是我会继续研究下去,技多不压身. <Hadoop基础教程> ...

  7. 【spark】常用转换操作:sortByKey()和sortBy()

    1.sortByKey() 功能: 返回一个根据键排序的RDD 示例 val list = List(("a",3),("b",2),("c" ...

  8. button出现投影

    style="?android:attr/borderlessButtonStyle" xml中加上这属性

  9. Ceph与OpenStack整合(仅为云主机提供云盘功能)

    1. Ceph与OpenStack整合(仅为云主机提供云盘功能) 创建: linhaifeng,最新修改: 大约1分钟以前 ceph ceph osd pool create volumes 128 ...

  10. LeetCode OJ:Count Primes(质数计数)

    Count the number of prime numbers less than a non-negative number, n. 计算小于n的质数的个数,当然就要用到大名鼎鼎的筛法了,代码如 ...