Spring4.0学习笔记(2) —— 自动装配
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) —— 自动装配的更多相关文章
- Spring4.0学习笔记(11) —— Spring AspectJ 的五种通知
Spring AspectJ 一.基于注解的方式配置通知 1.额外引入的jar包: a) com.springsource.org.aopalliance-1.0.0.jar b) com.sprin ...
- Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean
1.实现Spring 提供的FactoryBean接口 package com.spring.facoryBean; import org.springframework.beans.factory. ...
- Spring4.0学习笔记(6) —— 通过工厂方法配置Bean
1.静态工厂方法: bean package com.spring.factory; public class Car { public Car(String brand) { this.brand ...
- Spring4.0学习笔记(5) —— 管理bean的生命周期
Spring IOC 容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务 Spring IOC 容器对Bean的生命周期进行管理的过程: 1.通过构造器或工厂方法 ...
- Spring4.0学习笔记(4) —— 使用外部属性文件
1.配置xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...
- Spring4.0学习笔记(3) —— Spring_Bean之间的关系
1.继承关系 bean-relation.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...
- Spring4.0学习笔记(12) —— JDBCTemplate 操作数据库
整体配置 1.配置xml文件 <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi ...
- Spring4.0学习笔记(10) —— Spring AOP
个人理解: Spring AOP 与Struts 的 Interceptor 拦截器 有着一样的实现原理,即通过动态代理的方式,将目标对象与执行对象结合起来,降低代码之间的耦合度,主要运用了Proxy ...
- Spring4.0学习笔记(9) —— Spring泛型依赖注入
1.定义基础仓库 package com.spring.generic.di; public class BaseRepository<T> { } 2.定义基础服务层 package c ...
随机推荐
- [Android] 混音器AudioMixer
AudioMixer是Android的混音器,通过混音器可以把各个音轨的音频数据混合在一起,然后输出到音频设备. 创建AudioMixer AudioMixer在MixerThread的构造函数内创建 ...
- Chapter 7 Backup and Recovery 备份和恢复:
Chapter 7 Backup and Recovery 备份和恢复: Table of Contents 7.1 Backup and Recovery Types 7.2 Database Ba ...
- 【模拟】XMU 1599 斐波那契汤
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1599 题目大意: 给k,m,q以及f[1]...f[k],当n<m时,f[n]= ...
- 服务器端javascript——Rhino和Node
Node: Node是v8 javasript解析器的一个特别版本,侧重于异步I/O,网络和HTTP 入门见:http://www.cnblogs.com/wishyouhappy/p/3647037 ...
- Diamond Armor - The most expensive Suit: 2.8 Mio Swiss Francs
Diamond Armor - The most expensive Suit: 2.8 Mio Swiss Francs Diamond Armor
- maven项目 打可执行jar包
1.pom添加 <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</art ...
- Object-C变量作用域 -- 笔记
@interface Dog: NSObject { @public int age; @protected int ID; @Private float price; } @end 字段作用域解析: ...
- CodeForces 146E - Lucky Subsequence DP+扩展欧几里德求逆元
题意: 一个数只含有4,7就是lucky数...现在有一串长度为n的数...问这列数有多少个长度为k子串..这些子串不含两个相同的lucky数... 子串的定义..是从这列数中选出的数..只要序号不同 ...
- [AngularJS] Design Pattern: Simple Mediator
We're going to use rootScope emit here to send out events and then we're going to listen for them in ...
- mysql 修复表和优化表
REPAIR TABLE `table_name` 修复表 OPTIMIZE TABLE `table_name` 优化表