Spring多对象引入方法

所属分类: 软件编程 / java 阅读数: 24
收藏 0 赞 0 分享

在以前使用xml配置注入的时候, 可以通过name名称注入, 也可以使用type类型注入.

在SpringBoot中, 可以使用@Resource和@Autowried注解进行注入.

@Resource 默认会使用名称进行注入,  如果找不到, 会使用自动使用类型进行注入.

@Autowried, 则会在容器中寻找匹配的对象, 如果找到则注入成功, 如果没找到或者找到多个, 则会报错.

但是, 如果有多个的情况下, 可以使用@Qualifier("别名") 进行约束.

1.创建Bean

1.1如果使用方法bean注入对象, 给@Bean添加name值.

@Bean(name="别名")

代码样例:

注意

下面的@Bean(name = "test01DataSource"), 可以使用applicationContext.getBean("test01DataSource") 进行获取.

@Primary表示 如果以type类型进行选择的话, 首选该Bean.

也即是说,使用applicationContext.getBean(DataSource.class) 和applicationContext.getBean("test01DataSource") 获取到的是同一个对象.

源码:

@Configuration
public class DataSourceConfig {
  @Bean(name = "test01DataSource")
  @Primary
  @ConfigurationProperties(prefix = "spring.datasource.test01")
  public DataSource getTest01DataSource() {
    return DataSourceBuilder.create().build();
  }
  @Bean(name = "test02DataSource")
  @ConfigurationProperties(prefix = "spring.datasource.test02")
  public DataSource test02DataSource() {
    return DataSourceBuilder.create().build();
  }
}

测试代码:

@Autowired
ApplicationContext applicationContext;
@Test
public void testDataSourceHashCode() {
  System.out.println(applicationContext.getBean(DataSource.class).hashCode());
  System.out.println((applicationContext.getBean("test01DataSource")).hashCode());
  System.out.println((applicationContext.getBean("test02DataSource")).hashCode());
}

结果样例:

1105282397
1105282397
793657559

1.2使用类注解进行注入对象. @Service, @Component,添加value值进行创建别名.

// 创建接口
public interface IBeanService {
  public void printInfo();
}
//创建实例01, 并且添加上@Primary注解, 表名默认使用这个类
@Service(value = "bean01")
@Primary
public class Bean01Service implements IBeanService {
  @Override
  public void printInfo() {
    System.out.println("This is bean 01");
  }
}
//创建实例02,
@Service(value = "bean02")
public class Bean02Service implements IBeanService {
  @Override
  public void printInfo() {
    System.out.println("This is bean 02");
  }
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class IBeanServiceTest {
  @Autowired
  ApplicationContext applicationContext;
// create default bean.
  @Autowired
  IBeanService beanService;
  @Autowired
  @Qualifier("bean01")
  IBeanService bean01Service;
  @Autowired
  @Qualifier("bean02")
  IBeanService bean02Service;
  @Test
  public void printInfo() {
  // create bean01
    IBeanService bean01 = (IBeanService) applicationContext.getBean("bean01");
  // create bean02
    IBeanService bean02 = (IBeanService) applicationContext.getBean("bean02");
    bean01.printInfo();
    bean02.printInfo();
  // create default bean, and it is bean01
    applicationContext.getBean(IBeanService.class).printInfo();
  }
  @Test
  public void printInfoThroughAutowired() {
    beanService.printInfo();
    bean01Service.printInfo();
    bean02Service.printInfo();
  }
}

2.调用

2.1.如果需要创建局部变量,  使用applicationContext.getBean(class/name)创建

对于有@Primary的对象, 直接使用getBean(class)进行调用.

applicationContext.getBean(IBeanService.class)

对于其他的Bean, 使用getBean(name)进行调用, 并进行类型强制转换.

eg. (IBeanService) applicationContext.getBean("bean02");

2.2.如果需要创建成员变量, 使用@Autowired和 @Qualifier("别名") 进行

对于有@Primary的对象, 直接使用@Autowired进行调用.代码如下

@Autowired
IBeanService beanService;

对于其他的bean, 通过添加@Qualifier("别名")进行调用, 代码如下

@Autowired
@Qualifier("bean02")
IBeanService bean02Service;

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

更多精彩内容其他人还在看

Collections工具类_动力节点Java学院整理

Collections工具类提供了大量针对Collection/Map的操作。这篇文章主要介绍了Collections工具类_动力节点Java学院整理,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringMVC集成Swagger实例代码

本篇文章主要介绍了SpringMVC集成Swagger实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

十大常见Java String问题_动力节点Java学院整理

本文介绍Java中关于String最常见的10个问题,需要的朋友参考下吧
收藏 0 赞 0 分享

Java微信公众平台开发(13) 微信JSSDK中Config配置

这篇文章主要为大家详细介绍了Java微信公众平台开发第十三步,微信JSSDK中Config配置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java实现一个达达租车系统的步骤详解

这篇文章主要给大家介绍了利用Java实现一个达达租车系统的步骤,文中给出了详细的实现思路和示例代码,并在文末给出了完整的源码供大家学习下载,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

Java微信公众平台开发(14) 微信web开发者工具使用

这篇文章主要为大家详细介绍了Java微信公众平台开发第十四步,微信web开发者工具的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Spring Boot整合RabbitMQ实例(Topic模式)

Topic Exchange 转发消息主要是根据通配符。接下来通过本文给大家分享Spring Boot整合RabbitMQ实例(Topic模式),需要的朋友参考下吧
收藏 0 赞 0 分享

Java微信公众平台开发(15) 微信JSSDK的使用

这篇文章主要为大家详细介绍了Java微信公众平台开发第十五步,微信JSSDK的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java多线程的同步方法实例代码

这篇文章主要介绍了 java多线程的同步方法实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

spring boot整合RabbitMQ实例详解(Fanout模式)

这篇文章主要介绍了spring boot整合RabbitMQ的实例讲解(Fanout模式),非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多