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/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());
}
}
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());
}
}
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());
}
}
Spring IoC — 基于XML的配置的更多相关文章
- Spring IoC — 基于注解的配置
基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...
- Spring学习--基于 XML 的配置声明切面
正常情况下 , 基于注解的生命要优先于基于 XML 的声明. 通过 AspectJ 注解 , 切面可以与 AspectJ 兼容 , 而基于 XML 的配置则是 Spring 专有的.由于 Aspect ...
- idea的spring整合基于xml文件配置的mybatis报Invalid bound statement (not found): com.music.dao.MusicDao.findAll的问题
一. 题主当时就是自己尝试整合spring和mybatis的时候遇到了这个问题,当时题主只看到了用注解的方式配置的dao层,题主用的是xml文件配置的形式, 而且坑爹的是题主的两个文件的路径写的也不一 ...
- (spring-第2回【IoC基础篇】)Spring的Schema,基于XML的配置
要深入了解Spring机制,首先需要知道Spring是怎样在IoC容器中装配Bean的.而了解这一点的前提是,要搞清楚Spring基于Schema的Xml配置方案. 在深入了解之前,必须要先明白几个标 ...
- Spring框架入门之基于xml文件配置bean详解
关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...
- Spring 框架的概述以及Spring中基于XML的IOC配置
Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...
- spring的基于xml的AOP配置案例和切入点表达式的一些写法
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- Spring Ioc容器xml配置
Spring Ioc容器xml配置基本结构: <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- Spring中基于xml的AOP
1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...
随机推荐
- Golang学习笔记
一.基础 1. Hello World程序 demo: package main import "fmt" // 注释 //注释 func main() { fmt.Printf( ...
- C# 文件管理类 Directory
今天简单接触了一下C#的文件管理类,对类的大体功能做了简单的了解; 做项目用于判断文件是否存.在创建文件.删除文件较为常用:今天大体总结文件调取功能: public string GetFile() ...
- Posix 信号量
作用 信号量的值为0或正整数,就像红灯与绿灯,用于指示当前是否可以接受任务. 信号量对进程和线程都适用. gcc编译时需加-lpthread 基本函数 信号量的相关函数与标准文件函数非常相似,可以理解 ...
- Error_code: 2003
DB:5.6.16 配置:主从 MySQL主从断掉,从库警告日志出现大量的Error_code: 2003Slave I/O error connecting to master .......ret ...
- Python之str(),repr(),``
对于对象obj: str()生成的字串是给人看的 repr()生成的字串是给解析器看的 ``与repr()等义. 最直接就是: ------------------- obj=eval(repr(ob ...
- 【js】随机数
<script> function GetRandomNum(Min,Max){ var Range = Max - Min; var Rand = Math.random() ...
- 【Node.app】Node.js for iOS
Node.app 是用于 iOS 开发的 Node.js 解释器,它允许最大的代码重用和快速创新,占用资源很少,为您的移动应用程序提供 Node.js 兼容的 JavaScript API.你的客户甚 ...
- php必看六本书
php和mysql web开发 PHP高级程序设计_模式.框架与测试.pdf PHP专业项目实例开发.pdf PHP5高级应用开发实践.pdf [深入PHP面向对象.模式与实践(第2版)].(美)赞 ...
- 微信小程序、应用号、订阅号、服务号、企业号小总结
微信小程序是现在微信推出的一个新的项目,但是很多人都不是很清楚微信小程序是怎么一回事,不明白到底怎样分别微信小程序和别的公众号.订阅号等的区别,那么让小编来给你介绍一下. 微信小程序目前是内侧阶段,是 ...
- C#如何判断两个数组相等
/// <summary> /// 数组比较是否相等 /// </summary> /// <param name="bt1">数组1</ ...