控制反转(Inversion ofControl):将一些对象的创建权交给Spring
需要的jar包
//日志接口 com.springsource.org.apache.commons.logging-1.1.1.jar //日志实现 com.springsource.org.apache.log4j-1.2.15.jar spring-beans-4.2.4.RELEASE.jar spring-context-4.2.4.RELEASE.jar spring-core-4.2.4.RELEASE.jar spring-expression-4.2.4.RELEASE.jar
public interface UserDao { public void save(); }
public class UserDaoImpl implements UserDao { @Override public void save() { System.out.println("UserDaoImpl...save..."); } }
UserDao userDao = new UserDaoImpl(); userDao.save();
如果底层实现切换,需要修改源码,Spring IOC就是用来在不修改源码的情况下对程序进行扩展.
配置文件 <?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.xsd"> <bean id="userDao" class="com.swifter.demo1.UserDaoImpl"></bean> </beans>
调用 ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao) classPathXmlApplicationContext.getBean("userDao"); userDao.save();
只需要业务代码,配置文件,反射和工厂不需要实现.
<bean id="customerDao" class="com.swifter.demo2.CustomerDaoImpl" init-method="init" destroy-method="destroy"> </bean>
public class CustomerDaoImpl implements CustomerDao { public void init() { System.out.println("CustomerDaoImpl...init..."); } public void destroy() { System.out.println("CustomerDaoImpl...destroy..."); } @Override public void save() { System.out.println("CustomerDaoImpl...save..."); } }
调用
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); CustomerDao customerDao = (CustomerDao) classPathXmlApplicationContext.getBean("customerDao"); customerDao.save(); classPathXmlApplicationContext.close();
//scope是singleton会调用destroy方法,但是prototype不会调用, 默认是singleton
<bean id="car" class="com.swifter.demo4.Car"> <constructor-arg name="name" value="QQ"/> <constructor-arg name="money" value="20000"/> </bean>
public class Car { private String name; private Double money; public Car() { } public Car(String name, Double money) { this.name = name; this.money = money; } @Override public String toString() { return "Car{" + "name='" + name + '/'' + ", money=" + money + '}'; } }
<bean id="car2" class="com.swifter.demo4.Car"> <property name="name" value="奔腾"/> <property name="money" value="2000"/> </bean>
public class Car { private String name; private Double money; public void setName(String name) { this.name = name; } public void setMoney(Double money) { this.money = money; } public Car() { } public Car(String name, Double money) { this.name = name; this.money = money; } @Override public String toString() { return "Car{" + "name='" + name + '/'' + ", money=" + money + '}'; } }
设置对象类型属性
<bean id="employee" class="com.swifter.demo4.Employee"> <property name="name" value="王五"/> <!--value设置普通类型, ref设置对象类型--> <property name="car" ref="car"/> </bean>
public class Employee { private String name; private Car car; public void setName(String name) { this.name = name; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Employee{" + "name='" + name + '/'' + ", car=" + car + '}'; } }
xmlns:p="http://www.springframework.org/schema/p" 要加上 <!--p名称空间方式--> <bean id="car3" class="com.swifter.demo4.Car" p:name="qq" p:money="200" />