Spring之注解实现依赖注入

1.使用@Autowired注解按类型自动装配引用数据类型

注:自动装配(按类型和名称)基于反射设计创建对象并暴力反射对应属性为私有属性初始化数据,因此无需提供setter方法。自动装配建议使用无参构造方法创建对象,如果不提供对应的构造方法,应提供唯一的构造方法

package domain;

import org.springframework.stereotype.Component;

@Component
public class Animal {
   
    private String name;
    private Integer age;
}

package domain;

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

@Component
public class People {
   
    @Autowired
    private Animal animal;

}

package config;

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

@Configuration
@ComponentScan("domain")
public class SpringConfig {
   
}

import config.SpringConfig;
import domain.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Demo {
   
    public static void main(String[] args) {
   
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        People people = ctx.getBean(People.class);
        System.out.println(people);

    }
}

2.使用@Autowired和@Qualifier注解按名称自动装配引用数据类型(不推荐使用)

注:@Qualifier注解无法单独使用,必须配合@Autowired注解使用

package domain;

import org.springframework.stereotype.Component;

@Component("animal")
public class Animal {
   
    private String name;
    private Integer age;
}

package domain;

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

@Component
public class People {
   
    @Autowired
    @Qualifier("animal")
    private Animal animal;

}

package config;

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

@Configuration
@ComponentScan("domain")
public class SpringConfig {
   
}

import config.SpringConfig;
import domain.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Demo {
   
    public static void main(String[] args) {
   
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        People people = ctx.getBean(People.class);
        System.out.println(people);

    }
}

3.使用@Value注解实现简单数据类型的注入

package domain;

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

@Component
public class Animal {
   
    @Value("maomi")
    private String name;
    @Value("123")
    private Integer age;
}

相关推荐

  1. Spring注解实现依赖注入

    2024-01-06 06:14:02       61 阅读
  2. spring(二):基于注解实现依赖注入

    2024-01-06 06:14:02       46 阅读
  3. Spring依赖注入的方式

    2024-01-06 06:14:02       52 阅读
  4. Spring 依赖注入

    2024-01-06 06:14:02       33 阅读
  5. Spring依赖注入

    2024-01-06 06:14:02       33 阅读
  6. Spring依赖注入原理与最佳实践

    2024-01-06 06:14:02       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-06 06:14:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-06 06:14:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-06 06:14:02       82 阅读
  4. Python语言-面向对象

    2024-01-06 06:14:02       91 阅读

热门阅读

  1. Tomcat

    Tomcat

    2024-01-06 06:14:02      55 阅读
  2. Tomcat调优

    2024-01-06 06:14:02       49 阅读
  3. Mabatis中String类型传参常见问题和解决办法

    2024-01-06 06:14:02       47 阅读
  4. SLB、DMZ、Nginx、Ingress、Gateway、Kibana和Grafana

    2024-01-06 06:14:02       64 阅读
  5. 大根堆小根堆

    2024-01-06 06:14:02       53 阅读