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 Framework基础上,Spring Security充分利用了依赖注入(DI,Dependency Injection)和面向切面技术。本实例简单配置了springsecurity权限控制,提供admin,user两个用户,在页面登录时以不同角色可访问不同...
Java_Spring 依赖注入简单实例
简易的spring demo,包含了bean自动装配,泛型依赖注入,Aop切面编程的实例
它是用Kotlin编写的,并使用Spring Boot进行依赖项注入和组件编排。 该应用程序的目标是通过将其包装在一个简单的案例中来演示一个简单的端到端Spring Boot应用程序。 它有一个主要的API端点,可用于检查面试候选人...
通过两个例子,说明了如何引用Spring中的依赖注入这一特性。 1.3 面向切面编程的实现 通过一个英勇的骑士的探险行动的示例设计,展示面向切面编程的具体实现。 1.4 使用模板消息样板式代码 主要是避免重复的代码,本...
SSM框架整合,使用最新版MyBatis-Jar包(Struts2.1+Spring3.0+MyBatis3.06) 下载之后执行.sql文件创建表和sequence ... 项目访问路径 localhost:8080/SSI_Demo...用户的增删改查操作,依赖注入和注解事物管理,适合初学者...
相比Spring的对象容器,它提供的功能更为精简常用(如对象延迟创建、对象属性自动注入等),降低了学习的难度,并且提供了很多扩展点,你只需简单地写一个插件实现类,并在配置文件中进行简单配置,就可以让Elite ...
(一) Spring 开发 Swing GUI 简介——依赖注入和富客户机 5 关于本教程 5 前提条件 6 Spring 和依赖注入概述 7 什么是 Spring 框架? 7 什么是依赖注入? 7 环境设置 7 选择编译环境 7 设置 Ant 8 通过 Ant 自动下载...
依赖注入框架,基于接口(Protocol)类似于Spring的一些功能 Spring中通过 java 的标注,实现对 module 的注入,在 OC 中需要手写 Module, 然后在类方法+load注入 module 建立起 Class 和 Interface 之间的关系,当使用...
体验.net2.0的优雅(四):Provider、策略、控制反转和依赖注入 泛型最佳实践 asp.net 2.0下嵌套masterpage页的可视化编辑 C# 2.0与泛型 动态调用对象的属性和方法——性能和灵活性兼备的方法 泛型技巧系列:用泛型...