Spring 依赖注入 简单实例Demo

package com.knight.service;

public interface Knight {
	public void doAction();
}

 

package com.knight.service.impl;

import com.knight.service.Knight;
import com.quest.service.Quest;

public class BraveKnight implements Knight {

	private Quest quest;
	public  BraveKnight(Quest quest) {
		this.quest = quest;
	}
	@Override
	public void doAction() {
		quest.qhelp();

	}

}

 

package com.quest.service;

public interface Quest {
	public void qhelp();
}

 

package com.quest.service.impl;

import com.quest.service.Quest;

public class BitDragon implements Quest {

	public BitDragon() {
	}

	public void qhelp() {
		System.out.println("bit dragon");
	}

}

 

package com.quest.service.impl;

import com.quest.service.Quest;

public class RescueBeauty implements Quest {

	public RescueBeauty() {
	}

	public void qhelp() {
		System.out.println("rescue beauty");
	}

}

 

<?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-3.0.xsd">

<bean id="knight" class="com.knight.service.impl.BraveKnight">
	<constructor-arg ref="quest"></constructor-arg>
</bean>

<bean id="quest" class="com.quest.service.impl.RescueBeauty"></bean>

</beans>

 

<?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-3.0.xsd">

<bean id="knight" class="com.knight.service.impl.BraveKnight">
	<constructor-arg ref="quest"></constructor-arg>
</bean>

<bean id="quest" class="com.quest.service.impl.BitDragon"></bean>

</beans>

 

package com.main;

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

import com.knight.service.Knight;

public class KnightMain {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("rescueBeautyKnights.xml");
		Knight knight = (Knight) context.getBean("knight");
		knight.doAction();
		
		ApplicationContext context1 = new ClassPathXmlApplicationContext("bitDragonKnights.xml");
		Knight knight1 = (Knight) context1.getBean("knight");
		knight1.doAction();
	}
}

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringFirst</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    </welcome-file-list>

<context-param>
  <param-name>contextConfigLocation</param-name>
   <param-value>
   rescueBeautyKnights.xml,
   bitDragonKnights.xml
   
   </param-value>
  
</context-param>
   
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
    
</web-app>

 
Spring 依赖注入 简单实例Demo_第1张图片
 
Spring 依赖注入 简单实例Demo_第2张图片
 

你可能感兴趣的:(spring,依赖注入,简单实例Demo)