1、属性注入

注意点:
1)如果类中显示定义了一个带参的构造函数,则一定还要显示提供一个无参构造函数,否则使用属性注入时将抛出异常。
2)JavaBean关于属性命名的特殊规范。Spring只会检查Bean中是否有对应的Setter方法,至于Bean中是否有对应的属性变量则不做要求。如maxSpeed对应setMaxSpeed(),brand对应setBrand()。
所以<property>元素的属性变量名应满足:xxx的属性对应setXxx()方法。变量的前两个字母要么全部大写,要么全部小写。
 
Car类:

package com.ioc.ch4_3_1;
/**
* Created by gao on 16-3-25.
*/
public class Car {
private int maxSpeed;
public String brand;
private double price;
public Car() {
}
public Car(int maxSpeed, String brand, double price) {
this.maxSpeed = maxSpeed;
this.brand = brand;
this.price = price;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public void setBrand(String brand) {
this.brand = brand;
}
public void setPrice(double price) {
this.price = price;
}
public int getMaxSpeed() {
return maxSpeed;
}
public String getBrand() {
return brand;
}
public double getPrice() {
return price;
}
public String toString() {
return "brand:" + brand + "/maxSpeed:" + maxSpeed + "/price:" + price;
}
}

beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="car" class="com.ioc.ch4_3_1.Car">
<property name="maxSpeed"><value>300</value></property>
<property name="brand"><value>奥迪</value></property>
<property name="price"><value>150000.00</value></property>
</bean>
</beans>

测试类:

package com.ioc.ch4_3_1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by gao on 16-3-25.
*/
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com\\ioc\\ch4_3_1\\beans.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car.toString());
}
}
输出结果:
brand:奥迪/maxSpeed:300/price:150000.0
 
2、构造函数注入
注意点:循环依赖问题。Spring容器能顺利实例化以构造函数注入方式配置的Bean有一个前提是:Bean构造函数入参引用的对象必须已经准备就绪。如果两个Bean都采用构造函数注入,而且都通过构造函数入参引用对方,则会发生循环依赖问题。这时可以将构造函数注入方式调整为属性注入方式。
Car类:

package com.ioc.ch4_3_2;
/**
* Created by gao on 16-3-25.
*/
public class Car {
private int maxSpeed;
public String brand;
private double price;
private String corp;
public Car() {
}
public Car(String brand, double price) {
this.brand = brand;
this.price = price;
}
public Car(int maxSpeed, String brand, double price) {
this.maxSpeed = maxSpeed;
this.brand = brand;
this.price = price;
}
public Car(String brand, String corp, int maxSpeed) {
this.brand = brand;
this.corp = corp;
this.maxSpeed = maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public void setBrand(String brand) {
this.brand = brand;
}
public void setPrice(double price) {
this.price = price;
}
public String getCorp() {
return corp;
}
public void setCorp(String corp) {
this.corp = corp;
}
public int getMaxSpeed() {
return maxSpeed;
}
public String getBrand() {
return brand;
}
public double getPrice() {
return price;
}
public String toString() {
return "brand:" + brand + "\tmaxSpeed:" + maxSpeed + "\tprice:" + price + "\tcorp:" + corp;
}
}

Boss类:

package com.ioc.ch4_3_2;
public class Boss {
private String name;
private Car car;
private Office office;
public Boss(String name, Car car, Office office) {
this.name = name;
this.car = car;
this.office = office;
}
public Boss(String name, Car car) {
this.name = name;
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String toString(){
return "name:"+name+"\tcar:"+car.getBrand()+"\toffice:"+office;
}
}

Office类:

package com.ioc.ch4_3_2;
public class Office { }

beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--构造函数注入:type -->
<bean id="car1" class="com.ioc.ch4_3_2.Car">
<constructor-arg type="java.lang.String">
<value>car1_甲壳虫</value>
</constructor-arg>
<constructor-arg type="double">
<value>300000.0</value>
</constructor-arg>
</bean>
<!--构造函数注入:index-->
<bean id="car2" class="com.ioc.ch4_3_2.Car">
<constructor-arg index="0" value="300"/>
<constructor-arg index="1" value="car2_宝马"/>
<constructor-arg index="2" value="200000.0"/>
</bean>
<!--构造函数注入:type&index -->
<bean id="car3" class="com.ioc.ch4_3_2.Car">
<constructor-arg index="0" type="java.lang.String">
<value>car3_红旗CA72</value>
</constructor-arg>
<constructor-arg index="1" type="java.lang.String">
<value>中国一汽</value>
</constructor-arg>
<constructor-arg index="2" type="int">
<value>200</value>
</constructor-arg>
</bean>
<!--构造函数注入:自动识别入参类型 -->
<bean id="boss1" class="com.ioc.ch4_3_2.Boss">
<constructor-arg>
<value>John</value>
</constructor-arg>
<constructor-arg>
<ref bean="car1" />
</constructor-arg>
<constructor-arg>
<ref bean="office" />
</constructor-arg>
</bean>
<bean id="car" class="com.ioc.ch4_3_2.Car"/>
<bean id="office" class="com.ioc.ch4_3_2.Office" />
</beans>

测试类:

package com.ioc.ch4_3_2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by gao on 16-3-25.
*/
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com\\ioc\\ch4_3_2\\beans.xml");
Car car1 = (Car) ctx.getBean("car1");
System.out.println(car1.toString());
System.out.println("--------------------------------");
Car car2 = (Car) ctx.getBean("car2");
System.out.println(car2.toString());
System.out.println("--------------------------------");
Car car3 = (Car) ctx.getBean("car3");
System.out.println(car3.toString());
System.out.println("--------------------------------");
Boss boss1 = (Boss) ctx.getBean("boss1");
System.out.println(boss1.toString());
}
}
输出结果:
brand:car1_甲壳虫 maxSpeed:0 price:300000.0 corp:null
--------------------------------
brand:car2_宝马 maxSpeed:300 price:200000.0 corp:null
--------------------------------
brand:car3_红旗CA72 maxSpeed:200 price:0.0 corp:中国一汽
--------------------------------
name:John car:car1_甲壳虫 office:com.ioc.ch4_3_2.Office@9eed10a
 
