spring2.5注解介绍

  • 格式:pdf
  • 大小:279.89 KB
  • 文档页数:12

【分享】Spring2.5注解介绍Auther:韩群峰Version: 1.0.0Date:2011-03-15ContentsAgenda3注解说明3注解说明3注解说明3注解说明4注解说明4注解说明4注解说明4注解说明4注解说明5注解说明5启用Spring MVC注解5框架中用到的注解5 @Controller6 @Service6 @Autowired6 @Autowired6 @RequestMapping6 @RequestMapping7 @RequestMapping7 @RequestParam7请求处理方法入参的可选类型(1)7请求处理方法入参的可选类型(2)8请求处理方法入参的可选类型(3)8请求处理方法返回值的可选类型(1)8请求处理方法返回值的可选类型(2)8请求处理方法返回值的可选类型(3)9请求处理方法返回值的可选类型(4)9 @ModelAttribute9 @ModelAttribute9 @Cacheable和@CacheFlush9自定义注解10 @UserOperateLog10其它注解10 @Resource10 @Resource10@PostConstruct和@PreDestroy10 @Repository11 @Component(不推荐使用)11 @Scope11 @SessionAttributes11 @SessionAttributes11 @InitBinder11 @InitBinder12参考资料12Agenda•Goal•注解说明•框架中用到的注解•自定义注解•其它注解注解说明•使用注解之前bean的声明与注入方式•bean<bean id="userManagerImpl"class="erManagerImpl"> <property name="userDao"ref="userDao"/> </bean><bean id="userDao"class="erDaoImpl"> <propertyname="sessionFactory"ref="mySessionFactory"/></bean>注解说明•使用注解之前bean的声明与注入方式•byName<beans xmlns="/schema/beans"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans-2.5.xsd"default-autowire="byName">注解说明•使用注解之前bean的声明与注入方式•byType<beans xmlns="/schema/beans"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans-2.5.xsd"default-autowire="byType">注解说明•使用注解之前bean的声明与注入方式•以上方式都需要在代码中包含set方法才能实现注入public class UserManagerImpl implements UserManager {private UserDao userDao;public void setUserDao(UserDao userDao) {erDao = userDao;}注解说明•注册注解处理器•方式一:bean<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>注解说明•注册注解处理器•方式二:命名空间<context:annotation-config /><context:annotationconfig/>将隐式地向Spring容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。

注解说明•注册注解处理器•方式三:命名空间<context:component-scan />如果要使注解工作,则必须配置component-scan,实际上不需要再配置annotation-configbase-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-scan base-package="qin" ><context:include-filter type="annotation"expression="org.springframework.stereotype.Controller"/><context:include-filter type="annotation"expression="org.springframework.stereotype.Service"/><context:include-filter type="annotation"expression="org.springframework.stereotype.Repository"/></context:component-scan>启用Spring MVC注解•启动Spring MVC的注解功能,完成请求和注解POJO的映射•<beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>框架中用到的注解•@Controller•@Service•@Autowired•@RequestMapping•@RequestParam•@ModelAttribute•@Cacheable•@CacheFlush@Controller•例如@Controllerpublic class SoftCreateController extends SimpleBaseController {}•或者@Controller("softCreateController")•说明@Controller负责注册一个bean到spring上下文中,bean的ID默认为类名称开头字母小写@Service•例如@Servicepublic class SoftCreateServiceImpl implements ISoftCreateService {}•或者@Service("softCreateServiceImpl")•说明@Service负责注册一个bean到spring上下文中,bean的ID默认为类名称开头字母小写@Autowired•例如@Autowiredprivate ISoftPMService softPMService;•或者@Autowired(required=false)private ISoftPMService softPMService = new SoftPMServiceImpl();@Autowired•说明@Autowired根据bean类型从spring上线文中进行查找,注册类型必须唯一,否则报异常与@Resource的区别在于,@Resource允许通过bean名称或bean类型两种方式进行查找@Autowired(required=false)表示,如果spring上下文中没有找到该类型的bean时,才会使用new SoftPMServiceImpl();@RequestMapping•类@RequestMapping("/bbtForum.do")public class BbtForumController {@RequestMapping(params = "method=listBoardTopic")public String listBoardTopic(int topicId,User user) {}}@RequestMapping•方法@RequestMapping("/softpg/downSoftPg.do")@RequestMapping(value="/softpg/ajaxLoadSoftId.do",method = POST)@RequestMapping(value = "/osu/product/detail.do", params = { "modify=false" }, method = POST)•说明@RequestMapping可以声明到类或方法上@RequestMapping•参数绑定说明如果我们使用以下的 URL 请求:http://localhost/bbtForum.do?method=listBoardTopic&topicId=1&userId=10&userName=tomtopicId URL 参数将绑定到 topicId 入参上,而 userId 和 userName URL 参数将绑定到 user 对象的 userId 和 userName 属性中。