当前位置: 移动技术网 > IT编程>开发语言>Java > Spring多对象引入方法

Spring多对象引入方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

在以前使用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;

总结

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网