springboot 属性注入(@Autowired、@Resource)

    技术2025-09-10  68


    springboot 属性注入(@Autowired、@Resource)

     

     

    ********************

    相关注解

     

    @Autowired:spring提供注解,根据类型注入属性

    @Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Autowired { /** * Declares whether the annotated dependency is required. * <p>Defaults to {@code true}. */ boolean required() default true; }

     

    @Qualifier:@Autowired注入时,若有多个同类型的bean,可使用该注解指定加载的bean的名称

    @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Qualifier { String value() default ""; }

     

     

    @Resource:java提供注解,可根据名称(默认)、类型注入属性

    @Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Repeatable(Resources.class) public @interface Resource { String name() default ""; //根据名称注入属性,默认使用名称注入 Class<?> type() default Object.class; //根据类型注入属性 String lookup() default ""; boolean shareable() default true; String mappedName() default ""; String description() default ""; Resource.AuthenticationType authenticationType() default Resource.AuthenticationType.CONTAINER; public static enum AuthenticationType { CONTAINER, APPLICATION; private AuthenticationType() { } } }

     

     

    ********************

    示例

     

    *****************

    pojo 层

     

    Person

    @Data public class Person { private String name; private Integer age; }

     

    *****************

    config 层

     

    WebConfig

    @Configuration public class WebConfig { @Bean("person") public Person initPerson(){ Person person=new Person(); person.setName("瓜田李下"); person.setAge(20); return person; } @Bean("person2") public Person initPerson2(){ Person person=new Person(); person.setName("瓜田李下 2"); person.setAge(21); return person; } }

     

    *****************

    controller 层

     

    HelloController

    @RestController public class HelloController { @Autowired @Qualifier("person") private Person person; @Autowired @Qualifier("person2") private Person person2; @RequestMapping("/hello") public String hello(){ System.out.println("helloController: "+person); System.out.println("helloController: "+person2); return "success"; } }

     

    Hello2Controller

    @RestController public class Hello2Controller { @Resource private Person person; @Resource private Person person2; @Resource(name = "person2") private Person person3; @RequestMapping("/hello2") public String hello(){ System.out.println("hello2Controller: "+person); System.out.println("hello2Controller: "+person2); System.out.println("hello2Controller: "+person3); return "success"; } }

     

    *****************

    控制台输出

     

    localhost:8080/hello

    helloController: Person(name=瓜田李下, age=20) helloController: Person(name=瓜田李下 2, age=21)

     

    localhost:8080/hello2

     

    hello2Controller: Person(name=瓜田李下, age=20) hello2Controller: Person(name=瓜田李下 2, age=21) hello2Controller: Person(name=瓜田李下 2, age=21)

     

     

    Processed: 0.009, SQL: 10