applicationContext.xml配置文件加入:
<context:annotation-config/>
作用是隐式地向 Spring 容器注册
AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、
PersistenceAnnotationBeanPostProcessor 以及
RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。
注册这4个 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。
<!--
设置需要进行Spring注解扫描的类包 -->
<context:component-scan
base-package="xx.xx"
/> 可以用*代表所有
@Service()
publicclass ServiceImpl
implements Service
调用时:
@Resource()
private Service
Service;
这样的话就能实现自动注入了
@Service一般用来定义Service层 dao,action都有对应的注解网上可查
本文介绍了如何在Spring中使用applicationContext.xml配置文件启用注解支持,并通过<context:annotation-config/>来自动注册处理@Autowired等注解的BeanPostProcessors。此外,还讲解了如何使用<context:component-scan>指定扫描路径,实现@Service等注解的类自动发现。
3358

被折叠的 条评论
为什么被折叠?



