- package com.gary.test;
- public class Source {
- public void helloWorld(){
- System.out.println("Hello World!");
- }
- }
- package com.gary.test;
- public class Target {
- private Source source;
- public Target(Source source){
- this.source = source;
- }
- public void sayHelloWorld(){
- source.helloWorld();
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="source" class="com.gary.test.Source" />
- <bean id="target" class="com.gary.test.Target">
- <constructor-arg ref="source" />
- </bean>
- </beans>
- package com.gary.test;
- import org.junit.AfterClass;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TargetTest {
- static BeanFactory factory = null;
- static Target target = null;
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- try{
- factory = new ClassPathXmlApplicationContext("applicationContext.xml");
- target = (Target) factory.getBean("target");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- @AfterClass
- public static void tearDownAfterClass() throws Exception {
- }
- @Test
- public void testSayHelloWorld() {
- target.sayHelloWorld();
- }
- }
- package com.gary.test;
- public class Source {
- public void helloWorld(){
- System.out.println("Hello World!");
- }
- }
- package com.gary.test;
- public class Target {
- private Source source;
- public void setSource(Source source) {
- this.source = source;
- }
- public Source getSource() {
- return source;
- }
- public void sayHelloWorld(){
- getSource().helloWorld();
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
- default-autowire="byType">
- <bean id="source" class="com.gary.test.Source" />
- <bean id="target" class="com.gary.test.Target">
- <!-- 在beans标签添加default-autowire="byType"属性后,下面这行可以省略 -->
- <!-- <property name="source" ref="source" /> -->
- </bean>
- </beans>
- package com.gary.test;
- import org.junit.AfterClass;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TargetTest {
- static BeanFactory factory = null;
- static Target target = null;
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- try{
- factory = new ClassPathXmlApplicationContext("applicationContext.xml");
- target = (Target) factory.getBean("target");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- @AfterClass
- public static void tearDownAfterClass() throws Exception {
- }
- @Test
- public void testSayHelloWorld() {
- target.sayHelloWorld();
- }
- }
- package com.gary.test;
- public class Source {
- public void helloWorld(){
- System.out.println("Hello World!");
- }
- }
- package com.gary.test;
- public interface ISource {
- //方法名任意,方法参数是所依赖对象的类型
- public void injectSource(Source source);
- }
- package com.gary.test;
- public class Target implements ISource{
- private Source source;
- @Override
- public void injectSource(Source source) {
- this.source = source;
- }
- public void sayHelloWorld(){
- source.helloWorld();
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
- default-autowire="byType">
- <bean id="source" class="com.gary.test.Source" />
- <bean id="target" class="com.gary.test.Target" />
- </beans>
- package com.gary.test;
- import org.junit.AfterClass;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TargetTest {
- static BeanFactory factory = null;
- static Source source = null;
- static Target target = null;
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- try{
- factory = new ClassPathXmlApplicationContext("applicationContext.xml");
- target = (Target) factory.getBean("target");
- source = (Source) factory.getBean("source");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- @AfterClass
- public static void tearDownAfterClass() throws Exception {
- }
- @Test
- public void testSayHelloWorld() {
- target.injectSource(source);
- target.sayHelloWorld();
- }
- }