先管理Bean:
package com.ioc.bean;
import org.springframework.beans.factory.annotation.Configurable;
import com.ioc.service.IBankSecurityService;
@Configurable
public class SecurityAccount {
private IBankSecurityService bankSecurityService;
public void setBankSecurityService(IBankSecurityService bankSecurityService){
this.bankSecurityService=bankSecurityService;
}
public void toBank(Double money){
this.bankSecurityService.securityToBank(money);
}
public void toSecurity(Double money){
this.bankSecurityService.bankToSecurity(money);
}
}
一个Dao的持久层接口:
public interface IBankSecurityDao {
void print(Object bankSecuriObj);
}
接口的实现类:
package com.ioc.dao.impl;
import com.ioc.dao.IBankSecurityDao;
public class BankSecurityDao implements IBankSecurityDao{
@Override
public void print(Object bankSecuriObj) {
System.out.println("bankSecurity--->"+bankSecuriObj);
}
}
service层的实现:
接口:
package com.ioc.service;
public interface IBankSecurityService {
void printBankSecurity();
void securityToBank(Double money);
void bankToSecurity(Double money);
}
实现类:
package com.ioc.service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ioc.dao.IBankSecurityDao;
public class BankSecurityServiceImpl implements IBankSecurityService{
/**属性注入*/
@Autowired
private IBankSecurityDao bankSecurityDao;
/**
* 构建器注入的方式
* @param iBankSecurityDao
*/
public BankSecurityServiceImpl(IBankSecurityDao iBankSecurityDao){
this.bankSecurityDao=iBankSecurityDao;
System.out.println("Constructor inject");
}
/**
* Spring 接口设值注入
* @param iBankSecurityDao
*/
// public void setBankSecurityDao(IBankSecurityDao iBankSecurityDao){
// this.bankSecurityDao=iBankSecurityDao;
// }
public void printBankSecurity(){
this.bankSecurityDao.print(this);
}
@Override
public void securityToBank(Double money) {
System.out.println("securityToBank="+money);
}
@Override
public void bankToSecurity(Double money) {
System.out.println("bankToSecurity="+money);
}
}
配置的工厂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-2.0.xsd" >
<bean id="bankSecurityServiceImpl" class="com.ioc.service.BankSecurityServiceImpl">
<!-- 通过设值注入把bankSecurityDao注入到bankSecurityServiceImpl -->
<!-- <property name="bankSecurityDao" ref="bankSecurityDao"></property>-->
<!-- 通过构建器注入把bankSecurityDao注入到bankSecurityServiceImpl -->
<constructor-arg ref="bankSecurityDao" ></constructor-arg>
</bean>
<bean id="bankSecurityDao" class="com.ioc.dao.impl.BankSecurityDao"></bean>
<!-- 引入注解 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>
</beans>
测试:
package com.ioc.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.ioc.service.IBankSecurityService;
public class TestIoc {
public static void main(String[] args) {
//1:从classpath上装载XML配置文件
Resource resource=new ClassPathResource("com/ioc/test/beanFactory.xml");
//实例化ioc,此时容器并未初始化beanFactory.xml所定义的各个受管bean
//初始化BeanFactory实例后,Ioc并不会预先实例化配置文件里面已声明的各个受管理的POJO,
//只有在应用使用到的POJO,Spring才实例化使用到的POJO
BeanFactory factory=new XmlBeanFactory(resource);
//获取受管bean
IBankSecurityService ibankSecurityService=(IBankSecurityService)factory.
getBean("bankSecurityServiceImpl");
ibankSecurityService.printBankSecurity();
/*2:对于ApplicationContext而言,则不同,一旦实例化ApplicationContext,
* Ioc容器便会预先实例化配置文件中已声明的各个受管POJO,这就是同BeanFactory区别之一
* */
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/com/ioc/test/beanFactory.xml");
IBankSecurityService girl = (IBankSecurityService)ctx.getBean("bankSecurityServiceImpl");
girl.printBankSecurity();
/*属性注入*/
//获取受管的bean或POJO
IBankSecurityService bankSecurityService = (IBankSecurityService)ctx.getBean("bankSecurityServiceImpl");
bankSecurityService.printBankSecurity();
bankSecurityService.bankToSecurity(1000.00);
bankSecurityService.securityToBank(1500.00);
//bankSecuritycom.ioc.service.BankSecurityServiceImpl@1319c
//bankSecuritycom.ioc.service.BankSecurityServiceImpl@ca0115
}
}