springboot常用注解2

发布于:2024-04-29 ⋅ 阅读:(23) ⋅ 点赞:(0)

@Qualifier

public class QuaBean {

    private String name;

    public QuaBean(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
}

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuaConfig {

    @Bean
    public QuaBean quaBeanA() {
        return new QuaBean("a");
    }
    
    @Bean
    public QuaBean quaBeanB() {
        return new QuaBean("b");
    }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class QuaApp {

    public static void main(String[] args) {
        SpringApplication.run(QuaApp.class, args);
    }

    @Autowired
    @Qualifier("quaBeanA")
    QuaBean bean;
    
    @GetMapping("/qua")
    public String testGetBean() {
        System.out.println(bean.getName());
        return "";
    }
}

自定义限定注解

public class AnimalBean {
    
    private String name;

    public AnimalBean(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    

}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;
/**
 * 
 * @ElementType.CONSTRUCTOR :用于描述构造器。
  ● ElementType.FIELD :成员变量、对象、属性(包括enum实例)。
  ● ElementType.LOCAL_VARIABLE: 用于描述局部变量。
  ● ElementType.METHOD : 用于描述方法。
  ● ElementType.PACKAGE :用于描述包。
  ● ElementType.PARAMETER :用于描述参数。
  ● ElementType.ANNOTATION_TYPE:用于描述参数
  ● ElementType.TYPE :用于描述类、接口(包括注解类型) 或enum声明
  
  
    RetentionPolicy.SOURCE : 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解。
  ●   RetentionPolicy.CLASS : 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式。
  ●   RetentionPolicy.RUNTIME : 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。
 *
 */
@Target({ ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface AnimalQualifier {

    String type();
}

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CustomConfig {

    @Bean
    @AnimalQualifier(type = "person")
    public AnimalBean personBean() {
        return new AnimalBean("angus"); // 动物类型为person,名称为 angus
    }
    
    @Bean
    @AnimalQualifier(type = "cat")
    public AnimalBean catBean() {
        return new AnimalBean("tom"); // 动物类型为cat,名称为 tom
    }
}

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class CustomApp {

    public static void main(String[] args) {
        SpringApplication.run(CustomApp.class, args);
    }

    @Autowired
    @AnimalQualifier(type = "person")
    private AnimalBean person;
    
    @GetMapping("/cus")
    public String getAnm() {
        System.out.println(person.getName());
        return "";
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到