Spring Map依赖注入
2015-10-26 13:55
453 查看
package com.liyang; public interface Eat { void toSay() ; }
package com.liyang; import org.springframework.stereotype.Service; @Service public class HuoGuoEatImpl implements Eat{ @Override public void toSay() { System.out.println("老李在吃火锅!") ; } }
package com.liyang; import org.springframework.stereotype.Service; @Service public class KfcEatImpl implements Eat{ @Override public void toSay() { System.out.println("老李在吃肯德基!") ; } }
package com.liyang; import org.springframework.stereotype.Service; @Service public class DongBeiEatImpl implements Eat { @Override public void toSay() { System.out.println("老李在吃东北菜!") ; } }
@Autowired private Map<String, BaseService> map key是bean名字;value就是所有实现了BaseService的Bean
package com.liyang; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class EatFactory { @Autowired private Map<String , Eat> eat ; private final String suffix = "EatImpl" ; public enum Eater{ HUOGUO("huoGuo") , KFC("kfc") , DONGBEI("dongBei") ; private final String prefix ; private Eater(String prefix) { this.prefix = prefix ; } } public Eat getEater(Eater eater){ return eat.get(eater.prefix + suffix) ; } }
package com.liyang; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Service; import com.liyang.EatFactory.Eater; @Service public class Main { @Autowired public static EatFactory factory ; public static void main(String[] args) { String[] configLocations = { "applicationContext-resource.xml" }; ApplicationContext context = new ClassPathXmlApplicationContext(configLocations); factory = context.getBean("eatFactory" , EatFactory.class) ; //注意:首字母务必小写 factory.getEater(Eater.HUOGUO).toSay() ; factory.getEater(Eater.KFC).toSay() ; factory.getEater(Eater.DONGBEI).toSay() ; } }
输出:
老李在吃火锅!
老李在吃肯德基!
老李在吃东北菜!
<?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.liyang"/> </beans>
相关文章推荐
- mybatis3与spring整合之事务
- SpringMvc框架 MVC详细解说
- Spring3 MVC请求参数获取的几种方法
- Eclipse崩溃
- java.lang.NoClassDefFoundError: org/springframework/dao/support/DaoSupport:
- 用Java Servlets代替CGI
- myeclipse中xml文件格式的校验
- mybatis返回map类型数据空值字段不显示
- Java深度拷贝一个对象
- struts2的流程跳转
- java类集
- Java NIO中的Selector和IO复用
- JavaWeb的各种中文乱码终极解决方法
- 【Java基础】Java内部类
- 用Java访问带有Kerberos认证的HBase
- Java对象实例化的过程。
- java对象转化成String类型
- eclipse在线安装SVN
- struts2 之配置拦截器使网站页面在session失效后退回到登录页面
- Java核心技术第12章(4)