Spring IoC — 基于XML的配置
1、属性注入
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());} }
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());} }
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());} }
转载于:https://www.cnblogs.com/yangyquin/p/5320866.html
Spring IoC — 基于XML的配置相关推荐
- Spring IoC — 基于注解的配置
基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...
- spring框架的概述以及spring中基于XML的IOC配置——概念
1.spring的概述 spring是什么 spring的两大核心 spring的发展历程和优势 spring体系结构 2.程序的耦合及解耦 曾经案例中问题 ...
- spring中基于XML的AOP配置步骤
spring中基于XML的AOP配置步骤 IAccountService.java package com.itheima.service;/*** 账户的业务层接口*/ public interfa ...
- java spring bean配置文件_Spring基于xml文件配置Bean过程详解
这篇文章主要介绍了spring基于xml文件配置Bean过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 通过全类名来配置: class:be ...
- spring学习总结(十):基于 XML 的配置AOP
基于 XML 的配置声明AOP 除了使用 AspectJ 注解声明切面, Spring 也支持在 Bean 配置文件中声明切面. 这种声明是通过 aop schema 中的 XML 元素完成的. 正常 ...
- Spring Cache抽象-基于XML的配置声明(基于EhCache的配置)
概述 完整示例 pomxml增加依赖 数据库表数据Oracle 实体类 服务层 ehcache的配置文件 Spring-EhCache配置文件 单元测试 日志输出 日志分析 示例源码 概述 首先请阅读 ...
- Spring Cache抽象-基于XML的配置声明(基于ConcurrentMap的配置)
概述 示例 项目结构 数据库表数据Oracle 实体类 服务层 Spring配置文件 单元测试 日志输出 日志分析 示例源码 概述 Spring Cache基于注解的配置 如果不想使用注解或者由于其他 ...
- 从源码深处体验Spring核心技术--基于Xml的IOC容器的初始化
IOC 容器的初始化包括 BeanDefinition 的 Resource 定位.加载和注册这三个基本的过程. 我们以ApplicationContext 为例讲解,ApplicationConte ...
- Spring框架----Spring的基于XML的AOP的实现
导入依赖 <dependency><groupId>org.springframework</groupId><artifactId>spring-co ...
最新文章
- CVPR 2020满分论文 | FineGym:面向细粒度动作分析的层级化高质量数据集
- win10 tensorrt安装 踩坑记录
- Sitecore 9有什么新功能
- 一个小程序看流的读取
- 机器学习深度学习论文写作工具推荐
- 酷友观点/经验:支付接口返回数据接收地址,session数据丢失(或者说失效)的问题浅析(原创文章)...
- 【Css】基础属性(一)
- EJB是什么?EJB的概念分析与理解(copy)
- HTML+CSS+JS实现 ❤️svg图片透明层文本显示❤️
- ADS(Advanced Design system)仿真后绘图和绘图技巧
- H桥电机驱动电路详解
- catia二次开发:IDE界面介绍
- 【国象AI】总结 + 参考资料
- 电机与拖动 - 1 绪论
- Running MaxQuant——蛋白质组学建库软件(一)
- c#实现Udp通信(四)--UPD大数据量接收(异步接收)
- C++向量夹角公式(带正负)
- Android切词工具——BreakIterator(1)
- CMM---软件能力成熟度模型
- 【巴马火麻茶】调节三高、治疗失眠、排毒减肥,轻松get长寿的秘密!