Spring4 第二讲

public interface Tester {

	public void test();
}

public class ZhangSan implements Tester{

	public void test(){
		System.out.println("张三-测试");
	}
}

public class Lisi implements Tester{

	public void test(){
		System.out.println("李四-测试");
	}
}

public class JavaWork {
	
	private Tester tester;
	
	public void setTester(Tester tester) {
		this.tester = tester;
	}

	public void doTest(){
		
		tester.test();
	}
}

public class Test2 {

	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
		JavaWork javaWork=(JavaWork)ac.getBean("javaWork");
		javaWork.doTest();
	}
}

<?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="zhangsan" class="com.java1234.service.ZhangSan"></bean>
	
	<bean id="lisi" class="com.java1234.service.Lisi"></bean>
	
	<bean id="javaWork" class="com.java1234.service.JavaWork">
		<property name="tester" ref="lisi"></property>
	</bean>
  
</beans>

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