网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么在Spring中引入多对象

本篇文章为大家展示了怎么在Spring中引入多对象,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

成都创新互联成立10年来,这条路我们正越走越好,积累了技术与客户资源,形成了良好的口碑。为客户提供成都网站建设、网站制作、网站策划、网页设计、域名与空间、网络营销、VI设计、网站改版、漏洞修补等服务。网站是否美观、功能强大、用户体验好、性价比高、打开快等等,这些对于网站建设都非常重要,成都创新互联通过对建站技术性的掌握、对创意设计的研究为客户提供一站式互联网解决方案,携手广大客户,共同发展进步。

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;

上述内容就是怎么在Spring中引入多对象,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


文章标题:怎么在Spring中引入多对象
链接地址:http://bjjierui.cn/article/goophp.html

其他资讯