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

相关推荐
Geek攻城猫几秒前
Java 实现大文件上传与断点续传:原理、实践与优化
java
Kratzdisteln3 分钟前
【1902】自适应学习系统 - 完整技术方案
java·python·学习
橘橙黄又青17 分钟前
Spring篇
java·后端·spring
JaredYe17 分钟前
node-plantuml-2:革命性的纯Node.js PlantUML渲染器,告别Java依赖!
java·开发语言·node.js·uml·plantuml·jre
IT 行者19 分钟前
Spring Security 7 之 OIDC /connect/userinfo 端点解析:ID Token 与用户信息获取
linux·服务器·spring
hhzz20 分钟前
Springboot项目中使用EasyPOI方式导出合同word文档
java·spring boot·后端·word·poi·easypoi
派大鑫wink21 分钟前
【Day38】Spring 框架入门:IOC 容器与 DI 依赖注入
java·开发语言·html
爱丽_21 分钟前
Spring Bean 管理与依赖注入实践
java·后端·spring
独自破碎E23 分钟前
什么是Spring Bean?
java·后端·spring
手握风云-25 分钟前
JavaEE 进阶第十期:Spring MVC - Web开发的“交通枢纽”(四)
前端·spring·java-ee