spring常用注解

上传人:第*** 文档编号:32818412 上传时间:2018-02-12 格式:DOCX 页数:17 大小:24.94KB
返回 下载 相关 举报
spring常用注解_第1页
第1页 / 共17页
spring常用注解_第2页
第2页 / 共17页
spring常用注解_第3页
第3页 / 共17页
spring常用注解_第4页
第4页 / 共17页
spring常用注解_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《spring常用注解》由会员分享,可在线阅读,更多相关《spring常用注解(17页珍藏版)》请在金锄头文库上搜索。

1、Spring4.1.6 常用注解ControllerServiceAutowiredRequestMappingRequestParamModelAttributeCacheableCacheFlushResourcePostConstructPreDestroyRepositoryComponent (不推荐使用)ScopeSessionAttributesInitBinderRequiredQualifierController 例如Controllerpublic class SoftCreateController extends SimpleBaseController 或者Cont

2、roller(userController) 说明Controller 负责注册一个 bean 到 spring 上下文中,bean 的 ID 默认为类名称开头字母小写 Service 例如Servicepublic class SoftCreateServiceImpl implements ISoftCreateService 或者Service(softCreateServiceImpl) 说明Service 负责注册一个 bean 到 spring 上下文中,bean 的 ID 默认为类名称开头字母小写Autowired 例如Autowiredprivate ISoftPMServic

3、e softPMService; 或者Autowired(required=false)private ISoftPMService softPMService = new SoftPMServiceImpl(); 说明Autowired 根据 bean 类型从 spring 上线文中进行查找,注册类型必须唯一,否则报异常。 与Resource 的区别在于,Resource 允许通过 bean 名称或 bean 类型两种方式进行查找Autowired(required=false) 表示,如果 spring 上下文中没有找到该类型的bean 时, 才会使用 new SoftPMServiceI

4、mpl();Autowired 标注作用于 Map 类型时,如果 Map 的 key 为 String 类型,则 Spring 会将容器中所有类型符合 Map 的 value 对应的类型的 Bean 增加进来,用 Bean 的 id 或 name 作为 Map 的 key。Autowired 还有一个作用就是,如果将其 标注在 BeanFactory 类型、ApplicationContext 类型、 ResourceLoader 类型、ApplicationEventPublisher 类型、MessageSource 类型上,那么 Spring 会自动注入这些实现类的实例,不需要额外的操作

5、。RequestMapping 类ControllerRequestMapping(/bbtForum.do) copyright public class BbtForumController RequestMapping(params = method=listBoardTopic)public String listBoardTopic(int topicId,User user) 方法RequestMapping(/softpg/downSoftPg.do)RequestMapping(value=/softpg/ajaxLoadSoftId.do,method = POST)Req

6、uestMapping(value = /osu/product/detail.do, params = modify=false , method =POST) 说明RequestMapping 可以声明到 类或方法上 参数绑 定说明如果我们使用以下的 URL 请求:http:/localhost/itxxzSpring4?method=listBoardTopic&topicId=1&userId=10&userName=tom topicId URL 参数将绑定到 topicId 入参上,而 userId 和 userName URL 参数将绑定到 user 对象的 userId 和 u

7、serName 属性中。和 URL 请求中不允许没有 topicId 参数不同,虽然 User 的 userId 属性的类型是基本数据类型,但如果 URL 中不存在 userId 参数,Spring 也不会报错,此时 user.userId 值为 0 。如果 User 对象拥有一个 dept.deptId 的级联属性,那么它将和 dept.deptId URL 参数绑定。RequestParam 参数绑 定说明RequestParam(id)http:/localhost/itxxzSpring4?method=listBoardTopic&id=1&userId=10&userName=to

