Spring Map依赖注入

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值