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 注入配置文件中的值

相关推荐
学长毕业设计11 小时前
基于SpringBoot的公益基金管理系统(源码+文档+讲解视频)
java·spring boot·后端
东小西11 小时前
【SAA实战】第 3 篇 · 工具调用全攻略:把业务能力交给 Agent 自己调度
java·后端·spring
东小西12 小时前
【SAA实战】第 4 篇 · Agent 短期记忆:saver 让 Agent 跨轮记得住(threadId 隔离)
java·后端·spring
许彰午12 小时前
22-DataCenter报文序列化
java·低代码·架构·状态模式
2601_9620652512 小时前
[MySQL] SQL优化之性能分析
java·sql·mysql
小范同学_12 小时前
JDK1.7 与 JDK1.8 HashMap 底层原理对比 + 数组并发扩容死循环详解
java·开发语言
予昊13 小时前
从零实现“在线五子棋对战“:WebSocket 实时通信 + 段位匹配
java·开发语言·网络·websocket
2601_9622035113 小时前
【SpringAI入门】初识SpringAI
java
weixin_4614085814 小时前
Mybatis-flex小记
java·开发语言·mybatis
2601_9621771314 小时前
【MySQL篇】聚合查询,联合查询
android·java·mysql