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的延续,是软件开发中的 一个热点 ...
随机推荐
- Windows7鼠标右键里没有新建文本文件的选项,解决办法
1.“开始”->“运行”,输入"regedit",打开注册表编辑器 2.展开HKEY_CLASSES_ROOT,找到.txt 3.选中.txt,查看右侧窗格的“默认值”是不是 ...
- sublimetext2 中运行Python提示EOFError: EOF when reading a line
解决方法:一.安装sublimeREPL 打开sublimeText2按CTRL+SHIFT+P,英文版输入:install后选择Package Control: Install Package ...
- [转]PLS-S-00201, identifier 'CALLDEMO.GET_EMPLOYEES' must be declared 预编译错误原因及解决办法
$ proc sample9.pc SQLCHECK=SEMANTICS Pro*C/C++: Release 11.2.0.1.0 - Production on Tue Jan 8 15:18:4 ...
- 拥抱ARM妹纸第二季 之 第一次 点亮太阳
上次做鱼缸LED灯时还有很多材料正好拿来用.穆等等哥- 俺去找材料. 材料列表 3W LED x 3 散热片 x 1 恒流IC x 1 其他零 ...
- [Environment Build] 如何实现Visual Studio中的区域语言环境切换
最开始学习C#这门语言的时候,英文能力不够好,安装的中文版本的Visual Studio,现在工作有段时间了,公司又是个外企,慢慢不习惯中文版本的了,于是产生了想切换语言的想法,网上搜索了下,下载了个 ...
- openwrt无线中继教程
1.设置自己路由lan口的IP地址,网段不能跟上级路由的一样. 2.在无线标签下点击"扫描网络". 3.在新出现的界面中,会列出你附近的无线网络.点击你需要中继的网络右边的&quo ...
- 【开发记录】iOS中使用 Reachability 检测网络
如果你想在iOS程序中提供一仅在wifi网络下使用(Reeder),或者在没有网络状态下提供离线模式(Evernote).那么你会使用到Reachability来实现网络检测. 写本文的目的 了解Re ...
- NGUI3.5系列教程之 一些小功能的实现
(一)可拖动窗体的实现: 1:添加一个Sprite为鼠标点击区域,改名为:DragSprite 2:给DragSprite添加Collider 3:给DragSprite添加Drag Object , ...
- ThinkPHP学习笔记 实例化模型的四种方法
创建Action类 [php] <?php class NewObjectAction extends Action{ public function index(){ ...
- dbutils报错:com.microsoft.sqlserver.jdbc.SQLServerException: 无法识别元数据的表
今天用dbutils操作数据库,莫名地报错:com.microsoft.sqlserver.jdbc.SQLServerException: 无法识别元数据的表 检查了sql语句没有问题.经过仔细排查 ...