Spring IOC 容器可以自动装配Bean,需要做的是在<bean>的autowire属性里指定自动装配的模式

1)byType 根据类型自动装配,若IOC 容器中有多个目标Bean类型一致的Bean,Spring将无法判定哪个Bean最适合该属性,不能执行自动装配

2)byName 根据名称自动装配,必须将目标Bean名称和属性名设置的完全相同

配置方法:

Address.java

package com.spring.autowire;

public class Address {
private String city;
private String location;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String toString() {
return "Address [city=" + city + ", location=" + location + "]";
}
}

2、Car.java

package com.spring.autowire;

public class Car {
private String brand;
private String price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}

3、Person.java

package com.spring.autowire;

public class Person {
private String name;
private String age;
private Address address; @Override
public String toString() {
return "Person [address=" + address + ", age=" + age + ", car=" + car
+ ", name=" + name + "]";
}
private Car car;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
}

4、配置文件bean-autowire.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="car" class="com.spring.autowire.Car">
<property name="brand" value="Audi"></property>
<property name="price" value="300000"></property>
</bean> <bean id="address" class="com.spring.autowire.Address">
<property name="city" value="beijing"></property>
<property name="location" value="fangshan"></property>
</bean> <bean id="person" class="com.spring.autowire.Person" autowire="byName">
</bean>
</beans>

5、测试

package com.spring.autowire;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-autowire.xml");
Person person = (Person)ctx.getBean("person");
System.out.println(person);
}
}

  可以使用autowire 属性指定自动装配的方式,byName 根据bean 的名字和当前bean的setter风格的属性名进行自动装配 

  byType根据bean的类型和当前bean的属性类型进行自动装配,若 IOC 容器中有1个以上的类型的bean,则抛出异常              

Spring4.0学习笔记(2) —— 自动装配的更多相关文章

  1. Spring4.0学习笔记(11) —— Spring AspectJ 的五种通知

    Spring AspectJ 一.基于注解的方式配置通知 1.额外引入的jar包: a) com.springsource.org.aopalliance-1.0.0.jar b) com.sprin ...

  2. Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean

    1.实现Spring 提供的FactoryBean接口 package com.spring.facoryBean; import org.springframework.beans.factory. ...

  3. Spring4.0学习笔记(6) —— 通过工厂方法配置Bean

    1.静态工厂方法: bean package com.spring.factory; public class Car { public Car(String brand) { this.brand ...

  4. Spring4.0学习笔记(5) —— 管理bean的生命周期

    Spring IOC 容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务 Spring IOC 容器对Bean的生命周期进行管理的过程: 1.通过构造器或工厂方法 ...

  5. Spring4.0学习笔记(4) —— 使用外部属性文件

    1.配置xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...

  6. Spring4.0学习笔记(3) —— Spring_Bean之间的关系

    1.继承关系 bean-relation.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  7. Spring4.0学习笔记(12) —— JDBCTemplate 操作数据库

    整体配置 1.配置xml文件 <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi ...

  8. Spring4.0学习笔记(10) —— Spring AOP

    个人理解: Spring AOP 与Struts 的 Interceptor 拦截器 有着一样的实现原理,即通过动态代理的方式,将目标对象与执行对象结合起来,降低代码之间的耦合度,主要运用了Proxy ...

  9. Spring4.0学习笔记(9) —— Spring泛型依赖注入

    1.定义基础仓库 package com.spring.generic.di; public class BaseRepository<T> { } 2.定义基础服务层 package c ...

随机推荐

  1. API风格

    Client------------ Request ----------------->多Server端 Server------------------------------------- ...

  2. zoj 3787 Access System

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5274 #include <cstdio> #include < ...

  3. 吃了单片机GPIO端口工作模式的大亏 ——关于强推挽输出和准双向口(弱上拉)的实际应用

    最近公司在进行一个项目,需要用到超声波测距的功能,于是在做好硬件电路,但在写控制程序时,却遇上了令我费解的事情. 当在单片机最小系统上调好输出频率40kHz,占空比50%的方波输出信号后,将程序烧至超 ...

  4. opencv 用户文档 错误更正 仿射变换

    今天在看opencv官方给出的仿射变换计算仿射变换矩阵的文档的时候,发现官方文档中有个很明显的错误,再次给大家提个醒. 官方文档连接: http://opencv.willowgarage.com/d ...

  5. 计算机语言的发展(the history of computer's language)

    第一部分 计算机语言的分类: 机器语言:直接用二进制代码指令表达的计算机语言,指令是用0和1组成的一串代码.例如:1011011000000000,表示加法.可以直接执行. 汇编语言:汇编指令集.伪指 ...

  6. 关于Cookie和Session【转载】

    当你第一次访问一个网站的时候,网站服务器会在响应头内加上Set-Cookie:PHPSESSID=nj1tvkclp3jh83olcn3191sjq3(php服务器),或Set-Cookie JSES ...

  7. Repo安装遇到问题

    问题一: “The program 'repo' is currently not installed. You can install it by typing: sudo apt-get inst ...

  8. js用for循环为对象添加事件并传递参数

    var objArr = getObjArr(id);   for(var i=0; i<objArr.length; i++){    var param=objArr.param    ad ...

  9. cmd 命令设置UTF8

    使用cmd 执行java -jar executable.jar  测试包时,cmd显示中文正常,但是日志文件中中文显示不正常,也导致执行时不能正常做些检测和验证 这是由于cmd命令窗口的编码格式问题 ...

  10. iOS NSInvocation的学习

    用途: NSInvocation的作用和performSelector:withObject:的作用是一样的:用于iOS编程中调用某个对象的消息. performSelector:withObject ...