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/beanshttp://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/beanshttp://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/beanshttp://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)对于一个全新的应用来说,不推荐使用工厂方法的注入方式。

转载于:https://www.cnblogs.com/yangyquin/p/5320866.html

Spring IoC — 基于XML的配置相关推荐

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

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

  2. spring框架的概述以及spring中基于XML的IOC配置——概念

    1.spring的概述     spring是什么     spring的两大核心     spring的发展历程和优势     spring体系结构 2.程序的耦合及解耦     曾经案例中问题   ...

  3. spring中基于XML的AOP配置步骤

    spring中基于XML的AOP配置步骤 IAccountService.java package com.itheima.service;/*** 账户的业务层接口*/ public interfa ...

  4. java spring bean配置文件_Spring基于xml文件配置Bean过程详解

    这篇文章主要介绍了spring基于xml文件配置Bean过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 通过全类名来配置: class:be ...

  5. spring学习总结(十):基于 XML 的配置AOP

    基于 XML 的配置声明AOP 除了使用 AspectJ 注解声明切面, Spring 也支持在 Bean 配置文件中声明切面. 这种声明是通过 aop schema 中的 XML 元素完成的. 正常 ...

  6. Spring Cache抽象-基于XML的配置声明(基于EhCache的配置)

    概述 完整示例 pomxml增加依赖 数据库表数据Oracle 实体类 服务层 ehcache的配置文件 Spring-EhCache配置文件 单元测试 日志输出 日志分析 示例源码 概述 首先请阅读 ...

  7. Spring Cache抽象-基于XML的配置声明(基于ConcurrentMap的配置)

    概述 示例 项目结构 数据库表数据Oracle 实体类 服务层 Spring配置文件 单元测试 日志输出 日志分析 示例源码 概述 Spring Cache基于注解的配置 如果不想使用注解或者由于其他 ...

  8. 从源码深处体验Spring核心技术--基于Xml的IOC容器的初始化

    IOC 容器的初始化包括 BeanDefinition 的 Resource 定位.加载和注册这三个基本的过程. 我们以ApplicationContext 为例讲解,ApplicationConte ...

  9. Spring框架----Spring的基于XML的AOP的实现

    导入依赖 <dependency><groupId>org.springframework</groupId><artifactId>spring-co ...

最新文章

  1. CVPR 2020满分论文 | FineGym:面向细粒度动作分析的层级化高质量数据集
  2. win10 tensorrt安装 踩坑记录
  3. Sitecore 9有什么新功能
  4. 一个小程序看流的读取
  5. 机器学习深度学习论文写作工具推荐
  6. 酷友观点/经验:支付接口返回数据接收地址,session数据丢失(或者说失效)的问题浅析(原创文章)...
  7. 【Css】基础属性(一)
  8. EJB是什么?EJB的概念分析与理解(copy)
  9. HTML+CSS+JS实现 ❤️svg图片透明层文本显示❤️
  10. ADS(Advanced Design system)仿真后绘图和绘图技巧
  11. H桥电机驱动电路详解
  12. catia二次开发:IDE界面介绍
  13. 【国象AI】总结 + 参考资料
  14. 电机与拖动 - 1 绪论
  15. Running MaxQuant——蛋白质组学建库软件(一)
  16. c#实现Udp通信(四)--UPD大数据量接收(异步接收)
  17. C++向量夹角公式(带正负)
  18. Android切词工具——BreakIterator(1)
  19. CMM---软件能力成熟度模型
  20. 【巴马火麻茶】调节三高、治疗失眠、排毒减肥,轻松get长寿的秘密!

热门文章

  1. 有关Vector里面元素重复解决办法
  2. Oracle统计信息的导出、导入
  3. mysql-零基础安装
  4. 置顶java[常用]-[语法]-[基础操作]
  5. BufferedInputStream
  6. SCR638红外接收管介绍
  7. LinQ高级查询、组合查询
  8. 【client】与【offset】
  9. sqlite3-查看数据库
  10. Asp.Net Web API 2第七课——Web API异常处理