Spring component-scan XML配置与@ComponentScan注解配置
作者:蓝黑2020 发布时间:2023-07-04 03:37:12
前言
无论Spring的XML配置或者Java配置,都可以配置自动扫描,也就是在指定包及其子包下的component,都会被自动扫描并被Spring容器管理。
注:component指的是被 @Component
注解及其变种(如 @Controller
、 @Service
、 @Repository
、 @Configuration
等)所修饰的类。
环境:
Ubuntu 22.04
IntelliJ IDEA 2022.1.3
JDK 17.0.3
Spring 5.3.21
准备
创建Maven项目 test0831
。
修改 pom.xml
文件,添加依赖:
......
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.21</version>
</dependency>
......
在 src/test/java
目录下创建测试:
public class MyTest {}
创建如下POJO:
Axe
:Axe接口;StoneAxe
:Axe实现类;SteelAxe
:Axe实现类;Person
:Person持有Axe
package pojo;
public interface Axe {
public void chop();
}
package pojo;
import org.springframework.stereotype.Component;
@Component
public class StoneAxe implements Axe{
@Override
public void chop() {
System.out.println("Stone axe!");
}
}
package pojo;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
@Primary
public class SteelAxe implements Axe{
@Override
public void chop() {
System.out.println("Steel axe!");
}
}
package pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Person {
private String name;
private Axe axe;
public void setAxe(Axe axe) {
this.axe = axe;
}
public void setName(String name) {
this.name = name;
}
public void useAxe() {
System.out.println("I am " + name);
axe.chop();
}
@Autowired
public Person(@Value("Tom") String name, Axe axe) {
this.name = name;
this.axe = axe;
}
}
注:本例使用了 @Autowired
注解来自动装配注入,默认是 byType
,当有多个类都满足条件时Spring会报错,所以使用了 @Primary
注解来指定优先选择该类。
@Component的bean
XML配置
配置:
在 src/main/resources
目录下创建 applicationContext.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
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="pojo"/>
</beans>
测试:
创建测试如下:
@Test
public void test1() {
var ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
var person = ctx.getBean("person", Person.class);
person.useAxe();
}
运行测试,结果如下:
I am Tom
Steel axe!
Java配置 配置
创建Java配置类 MyConfig.java
如下:
package config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = {"pojo"})
public class MyConfig {
}
测试:
创建测试如下:
@Test
public void test2() {
var ctx = new AnnotationConfigApplicationContext(MyConfig.class);
var person = ctx.getBean("person", Person.class);
person.useAxe();
}
运行测试,结果如下:
I am Tom
Steel axe!
@Configuration的bean
自动扫描时,如果遇到Java配置类,也会加载其配置。
在 pojo
包下添加Java配置类 MyConfig2
:
package pojo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig2 {
@Bean
public String xxx() {
return "xxx";
}
}
本例中, MyConfig2
是一个配置类,其中配置了ID为 xxx
的bean。
XML配置 配置
前面已经配置了:
<context:component-scan base-package="pojo"/>
MyConfig2
在 pojo
包下,所以无需额外的配置。
测试
创建测试如下:
@Test
public void test3() {
var ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
var xxx = ctx.getBean("xxx", String.class);
System.out.println(xxx);
}
运行测试,结果如下:
xxx
Java配置 配置
前面已经配置了:
@ComponentScan(basePackages = {"pojo"})
MyConfig2
在 pojo
包下,所以无需额外的配置。
测试
创建测试如下:
@Test
public void test4() {
var ctx = new AnnotationConfigApplicationContext(MyConfig.class);
var xxx = ctx.getBean("xxx", String.class);
System.out.println(xxx);
}
运行测试,结果如下:
xxx
小结
@Configuration
是 @Component
的变种,因此也会被自动扫描并加载。
如果Java配置类不是在自动扫描的路径里,则需要显式加载:
通过XML配置加载:
<context:annotation-config/>
<bean class="config.MyConfig3"/>
通过Java配置加载:
@Import({MyConfig3.class})
来源:https://blog.csdn.net/duke_ding2/article/details/126617024


猜你喜欢
- 去公司面试,对方一口一个controller,一口一个service,dao,搞得我很紧张。其实都是很简单的东西,只是自己当时不知道罢了,接
- 在C#中常用到的运算符有条件运算符,is运算符,as运算符,typeof 运算符等等,接下来在文章中将为大家具体介绍各个运算符的使用方法条件
- 本文实例为大家分享了一个基于JAVA的知乎爬虫,抓取知乎用户基本信息,基于HttpClient 4.5,供大家参考,具体内容如下详细内容:抓
- IDEA安装阿里巴巴编码规范插件的两种方式:在线安装和离线安装。1.在线安装:打开file-settings-Plugins.如图:搜索到点
- 本文实例讲述了Java编程实现统计一个字符串中各个字符出现次数的方法。分享给大家供大家参考,具体如下:import java.util.It
- 本文实例为大家分享了java实现随机数生成器的具体代码,供大家参考,具体内容如下自己编的随机数生成器,比较简陋,功能也单一,当作练手。App
- 前言之前探讨过的 sealed class 和 sealed interface 存在 module 的限制,但其主要用于密封 class
- 01: 异步任务02: 定时任务一、SpringBoot--任务:邮件任务1.1 添加依赖(增加邮件支持)pom.xml <depen
- C# 匿名函数、lambda表达式、Linq查询一、匿名函数的使用匿名函数是一个“内联”语句或表达式
- 这篇文章主要介绍了springmvc处理模型数据Map过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 自定义starter yaml提示失效问题问题场景在自定义starter后,必不可少会有properties配置参数需要指定,而在有时又不知
- Chart折线图使用鼠标滚轮放大、缩小和平移曲线使用鼠标滚轮滚动放大和缩小X轴的宽度,鼠标左键按住拖动实现曲线的左右平移,不再使用滚动条。添
- 本文实例讲述了Android开发获取重力加速度和磁场强度的方法。分享给大家供大家参考,具体如下:Android获取重力加速度和磁场强度主要依
- 模板消息文档公众号的类型分为服务号、订阅号和企业号,其中服务号和订阅号比较常见。要想实现公众号推动消息给指定的用户,其类型必须为服务号。推送
- 本文实例为大家分享了C++ socket实现miniFTP的方法,供大家参考,具体内容如下客户端:服务端:建立连接 &
- 一、实验目的(1)掌握应用黑盒测试技术进行测试用例设计。(2)掌握对测试用例进行优化设计方法。二、实验内容日期问题测试以下程序:该程序有三个
- 目录1、效果视频2、剖析Android坐标系3、实现方式3.1 layout3.2 scrollBy3.3 offsetLeftAndRig
- 一、MyBatis背景介绍MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码
- 一般来说课本上的数据结构包括数组、单链表、堆栈、树、图。我这里所指的数据结构,是一个怎么表示一个对象的问题,有时候,单单一个变量声明不堪大用
- 当把一个事件发布到Spring提供的ApplicationContext中,被 * 侦测到,就会执行对应的处理方法。事件本身事件是一个自定义