Spring Map依赖注入
来源:互联网 发布:酷鱼网淘宝店 编辑:程序博客网 时间:2024/04/28 11:06
package com.liyang;public interface Eat { void toSay() ; }
package com.liyang;import org.springframework.stereotype.Service;@Servicepublic class HuoGuoEatImpl implements Eat{ @Override public void toSay() { System.out.println("老李在吃火锅!") ; }}
package com.liyang;import org.springframework.stereotype.Service;@Servicepublic class KfcEatImpl implements Eat{ @Override public void toSay() { System.out.println("老李在吃肯德基!") ; }}
package com.liyang;import org.springframework.stereotype.Service;@Servicepublic 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;@Servicepublic 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;@Servicepublic 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>
0 0
- Spring Map依赖注入
- Spring依赖注入properties,Map,List
- spring的list,set,map的依赖注入
- Spring依赖注入:注解注入
- spring 的依赖注入
- Spring依赖注入实践经验
- Spring的依赖注入
- Spring依赖注入
- spring(依赖注入-DI)
- spring依赖注入
- spring依赖注入
- 白话spring依赖注入
- Spring依赖注入方式
- Spring的依赖注入
- Spring依赖注入
- Spring依赖注入方式
- Spring 依赖注入
- spring依赖注入方式
- mybatis3与spring整合之事务
- idea构建项目
- Android NDK 开发 第一步的几个坑
- IOS--UITableViewController及UITableView
- html空格
- Spring Map依赖注入
- hdu 2500 做一个正气的杭电人
- 【转】macos下取消Android File Transfer自动启动
- request对象和response对象
- 关于sql语句的优化
- hibernate的基本数据类型
- android 自定义 View
- x86,amd64,x86-64,x64区别
- table中的td限制宽度width也不能让字符过长变成省略号生效?