`
lanqiaoyeyu
  • 浏览: 24336 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Spring学习(六) 通过在classpath自动扫描方式把组件纳入Spring容器中管理

    博客分类:
  • Java
阅读更多
之前我们都是使用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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <context:component-scan base-package="com.bill"/>
</beans>

其中,base-package="com.bill"为需要扫描的包(包含子包)。@Service用于标注业务层组件,@Controller用于标注控制层组件(入struts中的action),@Repository用于标注数据访问组件,即DAO组件,而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
例如:
数据访问层代码:
package com.bill.dao.impl;

import org.springframework.stereotype.Repository;
import com.bill.dao.PersonDao;

@Repository
public class PersonDaoBean implements PersonDao {
	public void add() {
		System.out.println("this is add() of PersonDaoBean");
	}
}

业务层代码:
package com.bill.impl;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.bill.PersonService;
import com.bill.dao.PersonDao;
/**
 * if Scope is prototype, it means new the object when using this bean
 * @author Bill
 *
 */
@Service("personService") //指定bean名字
//@Service("personService") @Scope("prototpye")//指定bean名字,并且每调用这个类,就创建新的对象
//@Service //默认bean的名称为类名的第一个字母小写,如personServiceBean
public class PersonServiceBean implements PersonService {
	private PersonDao personDao;
	public void save(){
		personDao.add();
	}

	//当Spring容器装载前执行初始化方法
	@PostConstruct
	public void init(){
		System.out.println("init() function");
	}
	
	//当Spring容器卸载之前执行释放资源方法
	@PreDestroy
	public void destroy(){
		System.out.println("destroy resource");
	}
}


测试代码:
		AbstractApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
		PersonService personService = (PersonService)act.getBean("personService");
		personService.save();
	    act.close();
分享到:
评论

相关推荐

    Spring通过在classpath自动扫描方式把组件纳入spring容器中管理

    NULL 博文链接:https://huangminwen.iteye.com/blog/1486843

    浅析Spring配置中的classpath:与classpath*:的区别

    主要介绍了Spring配置中的"classpath:"与"classpath*:"的区别,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

    Spring2.5 自动扫描classpath

    NULL 博文链接:https://hahalzb.iteye.com/blog/690802

    springIOC核心组件分析.vsdx

    spring-context-indexer:类管理组件和Classpath扫描 spring-expression:表达式语句 切面编程: spring-aop:面向切面编程,CGLB,JDKProxy spring-aspects:集成AspectJ,Aop应用框架 spring-instrume

    Spring中使用classpath加载配置文件浅析

    本文档介绍了spring中的在classpath中加载配置文件

    Spring组件自动扫描详解及实例代码

    一个系统往往有成千上万的组件,如果需要手动将所有组件都纳入spring容器中管理,是一个浩大的工程。 解决方案 Spring 提供组件扫描(component scanning)功能。它能从classpath里自动扫描、侦测和实例化具有特定...

    spring jar 包详解

    (1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个...

    Spring中文帮助文档

    2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载...

    struts hibernate spring 集成时使用依赖注解的方式的秘籍

    //applicationContext.xml文件中添加 &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...-- 开启classpath扫描 --&gt; &lt;context:component-scan base-package="com.haijian" /&gt;

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring API

    2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载...

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个jar文件是所有应用都要...

    Spring攻略(第二版 中文高清版).part1

    1.14 从Classpath中扫描组件 50 1.14.1 问题 50 1.14.2 解决方案 51 1.14.3 工作原理 51 1.15 小结 56 第2章 高级Spring IoC容器 57 2.1 调用静态工厂方法创建Bean 57 2.1.1 问题 57 2.1.2 解决...

    Spring_Framework_ API_5.0.5 (CHM格式)

    Spring5 版本的候选版本已经在 classpath 和 modulepath 上支持 Java9 了。 GA版本中你可以期待良好的 JDK9 支持。 3. 使用 JDK8 特性 获取免费Spring 5 新特性视频详解可以群:554355695 在 Spring4.3 之前,...

    Spring.html

    Spring IOC 控制反转:把创建对象的权利交给Spring 创建对象 1.... 2.... 3.... 管理对象 ... BeanFactory:使用这个工厂创建...在Servlet中使用WebApplicationContextUtils获取容器对象 5.使用容器对象去获取Service对象

    Spring攻略(第二版 中文高清版).part2

    1.14 从Classpath中扫描组件 50 1.14.1 问题 50 1.14.2 解决方案 51 1.14.3 工作原理 51 1.15 小结 56 第2章 高级Spring IoC容器 57 2.1 调用静态工厂方法创建Bean 57 2.1.1 问题 57 2.1.2 解决...

    Spring Framewor开发手册

    2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织...

    spring2.5.chm帮助文档(中文版)

    2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载...

    spring boot加载资源路径配置和classpath问题解决

    主要介绍了spring boot加载资源路径配置和classpath问题解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

Global site tag (gtag.js) - Google Analytics