spring创建策略模式的两种方式(xml配置和纯注解)
来源:互联网 发布:模拟和网络监控的区别 编辑:程序博客网 时间:2024/06/05 04:37
策略模式是很好用的设计模式,spring实现策略模式更加简化了代码流程:
xml和纯注解都可以实现,但是纯注解更加方便,没有配置,更加方便:先看注解的方式:
公共接口被实现
package org.gyy.strategy.iface;public interface IContextStrategy {void say(String name);}
实现类:
package org.gyy.strategy.impl;import org.gyy.strategy.iface.IContextStrategy;import org.springframework.stereotype.Component;@Componentpublic class ALIContextStrategy implements IContextStrategy {@Overridepublic void say(String name) {System.out.println("阿里巴巴集团欢迎你:"+name);}}
package org.gyy.strategy.impl;import org.gyy.strategy.iface.IContextStrategy;import org.springframework.stereotype.Component;@Componentpublic class BDUContextStrategy implements IContextStrategy{@Overridepublic void say(String name) {// TODO Auto-generated method stubSystem.out.println("百度集团欢迎你:"+name);}}
package org.gyy.strategy.impl;import org.gyy.strategy.iface.IContextStrategy;import org.springframework.stereotype.Component;@Componentpublic class TENContextStrategy implements IContextStrategy {@Overridepublic void say(String name) {// TODO Auto-generated method stubSystem.out.println("腾讯集团欢迎你:"+name);}}
工厂:spring在对map注入时,会将类名作为key
package org.gyy.strategy;import java.util.HashMap;import java.util.Map;import javax.annotation.Resource;import org.gyy.strategy.iface.IContextStrategy;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class ContextStrategyFactory {@Autowiredprivate Map<String, IContextStrategy> contextStrategy = new HashMap<String,IContextStrategy>();public Map<String, IContextStrategy> getContextStrategy() {return contextStrategy;}public void setContextStrategy(Map<String, IContextStrategy> contextStrategy) {this.contextStrategy = contextStrategy;}public IContextStrategy doStrategy(String type){return this.contextStrategy.get(type);}}
测试:
package org.gyy.strategy;import javax.annotation.Resource;import org.gyy.baseTest.BaseJunit4Test;import org.gyy.strategy.iface.IContextStrategy;import org.junit.Test;public class StrategyTest extends BaseJunit4Test {@Resourceprivate ContextStrategyFactory strategyFactory;@Testpublic void testStrategy(){IContextStrategy doStrategy = strategyFactory.doStrategy("ALIContextStrategy");doStrategy.say("张三");}}
到这里就是全部的基本实现,如果有复杂的业务需求,可以组合实现具体的方法,
下面是xml的配置,没增加一个实现类就要在map加一个键值对,特别的麻烦:
<bean id="contextStrategyFactory" class="org.gyy.strategy.ContextStrategyFactory"> <property name="contextStrategy"> <map> <entry key="1" value-ref="aLIContextStrategy"/> <entry key="2" value-ref="bDUContextStrategy"/> <entry key="3" value-ref="tENContextStrategy"/> </map> </property> </bean> <bean id="aLIContextStrategy" class="org.gyy.strategy.impl.ALIContextStrategy"/> <bean id="bDUContextStrategy" class="org.gyy.strategy.impl.BDUContextStrategy"/> <bean id="tENContextStrategy" class="org.gyy.strategy.impl.TENContextStrategy"/>
阅读全文
0 0
- spring创建策略模式的两种方式(xml配置和纯注解)
- spring aop注解和xml两种方式的基础
- spring系列(二)——AOP的两种实现方式(xml和注解)
- 关于mybatis两种连接mysql的方式(注解和xml配置)
- spring中配置Bean的xml方式和注解方式
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- spring3.1xml配置和注解两种方式总结
- spring 配置的两种方式:JAVA配置和注解配置
- 【Spring aop】Spring aop的XML和注解的两种配置实现
- 实例说明Spring实现AOP的2种方式(注解和XML配置)
- Spring IOC 依赖注入的两种方式XML和注解
- Spring IOC 依赖注入的两种方式:XML和注解
- Spring IOC 依赖注入的两种方式XML和注解
- Spring IOC 依赖注入的两种方式XML和注解
- Spring IOC 依赖注入的两种方式XML和注解
- Spring框架实现AOP的xml和注解两种方式
- linux 学习笔记(一)——文件操作
- JavaScript中函数调用
- 第5章 开始Unity Shader 学习之旅
- hdu 2586 How far away ?
- 【踩坑后总结】如何在Windows下安装配置MongoDB
- spring创建策略模式的两种方式(xml配置和纯注解)
- JavaScript的全局变量和局部变量
- Android自定义view之事件传递机制
- 配置PHP运行环境(xampp+tomcat)
- 不要试图强制你的下属写代码风格和你一个模样
- 三种方法实现生产者/消费者模型(Java)
- How Tomcat Works之(可接受servlet请求)
- 环境变量path中,加载顺序,先加在配置在最前面的,如果找到不继续往下寻找。
- SpringMVC测试