spring —— IoC容器(二)

二、基于注解管理 bean

(一)Autowired 自动装配

(1)属性上自动装配

java 复制代码
package com.fourth.anno;

import org.springframework.stereotype.Component;

@Component
public class Dao {
    public void runDao(){
        System.out.println("hello world");
    }
}
java 复制代码
package com.fourth.anno;

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

@Component
public class Service {
    @Autowired
    private Dao dao;
    public void runService(){
        dao.runDao();
    }
}
java 复制代码
package com.fourth.anno;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean-anno.xml");
        Service service = context.getBean(Service.class);
        service.runService();
    }
}
XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <context:component-scan base-package="com.fourth.anno"></context:component-scan>
</beans>

(2)set 方法上自动装配

java 复制代码
package com.fourth.anno;

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

@Component
public class Service {    
    private Dao dao;

    @Autowired
    public void setDao(Dao dao){
        this.dao=dao;
    }

    public void runService(){
        dao.runDao();
    }
}

(3)构造方法上自动装配

java 复制代码
package com.fourth.anno;

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

@Component
public class Service {

    private Dao dao;
    @Autowired
    public Service(Dao dao) {
        this.dao = dao;
    }

    public void runService(){
        dao.runDao();
    }
}

(4)构造方法的形参上自动装配

java 复制代码
package com.fourth.anno;

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

@Component
public class Service {

    private Dao dao;

    public Service(@Autowired Dao dao) {
        this.dao = dao;
    }

    public void runService(){
        dao.runDao();
    }
}

(二)Resource 自动装配

(1)属性上自动装配

java 复制代码
package com.fourth.anno;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class Service {
    @Resource
    private Dao dao;

    public void runService(){
        dao.runDao();
    }
}

(2)set 方法上自动装配

java 复制代码
package com.fourth.anno;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class Service {

    private Dao dao;
    @Resource
    public void setDao(Dao dao) {
        this.dao = dao;
    }

    public void runService(){
        dao.runDao();
    }
}

Autowired 与 Resource 比较:

Autowired 可以应用在属性、set 方法、构造方法、构造方法的形参上;其默认通过类型进行装配。

Resource 可以应用在属性、set 方法上;其默认通过名称进行装配。

Autowired 基于 spring 框架;Resource 基于 java 扩展包,如果 JDK 版本高于11或低于8,应引入以下依赖:

复制代码
<dependency>
    <groupId>jakarta.annotation</groupId>
    <artifactId>jakarta.annotation-api</artifactId>
    <version>2.1.1</version>
</dependency>

(三)全注解开发

全注解开发,即使用配置类代替 xml 配置文件:

java 复制代码
package com.fourth.anno;

import org.springframework.stereotype.Component;

@Component
public class Dao {
    public void runDao(){
        System.out.println("hello world");
    }
}
java 复制代码
package com.fourth.anno;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class Service {

    private Dao dao;
    @Resource
    public void setDao(Dao dao) {
        this.dao = dao;
    }

    public void runService(){
        dao.runDao();
    }
}
java 复制代码
package com.fourth.anno;

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

@Configuration
@ComponentScan(value = "com.fourth.anno")
public class SpringConfig {
}
java 复制代码
package com.fourth.anno;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        Service service = context.getBean(Service.class);
        service.runService();
    }
}
相关推荐
午安~婉3 分钟前
ESLint
前端·eslint·检查
“抚琴”的人4 分钟前
C#中获取程序执行时间
服务器·前端·c#
秉承初心11 分钟前
Java 23种设计模式的详细解析
java·设计模式
掘金一周14 分钟前
Flex 布局下文字省略不生效?原因其实很简单| 掘金一周 10.16
前端
千码君201616 分钟前
Go语言:记录一下Go语言系统学习的第一天
java·开发语言·学习·golang·gin·并发编程·编译语言
聪明的笨猪猪18 分钟前
Java 面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
Stringzhua19 分钟前
Vue的Axios介绍【9】
前端·javascript·vue.js
MrSYJ36 分钟前
学完涨工资的技巧2:Spring Authorization Server如何签发JWTToken
java·spring boot·微服务
摸着石头过河的石头36 分钟前
JavaScript 垃圾收集:内存管理的艺术
前端·javascript
前端小崽子37 分钟前
🔥 踩坑实录:Fabric 在 Intel Iris Xe 显卡上 CPU 飙升 100%
前端