扫描 + 注解完成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 ...
随机推荐
- javascript的基础知识点
一:鼠标提示框 需求描述:鼠标移入都input上,div显示,移出,div消失 分析:控制display=block/none 鼠标移入,鼠标移出事件 <input type="bu ...
- 《Head First 软件开发》阅读六
软件错误:专业排错 你编写的代码,你的责任.处理错误的方法和其他流程一样,准备好白板.让客户参与.满怀信心的估计.重构与预构. 首先是与客户加强沟通,不管是谁的代码,在自己的系统里就是自己的代码.使代 ...
- teradata安装
一,下载 步骤1 - 从链接下载所需的VM版本,http://downloads.teradata.com/download/database/teradata-express-for-vmware- ...
- Shell中Bash的基本功能(二)
1 历史命令 1)历史命令的查看[root@localhost ~]# history [选项] [历史命令保存文件]选项:-c: 清空历史命令-w: 把缓存中的历史命令写入历史命令保存文件.如果不手 ...
- Sparrow 开发板化身电脑音量调节器
前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正. 之前的新浪不能用啦,这次部分图片用的sm.ms的图床,加载慢,请耐心,准备换图床. 1.开箱简介 来填坑了!这次是 Sparrow ...
- 1-window搭建git
windows7搭建Git私服 作为版本控制工具大多公司会选用Git,但svn也具有一定的优势,在对开源项目管理方面,Git具有一定的优势,我们可以将自己的项目放到GitHub上面,供大家交流学习,但 ...
- 使用GitHub(一):添加SSHkey
使用GitHub(一):添加SSHkey 本文简单介绍使用GitHub对代码进行版本控制,包括添加SSHkey.配置Git.使用Git创建版本库并在GitHub上进行管理,主要目的是对学习内容进行总结 ...
- 挖矿病毒DDG的分析与清除
注:以下所有操作均在CentOS 7.2 x86_64位系统下完成. 今天突然收到“阿里云”的告警短信: 尊敬的****:云盾云安全中心检测到您的服务器:*.*.*.*(app)出现了紧急安全事件:挖 ...
- 微信小程序 导航(a 连接)自定义组件
导航:navigator 组件 组件上的属性: target:跳到其他小程序( 默认是当前小程序 ),当属性值为 miniProgram 时,跳到别的小程序(如果要跳到别的小程序,需要填写 appid ...
- loj#6038 「雅礼集训 2017 Day5」远行
分析 代码 #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define ...