Spring第二讲

Spring第二讲_第1张图片

<?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="zhangsan"></property> -->
		<property name="tester" ref="lisi"></property>
	</bean> 
</beans>
package com.java1234.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.java1234.service.JavaWork;
import com.java1234.test.HelloWorld;

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

import com.java1234.service.JavaWork;
import com.java1234.service.Lisi;
import com.java1234.service.ZhangSan;

public class Test {
	
	public static void main(String[] args) {
		JavaWork javaWork=new JavaWork();
//		javaWork.setTester(new ZhangSan());
		javaWork.setTester(new Lisi());
		javaWork.doTest();
	}
}

package com.java1234.test;

public class HelloWorld {

	public void say(){
		System.out.println("Spring4大爷你好!");
	}
}

package com.java1234.service;

public class ZhangSan implements Tester{
	public void test(){
		System.out.println("张三-测试程序");
	}
}

package com.java1234.service;

public interface Tester {
	
	public void test();
}

package com.java1234.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.java1234.test.HelloWorld;

public class Test {
	
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
		HelloWorld helloWorld=(HelloWorld)ac.getBean("helloWorld");
		helloWorld.say();
	}
}

package com.java1234.service;

public class Lisi implements Tester{
	public void test(){
		System.out.println("李四-测试程序");
	}
}

package com.java1234.service;

public class JavaWork {
	
	private Tester tester;
	
	public void setTester(Tester tester) {
		this.tester = tester;
	}
	public void doTest(){
//		ZhangSan zhangsan=new ZhangSan();
//		zhangsan.test();
		tester.test();
	}
}

你可能感兴趣的:(Spring,spring,java,servlet)