Spring3注解介绍.docx
上传人:sy****28 上传时间:2024-09-13 格式:DOCX 页数:15 大小:23KB 金币:14 举报 版权申诉
预览加载中,请您耐心等待几秒...

Spring3注解介绍.docx

Spring3注解介绍.docx

预览

免费试读已结束,剩余 5 页请下载文档后查看

14 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

注解说明•注册注解处理器•方式一:bean<beanclass="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>•方式二:命名空间<context:annotation-config/><context:annotationconfig/>将隐式地向Spring容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。•方式三:命名空间<context:component-scan/>如果要使注解工作,则必须配置component-scan,实际上不需要再配置annotation-config。base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。还允许定义过滤器将基包下的某些类纳入或排除。•Spring支持以下4种类型的过滤方式:•注解org.example.SomeAnnotation将所有使用SomeAnnotation注解的类过滤出来•类名指定org.example.SomeClass过滤指定的类•正则表达式com.kedacom.spring.annotation.web..*通过正则表达式过滤一些类•AspectJ表达式org.example..*Service+通过AspectJ表达式过滤一些类•正则表达式的过滤方式举例:<context:component-scanbase-package="com.casheen.spring.annotation"><context:exclude-filtertype="regex"expression="com.casheen.spring.annotation.web..*"/></context:component-scan>•注解的过滤方式举例:<context:component-scanbase-package="com.netqin"><context:include-filtertype="annotation"expression="org.springframework.stereotype.Controller"/><context:include-filtertype="annotation"expression="org.springframework.stereotype.Service"/><context:include-filtertype="annotation"expression="org.springframework.stereotype.Repository"/></context:component-scan>启用SpringMVC注解•启动SpringMVC的注解功能,完成请求和注解POJO的映射•<beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>注解介绍•@Controller•@Service•@Autowired•@RequestMapping•@RequestParam•@ModelAttribute•@Cacheable•@CacheFlush•@Resource•@PostConstruct•@PreDestroy•@Repository•@Component(不推荐使用)•@Scope•@SessionAttributes•@InitBinder•@Required•@Qualifier@Controller•例如@ControllerpublicclassSoftCreateControllerextendsSimpleBaseController{}•或者@Controller("softCreateController")•说明@Controller负责注册一个bean到spring上下文中,bean的ID默认为类名称开头字母小写@Service•例如@ServicepublicclassSoftCreateServiceImplimplementsISoftCreateService{}•或者@Service(