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

相关推荐
ChillJavaGuy24 分钟前
常见限流算法详解与对比
java·算法·限流算法
寻星探路32 分钟前
数据库造神计划第六天---增删改查(CRUD)(2)
java·大数据·数据库
毕设源码-朱学姐1 小时前
【开题答辩全过程】以 4S店汽车维修保养管理系统为例,包含答辩的问题和答案
java·spring boot·汽车
盖世英雄酱581361 小时前
Read timed out问题 排查
java·数据库·后端
狼爷2 小时前
破解 JetBrains 的学生,后来都成了它的 “推销员”:一场用习惯换市场的长期战
java·jetbrains
.豆鲨包2 小时前
【Android】Viewpager2实现无限轮播图
android·java
BXCQ_xuan2 小时前
软件工程实践二:Spring Boot 知识回顾
java·spring boot·后端
老赵的博客2 小时前
c++ unqiue指针
java·jvm·c++
wuxuanok2 小时前
SpringBoot -原理篇
java·spring boot·spring
柿蒂2 小时前
从if-else和switch,聊聊“八股“的作用
android·java·kotlin