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;
}
相关推荐
快乐肚皮5 分钟前
基于Spring Cloud Sentinel自研Slot扩展实战
java·spring cloud·sentinel
快乐肚皮7 分钟前
Spring Cloud Sentinel 快速入门与生产实践指南
spring·spring cloud·sentinel
zc.z11 分钟前
sentinel核心原理-高频问题
java·开发语言
夏季疯25 分钟前
学习笔记:黑马程序员JavaWeb开发教程(2025.4.6)
java·笔记·学习
chxii36 分钟前
8.1UDP点对点聊天小项目
java·网络·udp
犬余36 分钟前
告别Spring AI!我的Java轻量AI框架实践(支持多模型接入|注解式MCP架构|附开源地址)
java·人工智能·spring
sanx181 小时前
开发体育比分网站,有哪些坑需要注意的
java
Linging_242 小时前
Spring AI Alibaba集成阿里云百炼大模型应用
人工智能·spring·阿里云
gyeolhada2 小时前
2025蓝桥杯JAVA编程题练习Day8
java·数据结构·算法·蓝桥杯
JANYI20182 小时前
一文读懂----Docker 常用命令
java·docker·eureka