一、配置
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 中写的是需要扫描的包
- <context:component-scan base-package="com.spring.ioc5">
- <!-- annotation 通过注解来过滤 org.example.SomeAnnotation
- assignable 通过指定名称过滤 org.example.SomeClass
- regex 通过正则表达式过滤 org\.example\.Default.*
- aspectj 通过aspectj表达式过滤 org.example..*Service+
- -->
- <context:include-filter type="regex" expression="com.spring.ioc5.*"/>
- <context:exclude-filter type="annotation" expression="org.springframework.beans.factory.annotation.Autowired"/>
- </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;
}