spring classpath自动扫描把组件纳入spring容器中管理

一、配置

1.引入context命名空间需要在xml中配置

xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd

2.在配置文件中添加

<context:component-scan base-package="cn.bdqn"/>

base-package 中写的是需要扫描的包

  1. <context:component-scan base-package="com.spring.ioc5">  
  2.         <!-- annotation 通过注解来过滤          org.example.SomeAnnotation    
  3.              assignable 通过指定名称过滤        org.example.SomeClass  
  4.              regex      通过正则表达式过滤      org\.example\.Default.*  
  5.              aspectj    通过aspectj表达式过滤  org.example..*Service+  
  6.          -->  
  7.         <context:include-filter type="regex" expression="com.spring.ioc5.*"/>  
  8.         <context:exclude-filter type="annotation" expression="org.springframework.beans.factory.annotation.Autowired"/>  
  9. </context:component-scan> 


二、功能

1.在使用组件扫描元素时,AutowireAnnotationBeanPostProcessor 和CommonAnnotationBeanPostProcessor会隐式地被包括进来,。也就是说,里那个组件都会被自动检测并织入,所有这一切都不需要再XML中日工任何bean配置元数据。

2.功能介绍

i.Service 用于标注业务层组件

ii.@Controller用于表组控制层组件(如struts中的action)

iii.@Repository 用于标注数据访问组件,即DAO组件、

iii.@Component泛指组件,当组件不好归类的时候,我们可以使用这个租借进行标注

三、使用

package com.bdqn.dao.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;
import com.bdqn.dao.EmployeeDao;
import com.bdqn.entity.Employee;
import com.bdqn.util.HibernateUtil;
/**
 * 
 * @Repository 标注在类的上面
 * 相当于spring中定义:
 * <bean id="employeeDaoImpl" class=" com.bdqn.dao.impl.EmployeeDaoImpl"></bean>
 *注意!!!:如果没有为@Repository定义任何值的时候,此时属性的值,就是该类的名称,且首字母要小写
 * 如果定义为@Repository("employeeDao")
 * 相当于spring中定义:
 * <bean id="employeeDao" class=" com.bdqn.dao.impl.EmployeeDaoImpl">
 * <property name="employee" ref="employee"/>
 * </bean>
 */
@Repository
public class EmployeeDaoImpl implements EmployeeDao {

@Resource(name="employee")
private Employee employee;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值