Bean Annotations (Annotation) configuration (3) - configuration dependency injection


Spring series of tutorials


Notes configuration dependencies between the way Bean, through annotation: @Autowired.

Spring scan all with @Componentclass notes, and to register it as a bean, and then find the Spring with @Autowiredvariable comment, to rely Bean injection.

Use @Autowiredannotation dependency injection three ways:

  • Constructor injection
  • Setter Injection
  • Field injection

To ensure that the dependent class already configured Bean, otherwise it can not be injected.

We are the following categories:

Database.java


package com.qikegu.demo;

import org.springframework.stereotype.Component;

@Component
public class Database implements Service {
  
  @Override
  public String name() {
    return "数据库名称:MySQL";
  }
  
  @Override
  public void run() {
    System.out.println("数据库正在运行");
  }
  
  @Override
  public void stop() {
    System.out.println("数据库已经停止");
  }
}

Logger.java

package com.qikegu.demo;

import org.springframework.stereotype.Component;

@Component
public class Logger implements Service {
  @Override
  public String name() {
    return "Logger X";
  }
  
  @Override
  public void run() {
    System.out.println("Loggery已经准备好");
  }
  
  @Override
  public void stop() {
    System.out.println("Logger已经停止");
  }
}

Mail.java

package com.qikegu.demo;

import org.springframework.stereotype.Component;

@Component
public class Mail implements Service {
  @Override
  public String name() {
    return "邮件收发";
  }
  
  @Override
  public void run() {
    System.out.println("邮件收发正在运行");
  }
  
  @Override
  public void stop() {
    System.out.println("邮件收发已经停止");
  }
}

1. Constructor injection

Use @Autowiredannotation item constructor dependency injection class.

App.java

package com.qikegu.demo;

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

@Component
public class App {
  
  private Service mainService;
  private Service[] services;
      
  public App(){
  }
 
  /* 
  // 构造方法注入
  @Autowired
  public App(@Qualifier("logger") Service main){
    this.setMainService(main);
  } 
  */
  
  // 构造方法注入
  @Autowired
  public App(Service[] services){
    this.setServices(services);
  }
  
  public Service getMainService() {
    return mainService;
  }
  
  public void setMainService(Service mainService) {
    this.mainService = mainService;
  }
  
  public Service[] getServices() {
    return services;
  }
  
  public void setServices(Service[] services) {
    this.services = services;
  }
}

NOTE: Only one constructor for automatic assembly.

  • Question 1: You can see the parameter type constructor Serviceis an interface, it has several implementation classes, this instance of which will achieve class injecting it?

If only one implementation class, by default, will inject this class.

If there are multiple classes implemented, require the use of @Qualifier("bean_id")annotation explicitly specified. If not specified, Spring container will match the variable name according to an implementation class, if no match is found, an exception is thrown.

For public App(Service[] services)Service Array: Spring will achieve all instances of the class injection.

  • Question 2: Spring container in what way match Bean?

The default is the data type (the byType), when used @Qualifierduring annotation, by name (byName).

2. Setter Method Injection

Call with @Autowiredsetter methods annotated inject dependencies.

App.java

package com.qikegu.demo;

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

@Component
public class App {
 
  private Service mainService;
  private Service[] services;
 
  public App(){
  }
 
  public Service getMainService() {
    return mainService;
  }
  
  // Setter方法注入
  @Autowired
  @Qualifier("logger")
  public void setMainService(Service mainService) {
    this.mainService = mainService;
  }

  public Service[] getServices() {
    return services;
  }
 
  @Autowired 
  public void setServices(Service[] services) {
    this.services = services;
  }
}

In fact, with any @Autowiredmethod annotated, you can inject dependencies, not just Setter method.

// Setter方法注入
@Autowired
@Qualifier("logger")
public void anyMethod(Service mainService) {
  this.mainService = mainService;
}

3. field injection

Field on the object by using the @Autowiredinjection dependency annotation.

App.java

package com.qikegu.demo;

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

@Component
public class App {

  // 字段注入 
  @Autowired
  @Qualifier("logger") 
  private Service mainService;
  
  // 字段注入 
  @Autowired
  private Service[] services;
 
  public App(){
  }
 
  public Service getMainService() {
    return mainService;
  }

  public Service[] getServices() {
    return services;
  }
}
  • Question: This is mentioned several types of dependency injection, which is better?

It has its own advantages, to see their own preferences, choose a consistency throughout the project

By injecting annotation value

The method of using the constructor, methods, and fields in addition to the setter dependency injection, injection may also be a value.

App.java

package com.qikegu.demo;

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

@Component
public class App {
 
  // 使用@Value注解注入值
  @Value("1024")
  private int id;
  
  public App(){
  }
  
  // 使用@Value注解注入值
  public App(@Value("1024") int id){
    this.id = id;
  }
  
  public int getId() {
    return id;
  }
  
  // 使用@Value注解注入值
  @Value("1024")
  public void setId(int id) {
    this.id = id;
  }
}

Guess you like

Origin www.cnblogs.com/jinbuqi/p/10983456.html