这里小编就不做多的啰嗦啦,直接上讲解自动扫描的注解使用
1.搭建环境+测试
1.1 创建TestObject 类(@Component)
@Component
public class TestObject {
public TestObject(){
System.out.println("TestObject Constractor's ... ");
}
}
1.2 创建Controller层AnnotationController类(@Controller)
@Controller
public class AnnotationController {
public AnnotationController(){
System.out.println("Controller Constractor's ...");
};
}
1.3 创建持久化层 AnnotationRepository类(@Repository(“repository”),在这里起了个别名)
@Repository("repository")
public class AnnotationRepository {
public AnnotationRepository(){
System.out.println("AnnotationRepository Constractor's ...");
}
}
1.4 创建AnnotationService接口
public interface AnnotationService {
public void soutInfo();
}
1.4.1 创建AnnotationService实现类AnnotationJdbcServiceImpl(@Service)服务层
@Service("annotationService")
public class AnnotationServiceImpl implements AnnotationService{
@Override
public void soutInfo() {
System.out.println("AnnotationServiceImple's souts... ");
}
2 创建spring配置文件,导入context标签(如果没有可以粘小编的)
<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"
xmlns:p="http://www.springframework.org/schema/p"
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">
2.1 开启扫描包
<context:component-scan base-package="cn.itcast.spring.beans.Annotation">
</context:component-scan>
3 创建测试类
public class TestAnnotationsDemo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean-annotation.xml");
TestObject testObject = (TestObject) context.getBean("testObject");
System.out.println("testObject : " + testObject);
AnnotationController controller = (AnnotationController) context.getBean("annotationController");
System.out.println("controller : " + controller);
AnnotationRepository repository = (AnnotationRepository) context.getBean("repository");
System.out.println("repository :" + repository);
AnnotationService service = (AnnotationService) context.getBean("annotationService");
System.out.println("service : " + service);
}
3.1 这里需要注意的是,小编使用的是id的形式获取,如果要通过id获取,一定要跟类名对应,并且将类名的第一个字母改成小写字母,否则会抛出如下异常
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException:
No bean named 'AnnotationController' available
3.2 也可以是用类名映射的方式获取对应的Bean
TestObject testObject = context.getBean(TestObject.class);
输出结果:
TestObject Constractor’s …
Controller Constractor’s …
AnnotationRepository Constractor’s …
testObject : cn.itcast.spring.beans.Annotation.TestObject@e136d3
controller : cn.itcast.spring.beans.Annotation.controller.AnnotationController@8e4bb0
repository :cn.itcast.spring.beans.Annotation.repository.AnnotationRepository@79cee3
service : cn.itcast.spring.beans.Annotation.service.AnnotationServiceImpl@6a205
3.3 resource- pattern的使用:扫描过滤器只会扫描指定目录下的文件其他一律不扫描
<context:component-scan base-package="cn.itcast.spring.beans.Annotation"
resource-pattern="controller/*.class" ></context:component-scan>
</beans>
beans :contextcom
3.4 context:component-scan下的子节点配置(context:include-filter)只扫描配置的包,但是要注意,需要配合(use-default-filters=“false”)使用!!!
<context:component-scan base-package="cn.itcast.spring.beans.Annotation" use-default-filters="false">
<-- context:include-filter 子节点指定包含哪些表达式的组件,该子节点需要use- default-filters 配合使用-->
<!-- 只扫描AnnotationContoller包 -->
<context:include-filter
type="assignable"
expression="cn.itcast.spring.beans.Annotation.controller.AnnotationController"></context:include-filter>
-->
</context:component-scan>
3.5 context:exclude-filter扫描使用(除了指定的包之外都可以被扫描)
<context:component-scan base-package="cn.itcast.spring.beans.Annotation" >
<!-- context:exclude-filter 子节点指定排除哪些指定表达式的组件-->
<!-- 除了AnnotaionController 之外的所有包都可以被扫描 -->
<context:exclude-filter
type="assignable"
expression="cn.itcast.spring.beans.Annotation.controller.AnnotationController"></context:exclude-filter>
</context:component-scan>
这里注意type有几种形式(annotation,assignable,aspectj , custom , regex)但是最常用的是
(annotation,assignable
)
其他的注解小编就不作多的讲解了,主要讲解这两个标签
4.type=annotation
<!-- annotation类型指定中,exception指定注解包名 -->
<context:component-scan base-package="cn.itcast.spring.beans.Annotation">
<context:exclude-filter
type="annotation"
expression="org.springframework.stereotype.Service"/>
</context:component-scan>-->
4.1 type=assignable
<!-- assignable类型指定中,exception指定类的全路径 -->
<context:component-scan base-package="cn.itcast.spring.beans.Annotation" use-default-filters="false">
<!-- 只扫描AnnotationContoller包 -->
<context:include-filter type="assignable" expression="cn.itcast.spring.beans.Annotation.controller.AnnotationController"></context:include-filter>
2.@Autowired配置使用( Autowired有多种配置形式)
2.1 直接加在参数上(如果在IOC中有这样的Beab,将会自动注入,这里参数名称可以随意起)
@Autowired
private AnnotationService annotationServices;
2.2 添加到方法上)
private AnnotationService annotationService;
@Autowired
public void setAnnotationServices(AnnotationService annotationService) {
this.annotationServices = annotationServices;
}
补充:注解中的 @Autowired(requeired = false)属性,如果在IOC容器中未找到该Bean,则不会抛出异常,只会输出NULL
@Autowired(required = false)
private NomalAnnotation nomalAnnotation;
2.3 出现多重重复配置的时候, 别名一定要和注解一致 不然自动匹配不来(这里一定要注意,参数名称不能乱取!!!
保证注解名称一致 即使去掉set方法也是一样
2.4 与注解名称不匹配会抛出异常(本来是anntationService)
2.5 解决多个同注解层的解析方式:
2.5.1 也可以将@Qualifier加到方法参数上
@Autowired
public void setAnnotationServices(@Qualifier("annotationService")AnnotationService annotationServices) {
this.annotationServices = annotationServices;
}
关于自动扫描,注解配置形式小编就之讲这么多了,日后不断学习,不断完善,如有讲错的或者讲的不好的地方欢迎大家指正