启用组件扫描之Xml版本1
之前有个是针对Java的启用组件扫描的,
现在介绍下Xml配置的版本,大部分代码一样,少了唱片播放器Config
文件,多了音响系统.xml
文件,使用的ApplicationContext类型变化一下即可。
I唱片
接口代码
package 音响系统;
public interface I唱片 {
void 播放();
}
package 音响系统;
import org.springframework.stereotype.Component;
/**
*
* 没有必要显式配置`天之大专辑` Bean,
* 因为这个类使用了`@Component`注解,
* `@Component`表明该类会作为组件类,
* 所以Spring会为你把事情处理妥当。
*
* 不过,**组件扫描默认是不启用的**。
*/
@Component
public class 天之大专辑 implements I唱片 {
private String 歌手 = "费玉清";
private String 专辑名 = "天之大";
public void 播放() {
System.out.println("播放专辑《" + 专辑名 + "》 by " + 歌手);
}
}
天之大专辑
的具体内容并不重要。你要注意的是它使用了@Component
。
@Component
表明该类会作为组件类,并告知Spring要为这个类创建Bean。没有必要显式配置天之大专辑
Bean,因为这个类使用了@Component
注解,所以Spring会为你把事情处理妥当。(需要启用组件扫描,在后文【启用组件扫描】有介绍)
I媒体播放器
接口代码
package 音响系统;
public interface I媒体播放器 {
void 播放();
}
package 音响系统;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class 唱片播放器 implements I媒体播放器 {
private I唱片 唱片;
@Autowired
public 唱片播放器(I唱片 cd) {
this.唱片 = cd;
}
public void 播放() {
唱片.播放();
}
}
如果你不将I唱片
插入(注入)到I媒体播放器
中,那么I媒体播放器
其实是没有太大用处的。So,可以这样说,I媒体播放器
依赖于I唱片
才能完成它的使命。
启用组件扫描
组件扫描默认是不启用的。我们还需要显式配置一下Spring,从而命令Spring去寻找带有@Component
注解的类,并为其创建Bean。
如果你更倾向于使用XML来启用组件扫描的话,那么可以使用 Spring context 命名空间的 <context:component-scan>
元素。
src/main/resources/META-INF/spring/音响系统.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="音响系统" />
</beans>
看看效果
package 音响系统;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class 播放Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(
"META-INF/spring/音响系统.xml");
I媒体播放器 播放器 = context.getBean(I媒体播放器.class);
播放器.播放();
context.close();
}
}
Spring自带了多种类型的ApplicationContext。下面罗列的几个是你最有可能遇到的。
Java的
- AnnotationConfigApplicationContext
从一个或多个基于Java的配置类中加载 ApplicationContext。- AnnotationConfigWebApplicationContext
从一个或多个基于Java的配置类中加载 WebApplicationContext 。XML的
- ClassPathXmlApplicationContext
从类路径下的一个或多个XML配置文件中加载 ApplicationContext 定义,把应用上下文的定义文件作为类资源。- FileSystemXmlapplicationcontext
从文件系统下的一个或多个XML配置文件中加载 ApplicationContext 定义。- XmlWebApplicationContext
从Web应用下的一个或多个XML配置文件中加载 ApplicationContext 定义。
输出:
八月 07, 2021 10:12:23 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7106e68e: startup date [Sat Aug 07 10:12:23 CST 2021]; root of context hierarchy
播放专辑《天之大》 by 费玉清
八月 07, 2021 10:12:23 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7106e68e: startup date [Sat Aug 07 10:12:23 CST 2021]; root of context hierarchy
所有的代码见:
https://gitee.com/virhuiai/spring-wiring-example/tree/master/启用组件扫描之Xml版本1
与Java版本的对比图: