链接: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的自动配置的更多相关文章

  1. springboot 扫描不到包 @SpringBootApplication 自动配置原理

    解决方案 在main类中增加注解 @ComponentScan("com.test.test.*") 扫描具体的包 @ComponentScan(basePackages = {& ...

  2. 使用spring注解——定义bean和自动注入

    对于java bean的定义和依赖配置,使用xml文件真心是不方便. 今天学习如何用注解,解决bean的定义和注入. 常用注解: 1.自动注入:@Resources,@Autowired 2.Bean ...

  3. SpringBoot自动配置注解原理解析

    1. SpringBoot启动主程序类: @SpringBootApplication public class DemoApplication { public static void main(S ...

  4. SpringBoot 入门篇(二) SpringBoot常用注解以及自动配置

    一.SpringBoot常用注解二.SpringBoot自动配置机制SpringBoot版本:1.5.13.RELEASE 对应官方文档链接:https://docs.spring.io/spring ...

  5. SpringBoot系列二:SpringBoot自动配置原理

    主程序类的注解 @SpringBootApplication 注解,它其实是个组合注解,源码如下: @Target({ElementType.TYPE}) @Retention(RetentionPo ...

  6. Spring boot运行原理-自定义自动配置类

    在前面SpringBoot的文章中介绍了SpringBoot的基本配置,今天我们将给大家讲一讲SpringBoot的运行原理,然后根据原理我们自定义一个starter pom. 本章对于后续继续学习S ...

  7. Springboot 自动配置浅析

    Introduction 我们知道,SpringBoot之所以强大,就是因为他提供了各种默认的配置,可以让我们在集成各个组件的时候从各种各样的配置文件中解放出来. 拿一个最普通的 web 项目举例.我 ...

  8. SpringBoot 源码解析 (五)----- Spring Boot的核心能力 - 自动配置源码解析

    在上一篇博客中分析了springBoot启动流程,大体的轮廓只是冰山一角.今天就来看一下springBoot的亮点功能:自动化装配功能. 先从@SpringBootApplication开始.在启动流 ...

  9. SpringBoot自动配置原理学习

    介绍 构建Springboot项目时我们会创建一个启动类 @SpringBootApplication public class DemoApplication { public static voi ...

随机推荐

  1. webview默认是不开启localstorage的

    .setDomStorageEnabled(true);// 打开本地缓存提供JS调用,至关重要 转载 https://blog.csdn.net/xhf_123/article/details/77 ...

  2. es6 Object.assign(target, ...sources)

    Object.assign() 方法用于将所有可枚举属性(对象属性)的值从一个或多个源对象复制到目标对象.它将返回目标对象. 语法 Object.assign(target, ...sources) ...

  3. Scala传递参数遇到的坑

    1.方法中的参数全为val型. 例: def insertMap(map:=>Map[String,Int]):Unit={ map+=("b"->2)    //报错 ...

  4. Node.js之querystring模块

    querystring从字面上的意思就是查询字符串,一般是对http请求所带的数据进行解析.querystring模块只提供4个方法,在我看来,这4个方法是相对应的. 这4个方法分别是querystr ...

  5. 【leetcode】1227. Airplane Seat Assignment Probability

    题目如下: n passengers board an airplane with exactly n seats. The first passenger has lost the ticket a ...

  6. python4---打印长方形

    1:方法1for i in range(6): for j in range(3): print("*", end=" ") print() 2:输入显示长方形 ...

  7. Linux基础教程 linux下cat 命令使用详解

    cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...

  8. 批量下载文件asp.net

    一.实现步骤 在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹.然后调用 RAR程序,对临时文件夹进行压缩,然后输出到 ...

  9. H5 网站支付宝支付(前端部分)包含微信浏览器中的处理方法。

    手机网站唤起支付宝支付: H5 网站实现支付宝支付是一个很常见的需求: 实现方式主要是在后台配置和预支付, 前端需要做的就是唤起 支付宝App 然后就可以输入密码支付. 这个其实难度很低, 主要就是在 ...

  10. mysql根据身份证查询年龄,地址,性别

    elect  case left(idcard,2)  when '11' then '北京市' when '12' then '天津市' when '13' then '河北省' when '14' ...