标签:
<?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.xsd">
<context:component-scan base-package="">
</context:component-scan>
</beans>
类型 | 说明 |
---|---|
@component | 通用的构造型注解,标示该类为Spring 组件。 |
@Controller |
标识将该类定义为Spring MVC controller。 |
@Repository |
标识将该类定义为数据仓库(例如:Dao层)。 |
@Service | 标识将该类定义为服务(例如:Service层)。 |
package com.sjf.bean;
import org.springframework.stereotype.Component;
/**
* 学校实体类
* @author sjf0115
*
*/
@Component
public class School {
private String name;
private String location;
...
}
package com.sjf.bean;
import org.springframework.stereotype.Component;
/**
* 学生实体类
* @author sjf0115
*
*/
@Component("studentBean")
public class Student {
private String name;
private int age;
private School school;
...
}
<context:component-scan base-package="com.sjf.bean">
<context:include-filter type="" expression=""/>
<context:exclude-filter type="" expression=""/>
</context:component-scan>
类型 | 说明 |
---|---|
annotation | 过滤器扫描使用指定注解标注的那些类,通过expression属性指定要扫描的注解 |
assignable | 过滤器扫描派生于expression属性所指定类型的那些类。 |
aspectj | 过滤器扫描与expression属性所指定的AspectJ表达式所匹配的那些类。 |
custom | 使用自定义的org.springframework.core.type.TypeFilter实现类,该类由expression属性指定。 |
regex | 过滤器扫描类的名称与expression属性所指定的正则表达式所匹配的那些类。 |
package com.sjf.bean;
/**
* 农民实体类
* @author sjf0115
*
*/
public class Farmer implements Worker {
public void work() {
System.out.println("正在辛勤的耕地...");
}
}
<context:component-scan base-package="com.sjf.bean">
<context:include-filter type="assignable" expression="com.sjf.bean.Worker"/>
<context:exclude-filter type="assignable" expression="com.sjf.bean.Performer"/>
</context:component-scan>
Farmer farmer = (Farmer) context.getBean("farmer");
farmer.work();
正在辛勤的耕地... |
Singer singer = (Singer) context.getBean("singer");
singer.perform();
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘singer‘ is defined |
标签:
原文地址:http://blog.csdn.net/sunnyyoona/article/details/50650102