链接: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. Python修炼之路-Socket

    网络编程 socket套接字 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过“套接字”向网络发出请求或者应答网络请求. socket ...

  2. vue 无缝滚动文字

    前言 用vue做无缝滚动,字体弹幕 就上代码吧 <head> <meta charset="UTF-8"> <style> div, ul, l ...

  3. rsync快速部署记录

    rsync快速部署记录 安装rsync和使用环境:客户端:10.192.30.59 fudao_db_cluster_002 (将本地文件备份到服务端)服务端:10.192.30.60 fudao_d ...

  4. 分组统计 over(partition by

    sum( CASE WHEN ISNULL(b.zl, 0) = 0 THEN C.LLZL ELSE b.zl END * c.pccd * b.sl) over(partition by b.dj ...

  5. EF大数据插入

    _April给出代码: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotati ...

  6. linux下简易端口扫描器

    #include<iostream> #include<string.h> #include<sys/types.h> #include<sys/socket ...

  7. ModelSerializer 使用知识点_序列化和反序列化用法区别

    1.ModelSerializer  如下 from api_test.errorCode.errorCode import Statusclass RelatedbSerializer(serial ...

  8. 【python 应用之四】提升 Python 运行性能的 7 个习惯

    大家都知道艺赛旗的 RPA 依赖于 python 语言.因此我们可以掌握一些技巧,可尽量提高 Python 程序性能,也可以避免不必要的资源浪费.1.使用局部变量 尽量使用局部变量代替全局变量:便于维 ...

  9. Java九种基本数据类型,以及他们的封装类

    基本类型 大小(字节) 默认值 封装类 byte 1 (byte)0 Byte short 2 (short)0 Short int 4 0 Integer long 8 0L Long float ...

  10. SpringMVC拦截器+Spring自定义注解实现权限验证

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...