3、工厂方法注入
有非静态工厂方法和静态工厂方法两种方式
Car类:

package com.ioc.ch4_3_3;
/**
* Created by gao on 16-3-25.
*/
public class Car {
private int maxSpeed;
public String brand;
private double price;
public Car() {
}
public Car(int maxSpeed, String brand, double price) {
this.maxSpeed = maxSpeed;
this.brand = brand;
this.price = price;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public void setBrand(String brand) {
this.brand = brand;
}
public void setPrice(double price) {
this.price = price;
}
public int getMaxSpeed() {
return maxSpeed;
}
public String getBrand() {
return brand;
}
public double getPrice() {
return price;
}
public String toString() {
return "brand:" + brand + "\tmaxSpeed:" + maxSpeed + "\tprice:" + price;
}
}

CarFactory类:

package com.ioc.ch4_3_3;
public class CarFactory {
//创建Car的工厂方法
public Car createHongQiCar(){
Car car = new Car();
car.setBrand("car5_奔驰");
return car;
}
//工厂类方法是静态的
public static Car createCar(){
Car car = new Car();
return car;
}
}

beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="carFactory" class="com.ioc.ch4_3_3.CarFactory"/>
<bean id="car5" factory-bean="carFactory" factory-method="createHongQiCar"/>
<bean id="car6" class="com.ioc.ch4_3_3.CarFactory" factory-method="createCar"/>
</beans>

测试类:

package com.ioc.ch4_3_3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by gao on 16-3-25.
*/
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com\\ioc\\ch4_3_3\\beans.xml");
Car car5 = (Car) ctx.getBean("car5");
System.out.println(car5.toString());
System.out.println("-----------------------");
Car car6 = (Car) ctx.getBean("car6");
System.out.println(car6.toString());
}
}
输出结果:
brand:car5_奔驰 maxSpeed:0 price:0.0
-----------------------
brand:null maxSpeed:0 price:0.0
注入方法的考量:
1)支持使用构造函数注入的理由:
    · 构造函数可以保证一些重要的属性在Bean实例化时就设置好,避免了因为一些重要属性没有提供,导致一个无用Bean实例的情况;
    · 不需要为每个属性提供Setter方法,减少了类的方法个数;    
    · 可以更好地封装类变量,不需要为每个属性指定setter方法,避免外部错误的调用。
2)更多的开发者更倾向于使用属性注入方式,反对构造函数注入的理由:
    · 如果一个类的属性众多,构造函数的签名将变成一个庞然大物,可读性很差;
    · 灵活性不强,在有些属性是可选的情况下,如果通过构造函数注入,也需要为可选的参数提供一个null值;
    · 如果有多个构造函数,需要考虑配置文件和具体构造函数匹配歧义的问题,配置上相对复杂;
    · 构造函数不利于类的继承和扩展,因为子类需要引用到父类复杂的构造函数;
    · 构造函数注入有时会造成循环依赖的问题。
