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;
}
相关推荐
G_whang13 小时前
win10环境下jdk17下载安装及环境配置
java
非情剑13 小时前
Java-Executor线程池配置-案例2
android·java·开发语言
小张快跑。13 小时前
【Java企业级开发】(十)SpringBoot框架+项目实践
java·数据库·spring boot
夏小花花13 小时前
<editor> 组件设置样式不生效问题
java·前端·vue.js·xss
PieroPC13 小时前
用 nicegui 3.0 + sqlite3 做个简单博客
前端·后端
weixin_3077791314 小时前
Jenkins Ioncions API 插件:现代化图标库在持续集成中的应用
java·运维·开发语言·前端·jenkins
AnAnCode14 小时前
【时间轮算法】时间轮算法的详细讲解,从基本原理到 Java 中的具体实现
java·开发语言·算法·时间轮算法
Java天梯之路14 小时前
Spring IOC 核心源码解析
java·spring·源码
天草二十六_简村人14 小时前
jenkins打包制作Python镜像,并推送至docker仓库,部署到k8s
后端·python·docker·容器·kubernetes·jenkins
JIngJaneIL14 小时前
基于Java二手交易管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot