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的延续,是软件开发中的 一个热点 ...
随机推荐
- oracle数据库一些问题
1.oracle12安装的过程中需要注意字符的选择(特别注意 选择ZHS16GBK 国内企业大多选这个 否则创建数据的时候会出现中文变成问号) 2.设置密码是必须是大小写混合的 pl/sql32链 ...
- odp.net以及oracle oledb安装
连接Oracle数据库需要Oracle数据访问组件(ODAC). 1. 下载ODAC:http://www.oracle.com/technetwork/cn/database/windows/dow ...
- QQ群里收集的外企iOS开发的笔试题
一组外企iOS开发的笔试题,您能回答出来吗?从群里收集来的. 1 why can't NSArray contain NSInteger Instance? with which extra step ...
- ubuntu: 环境搭建
1.修改更新源 sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak sudo gedit /etc/apt/sources.li ...
- 使用JAXP进行sax解析
package cn.liuning.sax; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactor ...
- 团队博客作业Week1 Team Homework #3软件工程在北航
这次我们采访了一位大四的学姐,让她简单地谈了谈去年学习软件工程的经历和感受. 在完成软件工程大作业的过程中,由于计划安排与实际脱节,导致时间前松后紧,平均每周花在这门课上的时间大约有8个小时. 项目完 ...
- 将Xml字符串转换成(DataTable || DataSet || XML)对象
今天用到一个功能:就是把从数据库读出来的内容转换成XML字符串流格式,并输出给一个功能函数.在写的过程,为方便以后的使用,我对这一功能进行分装.该类的具体格式如下:XmlConvert类命名空间:Ni ...
- 微信诡异的 40029 不合法的oauth_code
最近几天在做微信公共平台开发,之前一切正常运行着,发布一套程序出去之后,发现时不时的报错! 小总结下问题出现原因:微信oauth2.0 接口说明 第一步:用户同意授权,获取code 在确保微信公众账号 ...
- 瞧一瞧,看一看呐,用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!!
瞧一瞧,看一看呐用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!! 现在要写的呢就是,用MVC和EF弄出一个CRUD四个页面和一个列表页面的一个快速DEMO,当然是在不 ...
- C#之多态
多态是面向对象编程中三大机制之一,其原理建立在"从父类继承而来的子类可以转换为其父类"这个规则之上,换句话说,能用父类的地方,就能用该类的子类.当从父类派生了很多子类时,由于每个子 ...