spring 注解

@Component

作用

复制代码
@Component注解的作用相当于 <bean id="bookDao" class="org.example.dao.impl.BookDaoImpl"/> 即:使用@Component后就不需要在xml中手动配置bean标签来实例化类了,但需要配合 <context:component-scan base-package="org.example"/> 标签使用

使用

案例

补充:xml中配置context空间

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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="org.example"/>
</beans>

@Component注解的三个衍生注解

  • @Controller:用于表现层bean定义
  • @Service:用于业务层bean定义
  • @Repository:用于数据层bean定义

案例

Spring 纯注解开发

作用

Spring3.8升级了纯注解开发模式,使用]ava类替代配置文件,开启了Spring快速开发赛道

使用

1.使用Java类代替Spring核心配置文件

2.使用@Configuration与@ComponentScan标签代替xml配置文件

3.读取Spring核心配置文件初始化容器对象切换为读取Java配置类初始化容器对象

说明:

@Configuration注解用于设定当前类为配置类

@Componentscan注解用于设定扫描路径,此注解只能添加一次,多个数据请用数组格式

案例

Spring纯注解下的bean作用域及生命周期

@Scope作用域

默认bean是单例模式,使用@Scope注解可控制单例非单例模式

  • singleton 单例 默认
  • prototype 非单例
java 复制代码
package org.example.dao.impl;

import org.example.dao.BookDao;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

@Repository
@Scope("prototype")
public class BookDaoImpl implements BookDao {
    public void save() {
        System.out.println("BookDaoImpl save");
    }
}

生命周期

  • @PostConstrct bean执行构造方法前
  • @RreDestroy bean销毁前

案例

java 复制代码
package org.example.dao.impl;

import org.example.dao.BookDao;
import org.springframework.stereotype.Repository;
import javax.annotation.PostConstrct;
import javax.annotation.RreDestroy;

@Repository
public class BookDaoImpl implements BookDao {
    public void save() {
        System.out.println("BookDaoImpl save");
    }
    @PostConstrct
    public void init() {
        System.out.println("BookDaoImpl init");
    }
    @RreDestroy
    public void destroy() {
        System.out.println("BookDaoImpl destroy");
    }
}

Sping 纯注解模式下的 自动装配(注入)

@Autowired

作用:相当于<property name="bookDao" ref="bookDao"/>

注意:

  • 自动装配基于反射设计创建对象并暴力反射对应属性为私有属性初始化数据,因此无需提供setter方法
  • 自动装配建议使用无参构造方法创建对象(默认),如果不提供对应构造方法,请提供唯一的构造方法
  • Autowired支持引用类型注入,值类型注入需要使用Value

案例

@Qualifier

案例

@Value

作用:相当于<property name="bookDao" value="《java入门》"/>

@PropertySource

@Value + @PropertySource 注入配置文件中的值

相关推荐
chuan.bai7 分钟前
Java RAG 实战附录:qwen3 与 bge-m3 模型切换指南
java·人工智能·算法
SL_staff9 分钟前
销售知识管理的断点分析与技术解法:从3天找话术到秒级检索
java·开源·github
荣码10 分钟前
向量数据库选型:4种方案全测了,选错重来成本最高
java·python
rannn_11112 分钟前
【力扣hot100】二叉树专题+总结
java·算法·leetcode·二叉树
摇滚侠37 分钟前
《Docker技术入门与实战 第4版》阅读笔记 6 使用 Dockerfile 创建镜像 2
java·笔记·docker
余额瞒着我当琳1 小时前
C++--vector第二讲:手写 C++ STL:vector 源码剖析与迭代器失效分析
android·java·c++
资源大佬星 课it1 小时前
黑马 Java+AI新版V16零基础就业班视频课程百度网盘下载
java·开发语言·人工智能
二十雨辰1 小时前
[Java]-微服务面试题
java·开发语言·微服务
我命由我123451 小时前
Compose Codelab 学习 - Compose 中的基本布局
android·java·java-ee·kotlin·android studio·android-studio·android runtime
莫得感情 o2 小时前
并发 11 · 同步工具类
java·并发