Spring 自动装配之不能偷懒
//Longmanfei.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.5.xsd"><bean id="greetingDaoImpl" ><!-- 通过set方法进行注入 --><property name="say" value="你好"></property> </bean><bean id="greetingServiceImpl"autowire="no"><property name="gdi" ref="greetingDaoImpl"></property></bean> </beans>//GreetingDaoImplpublic class GreetingDaoImpl implements GreetingDao { private String say;public void say() {System.out.println("我打了这个招呼"+say);}public void setSay(String say) {this.say = say;} }//GreetingServiceImplpublic class GreetingServiceImpl implements GreetingService{ private GreetingDaoImpl greetingDaoImpl;public void say() {greetingDaoImpl.say();}public void setGreetingDaoImpl(GreetingDaoImpl gdi) {System.out.println("我调用了set方法");this.greetingDaoImpl = gdi;}public GreetingServiceImpl() {super();System.out.println("我调用了空的构造器");}public GreetingServiceImpl(GreetingDaoImpl greetingDaoImpl) {super();System.out.println("我调用了有参的构造器");this.greetingDaoImpl = greetingDaoImpl;} }//junit测试 @Test public void test1(){ /*加载spring容器可以解析多个配置文件采用数组方式传递*/ ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:Longmanfei.xml"); //直接转换成接口便于日后修改数据/*Ioc控制反转体现*/ GreetingService gs=(GreetingService) ac.getBean("greetingServiceImpl"); gs.say(); }
?