Spring multi object introduction method

  • 2021-06-28 09:19:17
  • OfStack

Previously, when using xml configuration injection, you could use either name name injection or type type injection.

In SpringBoot, you can use the @Resource and @Autowried annotations for injection.

@Resource is injected with a name by default, and if it cannot be found, it is injected with an automatic use type.

@Autowried, the matching object is found in the container, the injection succeeds if it is found, or an error if no or more objects are found.

However, if there are multiple cases, you can use @Qualifier ("alias") for constraints.

1. Create Bean

1.1 Add an name value to @Bean if the object is injected using method bean.

@Bean (name="alias")

Code sample:

Be careful

The following @Bean (name = "test01DataSource") can be obtained using applicationContext.getBean ("test01DataSource").

@Primary indicates that the Bean is preferred if the selection is of type type.

That is, the same object is obtained using applicationContext.getBean (DataSource.class) and applicationContext.getBean ("test01DataSource").

Source code:


@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();
  }
}

Test code:


@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());
}

Sample results:

1105282397
1105282397
793657559

1.2 Inject objects using class annotations. @Service, @Component, add an value value to create an alias.


//  Create Interface 
public interface IBeanService {
  public void printInfo();
}
// Create an instance 01,  And add @Primary annotation ,  This class is used by default for table names 
@Service(value = "bean01")
@Primary
public class Bean01Service implements IBeanService {
  @Override
  public void printInfo() {
    System.out.println("This is bean 01");
  }
}
// Create an instance 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. Call

2.1. If you need to create a local variable, create it using applicationContext.getBean (class/name)

For objects with @Primary, call directly using getBean (class).

applicationContext.getBean(IBeanService.class)

For other Beans, use getBean (name) to make the call and cast the type.

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

2.2. If you need to create member variables, use @Autowired and @Qualifier ("alias")

For objects with @Primary, call directly with @Autowired. The code is as follows


@Autowired
IBeanService beanService;

For other beans, call by adding @Qualifier ("alias"), code as follows


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

summary


Related articles: