Spring之注解实现依赖注入

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

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

java 复制代码
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注解使用

java 复制代码
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注解实现简单数据类型的注入

java 复制代码
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;
}
相关推荐
lozhyf9 分钟前
能发弹幕的简单视频网站
java·spring boot·后端
SamDeepThinking10 分钟前
Cursor集成MCP MySQL服务器完整配置指南
后端·ai编程·cursor
微露清风10 分钟前
系统性学习数据结构-第三讲-栈和队列
java·数据结构·学习
AAA修煤气灶刘哥28 分钟前
ES 地理查询玩明白,产品要的 “附近的店” 再也难不倒我!(附 DSL+Java 实战)
java·后端·elasticsearch
雾里华29 分钟前
Spring AOP深度解析:从实现原理到最佳实践
spring
shark_chili38 分钟前
深入理解CPU缓存:编写高性能Java代码的终极指南
后端
十八旬1 小时前
苍穹外卖项目实战(day-5完整版)-记录实战教程及问题的解决方法
java·开发语言·spring boot·redis·mysql
m0_749299951 小时前
Nginx主配置文件
java·服务器·nginx
╭╰4021 小时前
苍穹外卖优化-续
java·spring·mybatis
金銀銅鐵1 小时前
[Java] 枚举常量的精确类型一定是当前枚举类型吗?
java·后端