扫描 + 注解完成bean的自动配置
链接:https://pan.baidu.com/s/1W3TINXNnqpxmkIADOcJZCQ
提取码:fmt5
我们知道,我们一般是通过id或name调用getBean方法来从IOC容器中获取相应的bean对象
这样就带来一个问题,我们需要写application.xml文件来把bean写入配置文件中
这种方式属于手动配置,比较麻烦
接下来,我们使用扫描 + 注解的方式,完成bean的自动配置:
首先在配置文件application.xml中写入:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="top.bigking.bean"/>
</beans>
可以看到,我们使用了<context:component-scan>标签来进行自动扫描,扫描哪里呢? base-package的值就是指定相应的包,该包下面的类全部被转化为bean对象
同时应该注意的是,类名首字母是大写,自动扫描时,会把类名首字母小写作为id,getBean时,使用这个id即可
同时,在top.bigking.bean包下的类,应该加上@Component注解,表示这个类是被扫描的bean对象之一
在测试类中:
package top.bigking.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.bigking.bean.Animal;
import top.bigking.conf.TestConfiguration; public class BigKingTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
Animal animal = (Animal) applicationContext.getBean("dog");
animal.bark();
}
}
我们通过ClassPathXmlApplicationContext来解析配置文件,初始化IOC容器
并通过getBean来获取bean对象
你能注意到,这仍然使用了application.xml配置文件,和之前没什么区别嘛
所以,我们只是用java代码,进行更加自动的配置
创建一个配置类
package top.bigking.conf; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import top.bigking.bean.Animal;
import top.bigking.bean.Cat;
import top.bigking.bean.Dog; @Configuration
@ComponentScan("top.bigking.bean")
public class TestConfiguration { @Bean
public Animal Cat(){
return new Cat();
}
@Bean
public Animal Dog(){
return new Dog();
} }
应该注意其中的@ComponentScan注解,括号中的参数的作用,和之前配置文件中的base-package="top.bigking.bean"的作用一致
@Bean 用在方法上,告诉Spring容器,你可以从下面这个方法中拿到一个Bean
所以在相应的类定义中,@Component注解不能删去
在测试类中,相应的,从获取配置文件,变成了获取配置类
package top.bigking.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.bigking.bean.Animal;
import top.bigking.conf.TestConfiguration; public class BigKingTest {
public static void main(String[] args) {
//ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(TestConfiguration.class);
Animal animal = (Animal) applicationContext.getBean("dog");
animal.bark();
}
}
这样,就实现了bean的自动配置,不需要自己再写在配置文件里了!
扫描 + 注解完成bean的自动配置的更多相关文章
- springboot 扫描不到包 @SpringBootApplication 自动配置原理
解决方案 在main类中增加注解 @ComponentScan("com.test.test.*") 扫描具体的包 @ComponentScan(basePackages = {& ...
- 使用spring注解——定义bean和自动注入
对于java bean的定义和依赖配置,使用xml文件真心是不方便. 今天学习如何用注解,解决bean的定义和注入. 常用注解: 1.自动注入:@Resources,@Autowired 2.Bean ...
- SpringBoot自动配置注解原理解析
1. SpringBoot启动主程序类: @SpringBootApplication public class DemoApplication { public static void main(S ...
- SpringBoot 入门篇(二) SpringBoot常用注解以及自动配置
一.SpringBoot常用注解二.SpringBoot自动配置机制SpringBoot版本:1.5.13.RELEASE 对应官方文档链接:https://docs.spring.io/spring ...
- SpringBoot系列二:SpringBoot自动配置原理
主程序类的注解 @SpringBootApplication 注解,它其实是个组合注解,源码如下: @Target({ElementType.TYPE}) @Retention(RetentionPo ...
- Spring boot运行原理-自定义自动配置类
在前面SpringBoot的文章中介绍了SpringBoot的基本配置,今天我们将给大家讲一讲SpringBoot的运行原理,然后根据原理我们自定义一个starter pom. 本章对于后续继续学习S ...
- Springboot 自动配置浅析
Introduction 我们知道,SpringBoot之所以强大,就是因为他提供了各种默认的配置,可以让我们在集成各个组件的时候从各种各样的配置文件中解放出来. 拿一个最普通的 web 项目举例.我 ...
- SpringBoot 源码解析 (五)----- Spring Boot的核心能力 - 自动配置源码解析
在上一篇博客中分析了springBoot启动流程,大体的轮廓只是冰山一角.今天就来看一下springBoot的亮点功能:自动化装配功能. 先从@SpringBootApplication开始.在启动流 ...
- SpringBoot自动配置原理学习
介绍 构建Springboot项目时我们会创建一个启动类 @SpringBootApplication public class DemoApplication { public static voi ...
随机推荐
- Python:JPG->JPEG
由于要在网页上使用图片渐进加载,所以需要将其他图片格式转化为JPEG格式 考虑使用python来实现批量转换 需要先安装pillow: pip install pillow 我这里将JPG格式转化为 ...
- UI控件Telerik UI for Silverlight发布R2 2019|附下载
Telerik UI for Silverlight包含了超过100个能用于纯Silverlight应用程序中或能作为现有ASP.NET应用程序的一部分的UI控件.通过与我们的WPF控件共享一个相同的 ...
- ps雨滴效果制作
雨滴效果制作 尽量选择比较暗或者有人打伞的照片,方便制作雨天的效果 新建图层,设置前景色为黑色,并填充新建的图层为黑色 选择滤镜->杂色->添加杂色,数量设置为150左右(设置得越大后面的 ...
- python爬虫及结巴分词《攀登者》影评分析
<攀登者>影评爬取及分析 0.项目结构 其中simkai.ttf为字体文件,Windows查看系统自带的字体 C:\Windows\Fonts 一.爬取豆瓣影评数据 # -*- codin ...
- Redhat 关闭防火墙和selinux
查看防火墙状态.systemctl status firewalld 临时关闭防火墙命令.重启电脑后,防火墙自动起来.systemctl stop firewalld 永久关闭防火墙命令.重启后,防火 ...
- 树莓派GPIO引脚标识图
- Linux缺少动态连接库.so--cannot open shared object file: No such file or directory
1 Liunx安装报错时,缺少动态链接库时,形式如下: /usr/local/libexec/gcc/x86_64-unknown-liunx-gnu/4.8.2/cc1: error while l ...
- 【知识】定时器setTimeout/setInterval执行时this指针指向问题
[问题描述] setTimetout/setInterval中this指针指向window,以下是一个小demo: var demoChange = { key: true, changeFun() ...
- 【bzoj3672&&uoj7】[Noi2014]购票
*题目描述: 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ市为根的有根树,每个城市与它的父亲用道路 ...
- 4.Python IDLE使用方法详解(包含常用快捷键)
在安装 Python 后,会自动安装一个 IDLE,它是一个 Python Shell (可以在打开的 IDLE 窗口的标题栏上看到),程序开发人员可以利用 Python Shell 与 Python ...