当前位置: 首页 > news >正文

学习springboot-Bean管理(Bean 注册,Bean 扫描)

Bean 扫描

可以浏览下面的博客链接 :spring 学习 (注解)-CSDN博客

在学习spring 注解时,我们使用 @Component ,@Service,@Controller等 这样的注解,将目标类信息,传递给IOC容器,为其创建对象


那时候扫描,解析为目标类创建对象的注解 有两者办法

1 spring 配置文件 中 使用<context :component-scan >标签 ,在标签,表明需要扫描哪一个包下的注解,明确 包名。

<context:component-scan base-package="fs">
</context:component-scan>

2 使用注解 扫描

// @Configuration 注解表示当前类是一个配置类,用来代替xml配置文件
@Configuration
// @ComponentScan 注解表示扫描包,用来代替xml配置文件
// basePackages 属性表示扫描的包
@ComponentScan(basePackages ="fs.exerise")

学习 springBoot 时,发现我们同样也需要使用很多注解像@Mapper,@RestController,@RequstMapping等... 


思考

1springboot 是spring 家族中的,按理来说也应该存在 扫描注解的。比如@ComponentScan ?

答:在springboot 中确实存在 扫描 ,解析的注解。只是被隐藏在 @SpringBootApplication 组成注解中

启动类

鼠标点击, @SpringBootApplication 组成注解 按ctrl+b  ,看到 该注解的底层代码


demo(案例)

验证 :使用的注解的可以被扫描的范围是启动类所在包及其子包

  • 启动类所在包在 springboot01 包下

  • 运行截图


修改:移动 controller 包到 heima 包下和 springboot 01包平级

发现:controller 包扫描不到。因此报了一个404 错误 !表示服务器 无法访问


解决办法

扩大 扫描范围,将范围扩展到heima 包下

运行截图


总结

1 在springboot 项目中,我们只管写各自注解 ,至于被使用的注解的扫描,解析过程,将交由springboot 自动完成,不需要我们人为操作。

2 springboot 项目中可以被扫描的范围是 默认是 启动类所在包及其子包


Bean 注册

目的:这里主要研究第三方 jar 对象【非自定义 对象】怎么注入到loc容器

看到这里,我们知道在学习 spring 时使用注解来注册bean 有:

思考

1 把三方jar 包 bean 对象注入到loc容器 使用这些注解 是否可行呢?

举例:打开springboot项目的外部库,任意一个 字节码文件,都是只读的。你无法在该类上使用上面说的 bean 注册的注解。

解决办法

1 @Bean 注解

功能:将目的对象以方法返回值的形式,传递给ioc 容器

@Bean 的使用

1 @Bean 默认方法名就是bean的id 将方法的返回值传递给ioc 容器

demo(案例)

目的:使用@ Bean 注解,将Country 对象传递给spring 容器

项目准备

1  使用 黑马视频教学使用的第三方jar 包 ,通过网盘分享的文件:02_Bean注册资料.rar
链接: https://pan.baidu.com/s/1rYC0mgIp_eWvdD6h2k015Q 提取码: mdpv

2 解压 文件

3 打开 本地仓库文件 修改jar包的所在路径

4 打开命令行在确保maven 环境配置好的情况下,运行该命令

  • 把这个jar 包下载到本地仓库

5 打开自己的本地仓库,可以看到 已经下好的jar包

6 打开IDEA 构建springboot 项目,在pom 文件添加已经下载的jar包坐标,最好加载成功

        <dependency><groupId>cn.itcast</groupId><artifactId>common-pojo</artifactId><version>1.0</version></dependency>

7 使用@ Bean 注解,将Country ,Provice对象传递给spring 容器

正式步骤

1 一个config 包,创建ComonConfig 类作为配置类

package com.it.heima.springboot03.config;import cn.itcast.pojo.Country;
import cn.itcast.pojo.Province;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class ComonConfig {@Beanpublic Country getCountry(){return new Country();}@Beanpublic Province getProvince() {return new Province();}
}

2 启动类中,调用getBean方法,id为默认的方法名

package com.it.heima.springboot03;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class Springboot03Application {public static void main(String[] args) {ApplicationContext run = SpringApplication.run(Springboot03Application.class, args);System.out.println(run.getBean("getCountry"));System.out.println("--------------------------------");System.out.println(run.getBean("getProvince"));}}

3 运行截图

  • 发现 Country 对象,Province 对象已经创建成功


2 @import 注解

使用方式

1 导入配置类

问题: 假设 我们把之前创建的配置类移动到启动类所在包之外,再一次运行代码很显然因为扫描不到配置类,从而无法从ioc容器 获得 对象,而报错

解决办法:在启动类 使用@import 注解,注解中写入 配置类的字节码文件


2 导入ImportSelector 接口实现类

应用场景:如果存在许多 配置类需要导入

  • CommonImportSelector 接口实现类

  • 启动类中使用 @Import 注解 ,注解中使用接口实现类的字节码文件

package com.it.heima.springboot03;import com.it.heima.config.ComonConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;//@Import(ComonConfig.class)
@Import(CommonImportSelector.class)
@SpringBootApplication
public class Springboot03Application {public static void main(String[] args) {ApplicationContext run = SpringApplication.run(Springboot03Application.class, args);System.out.println(run.getBean("getCountry"));System.out.println("--------------------------------");System.out.println(run.getBean("getProvince"));}}
  • 运行截图


更新

在ImportSelector 接口实现类 中把配置类的权限定类名存储在 配置文件

package com.it.heima.springboot03;import com.it.heima.config.ComonConfig;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;public class CommonImportSelector implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {InputStream resourceAsStream = CommonImportSelector.class.getClassLoader().getResourceAsStream("config.properties");BufferedReader in = new BufferedReader(new InputStreamReader(resourceAsStream));String line = null;List<String> list = new ArrayList<>();try {while ((line = in.readLine()) != null) {list.add(line);}} catch (Exception e) {e.printStackTrace();}finally {if (in != null){try {in.close();} catch (Exception e) {e.printStackTrace();}}}return list.toArray(new String[0]);}
}

运行截图


修改启动类中的 @Import(CommonImportSelector.class),把它试图变成 组合注解

  • 如下图所示 allCommon组合注解 具备以下三个注解的所有功能
package com.it.heima.springboot03;import org.springframework.context.annotation.Import;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// 作用在类上
@Target({ElementType.TYPE})
// 保留到运行时
@Retention(RetentionPolicy.RUNTIME)
@Import(CommonImportSelector.class)
public @interface allCommon {
}

启动类中

运行截图


http://www.mrgr.cn/news/94260.html

相关文章:

  • ESP-IDF ubuntu版本 V5.2
  • react实现一个列表的拖拽排序(react实现拖拽)
  • Kotlin和Java区别
  • 达梦主备集群部署
  • 阿里云操作系统控制台评测:国产AI+运维 一站式运维管理平台
  • ROS实践(四)机器人SLAM建图(gmapping)
  • 推理框架SGLang安装与调试
  • LVS + Keepalived 高可用集群
  • 《YOLOE: Real-Time Seeing Anything》论文速览翻译,支持文本提示,视觉提示等开放世界检测算法!
  • Java常见的并发设计模式
  • maven wrapper的使用
  • 爬虫中一些有用的用法
  • Qt:绘图API
  • 【Pytorch Transformers Fine-tune】使用BERT进行情感分类任务微调
  • Selenium 自动化测试学习总结
  • 本地Git仓库搭建(DevStar)与Git基本命令
  • MySQL的安装与建表
  • PySide(PyQT)的mouseMoveEvent()和hoverMoveEvent()的区别
  • java中小型公司面试预习资料(四):微服务架构
  • Unity 封装一个依赖于MonoBehaviour的计时器(上) 基本功能