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的延续,是软件开发中的 一个热点 ...
随机推荐
- 第一个C#应用 【搜索软件】
搜索软件V1.0 [附软件截图][http://pan.baidu.com/s/1mihEbe4] 设备搜索:支持广播搜索[local search],指定ip[range search]搜索,直接w ...
- VirtualBox内Linux系统与Windows共享文件夹
在日常工作或学习中我们经常需要在一台电脑上同时使用Windows和Linux(这里以Ubuntu为例)两个系统,我们通常的做法有两种: 一种安装双系统(双系统的安装方法经验里已经有很多,大家可以去参照 ...
- [ios]ipad下的splitViewController 让你的APP看起来酷酷的!
在ipad下可以使用splitViewController splitViewController下包含两个viewController 这是一种将屏幕一分为二的方式. 在水平状态下会出现成两个左右两 ...
- Window.ActiveXObject的用法 以及如何判断浏览器的类型
(window.ActiveXObject) 什么意思? 解:判断浏览器是否支持ActiveX控件,如果浏览器支持ActiveX控件可以利用 var xml=new ActiveXObject(&qu ...
- Java 线程池框架核心代码分析
前言 多线程编程中,为每个任务分配一个线程是不现实的,线程创建的开销和资源消耗都是很高的.线程池应运而生,成为我们管理线程的利器.Java 通过Executor接口,提供了一种标准的方法将任务的提交过 ...
- Java异常的深入研究与分析
前言 本文是异常内容的集大成者,力求全面,深入的异常知识研究与分析.本文由金丝燕网独家撰写,参考众多网上资源,经过内容辨别取舍,文字格式校验等步骤编辑而成,以飨读者.对于本文的内容,建议小白需要多多思 ...
- Apache Spark探秘:三种分布式部署方式比较
转自:链接地址: http://dongxicheng.org/framework-on-yarn/apache-spark-comparing-three-deploying-ways/ 目 ...
- IntelliJ IDEA的Maven项目在修改时报java.lang.OutOfMemoryError: PermGen space异常
什么也不说了---内存溢出,遇见太多回了,下面是解决方式: 1.在项目设置中新建Maven,然后设置VM: 2. 在pom.xml添加下面2个插件,一个是jrebel的,一个是jetty的 <b ...
- [BC]BestCoder Round#86小结
1001 [题意] 给定一个长度为n(n<=100000)的正整数序列,给出m(m<=100000)个子集合和的记录,问哪些一定比正确的记录多了 [题解] 对正整数序列求和,记录比和大的一 ...
- linux源码阅读笔记 void 指针
void 指针的步长为1,而其他类型的指针的步长与其所定义的数据结构有关. example: 1 #include<stdio.h> 2 main() 3 { 4 int a[10]; 5 ...