详解spring自动扫描包

时间:2022-09-26 20:24:45

配置文件

前面的例子我们都是使用xml的bean定义来配置组件。在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找及维护起来也不太方便。

spring2.5为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@component、@service、@controller、@repository注解的类,并把这些类纳入进spring容器中管理。

它的作用和在xml文件中使用bean节点配置组件是一样的。要使用自动扫描机制,我们需要打开以下配置信息:

?
1
2
3
4
5
6
7
8
9
10
<?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-4.2.xsd">
 <context:component-scan base-package="cn.itcast" />
</beans>

其中<context:component-scan base-package="cn.itcast" />这个配置隐式注册了多个对注解进行解析处理的处理器,包括<context:annotation-config/>该配置注册的处理器,也就是说写了<context:component-scan base-package="cn.itcast" />配置,就不用写<context:annotation-config/>配置了,此外base-package为需要扫描的包(含子包)。

注解

@service用于标注业务层组件、 @controller用于标注控制层组件(如struts2中的action)、@repository用于标注数据访问组件,即dao组件。而@component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
本文是建立在@autowire注解与自动装配的案例基础上的。

我们首先将spring的配置文件改为:

?
1
2
3
4
5
6
<?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-4.2.xsd">
 <context:component-scan base-package="cn.itcast" />
</beans>

一个实例

然后使用@service注解标注personservicebean类,如下:

?
1
2
3
4
5
6
7
8
9
10
11
@service
public class personservicebean implements personservice {
 private persondao persondao;
 public void setpersondao(persondao persondao) {
  this.persondao = persondao;
 }
 @override
 public void save() {
  persondao.add();
 }
}

使用@repository注解标注persondaobean类,如下:

?
1
2
3
4
5
6
7
@repository
public class persondaobean implements persondao {
 @override
 public void add() {
  system.out.println("执行persondaobean中的add()方法");
 }
}

最后,我们修改springtest类的代码为:

?
1
2
3
4
5
6
7
8
9
10
11
public class springtest {
 @test
 public void instancespring() {
  abstractapplicationcontext ctx = new classpathxmlapplicationcontext("beans.xml");
  personservice personservice = (personservice) ctx.getbean("personservicebean");
  persondao persondao = (persondao) ctx.getbean("persondaobean");
  system.out.println(personservice);
  system.out.println(persondao);
  ctx.close();
 }
}

测试instancespring()方法,可看到eclipse控制台打印:

详解spring自动扫描包

如果我们想使用按指定名称获取,可将personservicebean类的代码修改为:

?
1
2
3
4
5
6
7
8
9
10
11
@service("personservice")
public class personservicebean implements personservice {
 private persondao persondao;
 public void setpersondao(persondao persondao) {
  this.persondao = persondao;
 }
 @override
 public void save() {
  persondao.add();
 }
}

这样,springtest类的代码应改为:

?
1
2
3
4
5
6
7
8
9
public class springtest {
 @test
 public void instancespring() {
  abstractapplicationcontext ctx = new classpathxmlapplicationcontext("beans.xml");
  personservice personservice = (personservice) ctx.getbean("personservice");
  system.out.println(personservice);
  ctx.close();
 }
}

测试instancespring()方法,可看到eclipse控制台打印:

详解spring自动扫描包

我们前面学过spring管理的bean的作用域,我们就能知道以上spring管理的两个bean的作用域默认是singleton。当然了,我们也可以更改spring管理的bean的作用域,如将personservicebean类的代码改为:

?
1
2
3
4
5
6
7
8
9
10
11
@service("personservice") @scope("prototype")
public class personservicebean implements personservice {
 private persondao persondao;
 public void setpersondao(persondao persondao) {
  this.persondao = persondao;
 }
 @override
 public void save() {
  persondao.add();
 }
}

意味着spring管理的personservicebean这个bean的作用域变成prototype了,这时我们将springtest类的代码修改为:

?
1
2
3
4
5
6
7
8
9
10
public class springtest {
 @test
 public void instancespring() {
  abstractapplicationcontext ctx = new classpathxmlapplicationcontext("beans.xml");
  personservice personservice1 = (personservice) ctx.getbean("personservice");
  personservice personservice2 = (personservice) ctx.getbean("personservice");
  system.out.println(personservice1 == personservice2);
  ctx.close();
 }
}

测试instancespring()方法,可看到eclipse控制台打印:

详解spring自动扫描包

prototype作用域本来就意味着每次从spring容器获取bean都是新的对象嘛。

若是通过在classpath路径下自动扫描方这种式把组件纳入spring容器中管理,如何指定bean的初始化方法和销毁方法呢?这时我们就需要用到两个注解:@postconstruct和@predestroy。为了试验,我们将personservicebean类的代码修改为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@service("personservice")
public class personservicebean implements personservice {
 private persondao persondao;
 @postconstruct
 public void init() {
  system.out.println("初始化资源");
 }
 @predestroy
 public void destroy() {
  system.out.println("销毁、关闭资源");
 }
 public void setpersondao(persondao persondao) {
  this.persondao = persondao;
 }
 @override
 public void save() {
  persondao.add();
 }
}

接下来还要将springtest类的代码修改为:

?
1
2
3
4
5
6
7
8
public class springtest {
 @test
 public void instancespring() {
  abstractapplicationcontext ctx = new classpathxmlapplicationcontext("beans.xml");
  personservice personservice = (personservice) ctx.getbean("personservice");
  ctx.close();
 }
}

这样,测试instancespring()方法,eclipse控制台会打印:

详解spring自动扫描包

如要查看源码,可点击让spring自动扫描和管理bean进行下载

总结

以上所述是小编给大家介绍的spring自动扫描包,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/qq_36098284/article/details/80663860