8、mlistBoardTopic(RequestParam(id)int topicId,User user) 中的 topicId 绑定到 id 这 个 URL 参数, 那么可以通过对入参使用 RequestParam 注解来达到目的 RequestParam(required=false):参数不是必须的,默认为 trueRequestParam(value=id,required=false)请求处理方法入参的可选类型 Java 基本数据类型和 String默认情况下将按名称匹配的方式绑定到 URL 参数上,可以通过 RequestParam 注解改变 默认的绑定规则 request/re

9、sponse/session既可以是 Servlet API 的也可以是 Portlet API 对应的对象,Spring 会将它们绑定到 Servlet 和 Portlet 容器的相应对象上 copyright org.springframework.web.context.request.WebRequest内部包含了 request 对象 java.util.Locale 内容来自 绑定到 request 对应的 Locale 对象上 java.io.InputStream/java.io.Reader可以借此访问 request 的内容 java.io.OutputStream /

10、 java.io.Writer可以借此操作 response 的内容 任何标 注了 RequestParam 注解的入参 被标注 RequestParam 注解的入参将绑定到特定的 request 参数上。 java.util.Map / org.springframework.ui.ModelMap它绑定 Spring MVC 框架中每个请求所创建的潜在的模型对象,它们可以被 Web 视图对象访问(如 JSP ) 命令 / 表单对象(注:一般称绑定使用 HTTP GET 发送的 URL 参数的对象为命令对象,而称绑定使用 HTTP POST 发送的 URL 参数的对 象为表单对象)它 们的属

11、性将以名称匹配的规则绑定到 URL 参数上,同时完成类型的转换。而 类型转换的规则可以通过 InitBinder 注解或通过 HandlerAdapter 的配置进行调 整 org.springframework.validation.Errors / org.springframework.validation.BindingResult为 属性列表中的命令/ 表单对象的校验结果,注意检验结果参数必须紧跟在命令/ 表单对象的后面 IT 学习者() org.springframework.web.bind.support.SessionStatus可以通过该类型 status 对象显式结束表单

12、的处理,这相当于触发 session 清除其中的通过SessionAttributes 定义的属性 请求处理方法返回值的可选类型 void此时逻辑视图名由请求处理方法对应的 URL 确定,如以下的方法:RequestMapping(/welcome.do)public void welcomeHandler() 对应的逻辑视图名为 “ welcome ” String此时逻辑视图名为返回的字符,如以下的方法:RequestMapping(method = RequestMethod.GET)public String setupForm(RequestParam(ownerId) int ow

13、nerId, ModelMap model) Owner owner = this.clinic.loadOwner(ownerId);model.addAttribute(owner);return ownerForm;对应的逻辑视图名为 “ ownerForm ” org.springframework.ui.ModelMap和返回类型为 void 一样,逻辑视图名取决于对应请求的 URL ,如下面的例子:RequestMapping(/vets.do)public ModelMap vetsHandler() return new ModelMap(this.clinic.getVets

14、();对应的逻辑视图名为 “ vets ” ,返回的 ModelMap 将被作为请求对应的模型对象,可以在 JSP 视图页 面中访问到。 ModelAndView当然还可以是传统的 ModelAndView 。ModelAttribute 作用域: request 例如RequestMapping(/base/userManageCooper/init.do)public String handleInit(ModelAttribute(queryBean) ManagedUser sUser,Model model,) 或者ModelAttribute(coopMap)/ 将 coopMap

15、 返回到页 面public Map coopMapItems() 说明ModelAttribute 声明在属性上,表示该属性的 value 来源于 model 里queryBean ,并被保存到 model 里ModelAttribute 声明在方法上,表示该方法的返回值被保存到 model 里 Cacheable 和CacheFlush Cacheable :声明一个方法的返回值应该被缓 存例如:Cacheable(modelId = testCaching) CacheFlush :声明一个方法是清空缓存的触发器例如:CacheFlush(modelId = testCaching) 说明要配合缓存处理器使用Resource 例如Resourceprivate DataSource dataSource; / inject the bean named dataSource 或者Resource(name=dataSource)Resource(type=DataSource.class) 说明Resource 默认按 bean 的 na

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号