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;
}
相关推荐
6952 分钟前
两周浅学 RAG
后端
TeamDev31 分钟前
JxBrowser 9.0.0 版本发布啦!
java·前端·混合应用·jxbrowser·浏览器控件·跨平台渲染·原声输入
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题】【Java基础篇】第24题:Java面向对象有哪些特征
java·开发语言·后端·面试
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题】【Java基础篇】第25题:JDK1.8的新特性有哪些
java·开发语言·后端·面试
likerhood2 小时前
SLF4J: Failed to load class “StaticLoggerBinder“ 解决
java·log4j·maven
早日退休!!!2 小时前
大模型推理瓶颈七层分析模型
java·服务器·数据库
fliter2 小时前
Wrangler:Cloudflare 给 Rust + WASM 开发者造的那把锤子
后端
叶小鸡2 小时前
Java 篇-项目实战-天机学堂(从0到1)-day9
java·开发语言
兔子零10242 小时前
Ofox AI值得用吗?
前端·javascript·后端
@#¥&~是乱码鱼啦2 小时前
Spring分层架构:Controller、Service、Mapper数据链路,IOC的真实工作意义
java·spring·架构