详解spring自动扫描包

 更新时间:2018年06月12日 14:08:38   作者:qq_36098284  
这篇文章主要介绍了spring自动扫描包的相关知识,本文通过实例相结合的形式给大家介绍的非常详细,感兴趣的朋友跟随脚本之家小编一起看看吧

配置文件

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

Spring2.5为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入进Spring容器中管理。

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

<?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的配置文件改为:

<?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类,如下:

@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类,如下:

@Repository
public class PersonDaoBean implements PersonDao {
 @Override
 public void add() {
  System.out.println("执行PersonDaoBean中的add()方法");
 }
}

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

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控制台打印:

这里写图片描述

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

@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类的代码应改为:

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管理的bean的作用域,我们就能知道以上Spring管理的两个bean的作用域默认是singleton。当然了,我们也可以更改Spring管理的bean的作用域,如将PersonServiceBean类的代码改为:

@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类的代码修改为:

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控制台打印:

这里写图片描述

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

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

@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类的代码修改为:

public class SpringTest {
 @Test
 public void instanceSpring() {
  AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 
  PersonService personService = (PersonService) ctx.getBean("personService");
  ctx.close();
 }
}

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

这里写图片描述

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

总结

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

相关文章

  • SpringBoot结合JWT登录权限控制的实现

    SpringBoot结合JWT登录权限控制的实现

    本文主要介绍了SpringBoot结合JWT登录权限控制的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • Hibernate映射解析之关联映射详解

    Hibernate映射解析之关联映射详解

    所谓关联映射就是将关联关系映射到数据库里,在对象模型中就是一个或多个引用。下面这篇文章详细的给大家介绍了Hibernate映射解析之关联映射的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-02-02
  • java字符串相似度算法

    java字符串相似度算法

    这篇文章主要介绍了java字符串相似度算法,是Java实现比较典型的算法,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-02-02
  • Java 生成二维码的工具资料整理

    Java 生成二维码的工具资料整理

    本文主要介绍Java 生成二维码的几种方法,这里给大家详细介绍了java生成二维码的三种工具,并附有示例代码供大家参考,开发java 二维码的朋友可以参考下
    2016-08-08
  • Spring核心思想之浅谈IoC容器与依赖倒置(DI)

    Spring核心思想之浅谈IoC容器与依赖倒置(DI)

    文章介绍了Spring的IoC和DI机制,以及MyBatis的动态代理,通过注解和反射,Spring能够自动管理对象的创建和依赖注入,而MyBatis则通过动态代理实现了接口方法到数据库操作的映射,文章详细解释了Spring和MyBatis的工作原理,并通过示例代码展示了它们的结合使用方式
    2025-01-01
  • Java数据结构--时间和空间复杂度

    Java数据结构--时间和空间复杂度

    这篇文章主要介绍了java数据结构的时间和空间复杂度,小编觉得这篇文写的不错,感兴趣的朋友可以了解下,希望能够给你带来帮助
    2021-08-08
  • SpringBoot整合SpringBoot-Admin实现监控应用功能

    SpringBoot整合SpringBoot-Admin实现监控应用功能

    本文主要介绍如何整合Spring Boot Admin,以此监控Springboot应用,文中有相关的示例代码供大家参考,需要的朋友可以参考下
    2023-05-05
  • Java获取环境变量(System.getenv)的方法

    Java获取环境变量(System.getenv)的方法

    本文主要介绍了Java获取环境变量(System.getenv)的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Spring 方法注解@Bean及配置类扫描路径

    Spring 方法注解@Bean及配置类扫描路径

    这篇文章主要介绍了Spring 方法注解@Bean及配置类扫描路径,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧
    2024-12-12
  • IDEA 必要配置设置方式

    IDEA 必要配置设置方式

    这篇文章主要介绍了IDEA 必要配置设置方式,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04

最新评论