在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. 使用AutoIT检测已安装软件,并将结果保存在桌面

    $file = "\adobe安装列表.txt" $regedit1 = "hklm64\SOFTWARE\Wow6432Node\Microsoft\Windows\C ...

  2. 20145229吴姗珊《网络对抗》shellcode注入&Return-to-libc攻击深入

    20145229吴姗珊<网络对抗>shellcode注入&Return-to-libc攻击深入(待上传) shellcode注入 shellcode是一段代码,是溢出程序和蠕虫病毒 ...

  3. Linux系统下强大的lsof命令使用宝典

    lsof命令简介: lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件 ...

  4. CentOS 7 安装各个桌面版本

    http://unix.stackexchange.com/questions/181503/how-to-install-desktop-environments-on-centos-7 92dow ...

  5. Linux Shell脚本简介

    Shell 诞生于 Unix,是与 Unix/Linux 交互的工具,单独地学习 Shell 是没有意义的,请先参考Unix/Linux入门教程,了解 Unix/Lunix 基础. 近几年来,Shel ...

  6. contos LAMP环境搭建

    LINUX搭建LAMP笔记 .YUM:Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于R ...

  7. Navicat Premium 10/12——破解激活

    Navicat Premium 12官方Windows64位百度云 链接:https://pan.baidu.com/s/1hGmDljszQsUoi194CYdfmA 密码:1xff 官方下载链接 ...

  8. struts2取值

    http://www.cnblogs.com/yangy608/archive/2010/11/23/1885256.html struts2取值 1.标签取值方式一 通过<s:property ...

  9. (转)Java编译后产生class文件的命名规则

      今天刚好有同学问了下Java编译后产生的.class文件名的问题,虽然一直都在使用Java做开发,但是之前对编译后产生的.class文件名的规范也基本没做了解过,也真的是忏愧啊!今天无论如何都要总 ...

  10. asp.net core mvc中如何把二级域名绑定到特定的控制器上

    由于公司的工作安排,一直在研究其他技术,所以一直没时间更新博客,今天终于可以停下手头的事情,写一些新内容了. 应用场景:企业门户网站会根据内容不同,设置不同的板块,如新浪有体育,娱乐频道,等等.有的情 ...