在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. BigDecimal相关整理

    bigdecimal类型四则运算: BigDecimal s = new Bigdecimal(5); BigDecimal x = new Bigdecimal(15); 依次为最基础的加减乘除: ...

  2. git多站点帐号配置

    事件场景 小明同学是一个非常努力的coder,业余时间都花在了github.com上面,因为公司的台式机性能比较好,小明同学想在公司电脑上面也进行开发.但是github上面的帐号邮箱跟公司分配的帐号邮 ...

  3. java中线程状态-死亡

    线程死亡: 线程会以如下3种方式结束,结束后就处于死亡状态. 1.run()或call()方法执行完成,线程正常结束. 2.线程抛出一个未捕获的Exception或Error 3.直接调用该线程的st ...

  4. Define class with itself as generic implementation. Why/how does this work?

    https://stackoverflow.com/questions/10709061/define-class-with-itself-as-generic-implementation-why- ...

  5. Java数据类型——面试题

    1.short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 +=1;有什么错? 答:对于short s1=1;s1=s1+1来说,在s1+1运算时会自动提升表 ...

  6. c语言字符处理函数常见使用集合

    1.最近看一些开源项目代码时,总会看到 c 语言中一些  "str" 开头的处理字符串的用法,有的之前没用到过,特此记录,随时看到随时添加. 这里不提出源码,只是一些使用说明加例子 ...

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

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

  8. LeftoverDataException,依赖包,apache license 2.0

    1. poi3.9 LeftoverDataException org.apache.poi.hssf.record.RecordInputStream$LeftoverDataException: ...

  9. spring mvc:复选框(多选)

    以user为例,user下有 username用户,password密码, address地址, receivePaper是否订阅, favotireFramework兴趣爱好, user.java ...

  10. WPF中的平移缩放和矩阵变换(TranslateTransform、ScaleTransform、MatrixTransform)

    在WPF中的平移缩放都是通过RenderTransform这个类来实现这些效果的,在这个类中,除了平移和缩放还有旋转.扭曲变换.矩阵变换.这些都差不多的,都是坐标的变换. 这里我就先简单弄个平移和缩放 ...