4-3 Spring Bean装配之Autowired注解说明-2

@Autowired

  • 可以使用@Autowired注解那些众所周知的解析依赖性接口,比如:BeanFactory,ApplicationContext,Environment,ResourceLoader,ApplicationEventPublisher,and MessageSource
  • 可以通过添加注解给需要该类型的数组的字段或方法,以提供ApplicationContext中的所有特定类型的bean
  • 可以用于装配key为String的Map
  • 如果希望数组有序,可以让bean实现org.springframework.core.Ordered接口或使用的@Order注解

@Autowired使用时需要注意的点

  • @Autowired是由Spring BeanPostProcessor处理的,所以不能在自己的BeanPostProcessor或BeanFactoryPostProcessor类型应用这些注解,这些类型必须通过XML或者Spring的@Bean注解加载

数组及Map的自动注入

  • 例子:
    在这里插入图片描述
package com.torey.beanannotation.multibean;
/**
 * @InterfaceName:BeanInterface 用途:
 * 作者: litaofeng
 * 时间: 2019/8/25  22:22
 */
public interface BeanInterface {
}
package com.torey.beanannotation.multibean;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @ClassName:BeanInterfaceOne
 * @Description:
 * @author: Torey
 */
@Order(value = 2) //排序
@Component
public class BeanInterfaceOne implements BeanInterface {
}

package com.torey.beanannotation.multibean;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @ClassName:BeanInterfaceTwo
 * @Description:
 * @author: Torey
 */
@Order(value = 1)
@Component
public class BeanInterfaceTwo implements BeanInterface {
}

package com.torey.beanannotation.multibean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName:BeanInvoker
 * @Description:
 * @author: Torey
 */
@Component
public class BeanInvoker {
    @Autowired
    private List<BeanInterface> list;
    @Autowired
    private Map<String,BeanInterface> map;
    public void say(){
        if (null!=list&&0!=list.size()) {
            System.out.println("list===");
            for (BeanInterface bean : list) {
                System.out.println(bean.getClass().getName());
            }
        }else {
            System.out.println("List<BeanInterface> 没有被注入");
        }
        System.out.println("map===");
        if (null!=map&&0!=map.size()) {
            for (Map.Entry<String, BeanInterface> entry : map.entrySet()) {
                System.out.println(entry.getKey() + ": " + entry.getValue().getClass().getName());
            }
        }else {
            System.out.println("map 没有被注入");
        }
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.torey.beanannotation"/>
</beans>
package com.torey.springtest.bean;

import com.torey.beanannotation.injection.service.InjectionService;
import com.torey.beanannotation.multibean.BeanInvoker;
import com.torey.springtest.base.UnitTestBase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

/**
 * @ClassName:TestResource
 * @Description:
 * @author: Torey
 */
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase {
    public TestBeanAnnotation(){
        super("classpath:spring-beanannotation.xml");
    }
    @Test
    public void testMultBean(){
        BeanInvoker invoker= (BeanInvoker) super.getBean("beanInvoker");
        invoker.say();
    }
}

根据慕课网 moocer老师的spring课程 整理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值