3)对于一个全新的应用来说,不推荐使用工厂方法的注入方式。

Spring IoC — 基于XML的配置的更多相关文章

  1. Spring IoC — 基于注解的配置

    基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...

  2. Spring学习--基于 XML 的配置声明切面

    正常情况下 , 基于注解的生命要优先于基于 XML 的声明. 通过 AspectJ 注解 , 切面可以与 AspectJ 兼容 , 而基于 XML 的配置则是 Spring 专有的.由于 Aspect ...

  3. idea的spring整合基于xml文件配置的mybatis报Invalid bound statement (not found): com.music.dao.MusicDao.findAll的问题

    一. 题主当时就是自己尝试整合spring和mybatis的时候遇到了这个问题,当时题主只看到了用注解的方式配置的dao层,题主用的是xml文件配置的形式, 而且坑爹的是题主的两个文件的路径写的也不一 ...

  4. (spring-第2回【IoC基础篇】)Spring的Schema,基于XML的配置

    要深入了解Spring机制,首先需要知道Spring是怎样在IoC容器中装配Bean的.而了解这一点的前提是,要搞清楚Spring基于Schema的Xml配置方案. 在深入了解之前,必须要先明白几个标 ...

  5. Spring框架入门之基于xml文件配置bean详解

    关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...

  6. Spring 框架的概述以及Spring中基于XML的IOC配置

    Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...

  7. spring的基于xml的AOP配置案例和切入点表达式的一些写法

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  8. Spring Ioc容器xml配置

    Spring Ioc容器xml配置基本结构: <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  9. Spring中基于xml的AOP

    1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...

随机推荐

  1. 关于C语言指针中的p++与p+i

    先看一组代码: #include <stdio.h> void main() { int i,*p,a[7]; p=a; for(i=0;i<7;i++) scanf("% ...

  2. Java从入门到精通——数据库篇Oracle 11g服务详解

    装上Oracle之后大家都会感觉到我们的电脑慢了下来,如何提高计算机的速度呢?我们应该打开必要的服务,关闭没有用的服务.下面是Oracle服务的详解: Oracle ORCL VSS Writer S ...

  3. Vim配置IDE开发环境

    我的vim IDE界面: 1.安装Vim和Vim基本插件首先安装好Vim和Vim的基本插件.这些使用apt-get安装即可:lingd@ubuntu:~/arm$sudo apt-get instal ...

  4. 转:一份基础的嵌入式Linux工程师笔试题

    一. 填空题: 1. 一些Linux命令,显示文件,拷贝,删除 Ls cp rm 2. do……while和while……do有什么区别? 3. Linux系统下.ko文件是什么文件?.so文件是什么 ...

  5. 【css老版本浏览器兼容利器】ie-css3.htc

    做前端的同学都应该听说或者用过,是一段脚本,可以让ie实现css3里的圆角和阴影效果. css带来的便利是很容易感受的到的,但恶心的是它在ie下的不兼容,所以某位牛人现身写了个ie-css3.htc, ...

  6. AIR for IOS开发问题小结

    昨天终于成功地向APP STORE提交了应用,个人感觉用AIR做IOS开发就是个坑啊.出了问题之后,问苹果的技术支持,人家说“对于非XCODE环境下开发及发布所造成的问题我们在资料库中无法找到相应的解 ...

  7. ubuntu 14.04链接无线路由,建立无线和有线链接

    神奇的linux. 废话不多说,进入主题: 首先1:买一部带wifi的笔记本电脑,买一个可用的无线路由器,像网络提供商申请上网缴费==! 2,中国国情,我们大多都是用ADSL咯.所以其它情况就不说了. ...

  8. NodeJS学习笔记(转载)

    前言 让nodeJS跑起来 文件结构 node_modules/ejs app.js 路由 路由规则 添加路由规则 注册功能 MongoDB 安装MongoDB 链接MongoDB 结语 前言 最近同 ...

  9. LoadRunner 学习笔记(1)性能测试常见术语

    并发用户数据:与服务器进行交互的在线用户数量 请求响应时间:从Client端发出请求到得到响应的整个时间 一般包括网络响应时间 + server的响应时间 事务请求响应时间:完成这个事务所用的时间 这 ...

  10. 一个SQL Server 2008 R2 死锁的问题解决

    问题场景:在客户那碰到一个操作卡死的现象 问题解决: 1.如何挂钩是死锁问题:通过代码跟踪,发现是指执行一个SQL语句超时,因此猜想可能是表锁住了 2.如果确认是思索问题:通过SQL发现死锁,以下是相 ...