Spring 依赖注入 简单实例Demo 博客分类: Spring Spring 依赖注入 简单实例Demo
程序员文章站
2024-03-15 13:28:29
...
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 博客分类: Spring Spring 依赖注入 简单实例Demo
-
Spring Bean简单应用实例 博客分类: Spring Spring Bean简单应用实例装配beanbean注入
-
Spring Bean简单应用实例 博客分类: Spring Spring Bean简单应用实例装配beanbean注入
-
实例讲解Java的Spring框架中的控制反转和依赖注入
-
实例讲解Java的Spring框架中的控制反转和依赖注入
-
Spring依赖注入的两种方式(根据实例详解)
-
Spring依赖注入的两种方式(根据实例详解)
-
spring 控制反转与依赖注入原理-学习笔记 博客分类: spring-->>备忘笔记AOPIOC springAOPIOC控制反转依赖注入
-
spring--依赖注入 博客分类: ssh2 spring依赖注入控制反转
-
Spring 的核心机制:依赖注入(控制反转) 博客分类: Spring IoC DI Spring依赖注入DI控制反转IoC