Spring @Value 注入集合List、Set、Map

Spring @Value 注入集合List、Set、Map,第1张

Spring @Value 注入集合List、Set、Map

《Spring-IOC》中基于注解方式实现属性注入提到注解@Value,但只演示了注入普通类型属性。本文演示下注入集合List、Set、Map。

固定值

UserService:

package springIocTest.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Set;

@Service
public class UserService {
    @Value("lili")
    public String name;
    @Value("foo,bar,fun")
    public Set set;
    @Value("foo,bar,fun")
    public List list;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public Set getSet() { return set; }
    public void setSet(Set set) { this.set = set; }
    public List getList() { return list; }
    public void setList(List list) { this.list = list; }
}

程序调用:

public class springTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("springIocTest/applicationContext.xml");

        UserService userService = context.getBean("userService",UserService.class);
        System.out.println(userService.getName());
        System.out.println(userService.getList());
        System.out.println(userService.getSet());
    }
}

运行结果:

引入外部属性文件动态赋值

1、applicationContext.xml:




    
    
    
    

2、test.properties:

test.name=lili
test.set=foo,bar
test.list=foo,bar

3、UserService:

package springIocTest.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Set;

@Service
public class UserService {
    @Value("${test.name}")
    public String name;
    @Value("${test.set}")
    public Set set;
    @Value("${test.list}")
    public List list;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public Set getSet() { return set; }
    public void setSet(Set set) { this.set = set; }
    public List getList() { return list; }
    public void setList(List list) { this.list = list; }
}

程序调用不变,运行结果:

注入对象集合

接口IPayService:

package springIocTest.service;

public interface IPayService {
    public void doSomeService();
}

实现类PayServiceImpl1:

package springIocTest.service;
import org.springframework.stereotype.Component;

@Component
public class PayServiceImpl1 implements IPayService {
    @Override
    public void doSomeService() {
        System.out.println("do Service1");
    }
}

实现类PayServiceImpl2:

package springIocTest.service;
import org.springframework.stereotype.Component;

@Component
public class PayServiceImpl2 implements IPayService{
    @Override
    public void doSomeService() {
        System.out.println("do Service2");
    }
}

然后将接口注入类PayInvoker:

package springIocTest.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import springIocTest.service.IPayService;

import java.util.List;
import java.util.Map;

@Component
public class PayInvoker {

    @Autowired
    private List list;

    @Autowired
    private Map map;

    @Autowired
    @Qualifier("payServiceImpl1")
    private IPayService payService;

    public void fun() {
        if (null != list && 0 != list.size()) {
            System.out.println("list...");
            for (IPayService service : list) {
                System.out.println(service.getClass().getName());
            }
        } else {
            System.out.println("List list is null !!!!!!!!!!");
        }

        if (null != map && 0 != map.size()) {
            System.out.println("map...");
            for (Map.Entry entry : map.entrySet()) {
                System.out.println(entry.getKey() + "=" + entry.getValue().getClass().getName());
            }
        } else {
            System.out.println("Map map is null !!!!!!!!!!");
        }

        if (null != payService) {
            System.out.println(payService.getClass().getName());
        } else {
            System.out.println("payService is null...");
        }
    }
}

程序调用:

package springIocTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import springIocTest.pojo.PayInvoker;

public class springTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("springIocTest/applicationContext.xml");

        PayInvoker payInvoker = context.getBean("payInvoker",PayInvoker.class);
        payInvoker.fun();
    }
}

运行结果:

对于@Autowired声明的数组、集合类型,Spring会把容器中所有类型与集合(数组)中元素类型相同的bean构造出一个对应集合,注入到目标bean中。
比如Spring会把接口IPayService的所有实现类注入private List list;。

对于集合map的注入,map的key注入该bean的id,value注入该bean对象。

对于类对象注入,@Qualifier(“payServiceImpl1”)指定注入对象的id 。

其实,注入集合类型不要使用@Autowired,而使用@Resource注解。同时Spring官方也是不推荐使用@Autowired的。
PayInvoker:

...
@Component
public class PayInvoker {

    @Resource
    private List list;

    @Resource
    private Map map;

    @Resource(name  = "payServiceImpl1")
    private IPayService payService;

    public void fun() {
        ...
    }
}

运行结果:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5682198.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存