扫描 + 注解完成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 ...
随机推荐
- BZOJ - 1036 树的统计Count (LCT)
LCT试炼题(代码量居然完爆树剖?) #include<bits/stdc++.h> using namespace std; ,inf=0x3f3f3f3f; ],flp[N],n,m, ...
- 如何提升scrapy爬取数据的效率
在配置文件中修改相关参数: 增加并发 默认的scrapy开启的并发线程为32个,可以适当的进行增加,再配置文件中修改CONCURRENT_REQUESTS = 100值为100,并发设置成了为100. ...
- springCloud——Dalston.SR5升级到Greenwich.SR2
老项目: SpringBoot 版本 :1.5.13.RELEASE SpringCloud 版本:Dalston.SR5 项目升级: SpringBoot 版本 :2.1.6.RELEASE Spr ...
- python运算符Ⅵ
Python成员运算符 除了以上的一些运算符之外,Python还支持成员运算符,测试实例中http://www.xuanhe.net/包含了一系列的成员,包括字符串,列表或元组. 实例(Python ...
- MySQL自动生成序号列
select (@i:=@i+1) i,a.CITYID from basis_cityinfo a ,(select @i:=0) t2 order by a.id desc limit 220;
- GO语言学习笔记2-int类型的取值范围
相比于C/C++语言的int类型,GO语言提供了多种int类型可供选择,有int8.int16.int32.int64.int.uint8.uint16.uint32.uint64.uint. 1.i ...
- opencv加椒盐噪声
void salt(IplImage *img, int saltNum) { int x,y; int i ; unsigned char *src = NULL; src = (unsigned ...
- 消息队列之--Kafak
序言 消息丢失如何解决? 解耦 异步 并行 Docker安装Kafak 1.下载镜像 # zookeeper镜像 docker pull wurstmeister/zookeeper # kafka镜 ...
- JPA学习(六、JPA_JPQL)
框架学习之JPA(六) JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中 ...
- 文件操作:rewind()
函数名: rewind() 功 能: 将文件内部的位置指针重新指向一个流(数据流/文件)的开头 注意:不是文件指针而是文件内部的位置指针,随着对文件的读写文件的位置指针(指向当前读写字节)向后移动 ...