spring常用的注解

spring常用注解

一、@Controller

1.负责DispatcherServlet的分发和请求,将用户的请求经过service层变成一个model,然后将model返回给客户端View展示,
2.用controller标记一个类,然后在类中使用@RequestMapping和@RequestParam等一些的注解用以定义URL请求和Contrller
方法之间的映射,这样cotroller接能让外界访问到。
3.其实在@controller只是定义一个控制器,真正需要请求的处理器就是@RequestMapping(请求映射)注解的方法
4.要想使Spring认识请求的处理器,那么久必须在SpringMVC中加入:
(1)SpringMVC的配置文件中定义一个MyController的Bean对象
<bean class="com.host.app.web.controller.MyController"/>
(2)SpringMVC的配置文件中在哪儿其找有@Controller的注解标记
<context:component-scan base-package="net.viralpatel.spring3.controller" />

二、@RequestMapping

1.用来请求处理地址映射的注解,可用于类或者方法上
用在类上,便是类中所有响应请求的方法都是以该地址作为父路径
RequestMapping注解有六个属性,分成三类进行说明:
(1)value、method
value:指定请求的实际地址,指定的地址可以是URI Template 模式
(2)method:指定请求的method类型,get、post、put、delete等

/**
     				* Rest 风格的 URL(以 CRUD 为例):
     				*         新增:/order POST
     				*         修改:/order/1 PUT
     				*         获取:/order/1 GET
     				*         删除:/order/1 DELETE
     				* @param id
     				* @return
     				*/
    				@RequestMapping(value = "/testRestPut/{id}", method = RequestMethod.PUT)
    				public String testRestPut(@PathVariable int id) {
       					System.out.println("testRestPut:" + id);
        				return SUCCESS;
    				}
    
    				@RequestMapping(value = "/testRestDelete/{id}", method = RequestMethod.DELETE)
   				public String testRestDelete(@PathVariable int id) {
        				System.out.println("testRestDelete:" + id);
        				return SUCCESS;
    				}
    
    				@RequestMapping(value = "/testRestPost/{id}", method = RequestMethod.POST)
    				public String testRestPost(@PathVariable int id) {
        				System.out.println("testRestPost:" + id);
        				return SUCCESS;
    				}
    
    				@RequestMapping("/testRestGet")
    				public String testRestGet() {
        				System.out.println("testRestGet");
        				return SUCCESS;
    				}

2.consumes,produces

(1)consumes:指定处理请求的提交内容类型(Content-Type),例如application/json, text/html

(2)produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回

3.params,headers

(1)params:访问参数设置,指定request中必须包含某些参数值是,才让该方法处理。

(2)headers:头域,可以设置浏览器支持的格式,指定request中必须包含某些指定的header值,才能让该方法处理请求。

另外@RequestMapping的几种使用情况:

@RequestMapping("/helloword/?/aa") 的 Ant 路径,匹配符:
			?:匹配文件名的一个字符
     		*:匹配文件名的所有字符
    		**:匹配多层路径
		@RequestMapping("/testPojo") POJO类用法:
			@RequestMapping("/testPojo")
    			public String testPojo(User user) {
        			System.out.println("testPojo:" + user);
        			return "success";
    			}
		@RequestMapping("/testPojo") Map用法:
			@RequestMapping("/testMap")
    			public String testMap(Map<String, Object> map) {
        			map.put("names", Arrays.asList("Tomcat", "Eclipse", "JavaEE"));
        			return "success";
    			}
		@RequestMapping("/testPojo") ModelAndView用法:
			@RequestMapping("/testModelAndView")
    			public ModelAndView testModelAndView() {
        			String viewName = SUCCESS;
        			ModelAndView modelAndView = new ModelAndView(viewName);
        			modelAndView.addObject("time", new Date());
        			return modelAndView;
    			}

三、常用的还有

1.@RequestParam
放在参数前,表示只能接收参数a=b格式的数据,即Content-Type为application/x-www-form-urlencoded类型的内容。
2.@RequestBody
放在参数前,表示参数从request body中获取,而不是从地址栏获取,所以这肯定是接收一个POST请求的非a=b格 式的数据,即Content-Type不为application/x-www-form-urlencoded类型的内容。
3.@ResponseBody
放在方法上或者返回类型前,表示此方法返回的数据放在response body里面,而不是跳转页面。
一般用于ajax请求,返回json数据。
4.@RestController
这个是Controller和ResponseBody的组合注解,表示@Controller标识的类里面的所有返回参数都放在response body里面。
5.@PathVariable
路径绑定变量,用于绑定restful路径上的变量。
6.@RequestHeader
放在方法参数前,用来获取request header中的参数值。
7.@CookieValue
放在方法参数前,用来获取request header cookie中的参数值。

8.==@GetMapping PostMapping PutMapping… ==
*Mapping的是Spring4.3加入的新注解,表示特定的请求类型路径映射,而不需要写 RequestMethod来指定请求类型。

演示

import org.dom4j.util.UserDataElement;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

	//注解一个类,springMVC会自动扫描标注了这个注解的类都
	@Controller

	//注解方法(或者类),请求路径映射,可以标注类,也可以指定请求类型,默认不指定为全部接受
	@RequestMapping("/test")

	public class TestController {
		//value指的是实际请求的路径,方法是获取方法
		@RequestMapping(value = "/get/{no}", method = RequestMethod.GET)
    

		//在方法前或者返回类型前,用这个表示将数据放在了response body中,而不是跳转页面。一般用于ajax请求,
		//返回接送数据。
		@ResponseBody
    
		//@PathVariable表示路径变量,用于绑定restful(不活跃的)路径上的变量
		public Object get(@PathVariable("no") String no) {
        
			return new UserDataElement("");
    
		}

		@RequestMapping(value = "/save", method = RequestMethod.POST)
		public void save(@RequestBody UserDataElement user) {
		
		}
	}

四、@Resource和@Autowired

.@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,
需要导入,但是Spring支持该注解的注入。
1.共同点
俩者都是写在字典个setter方法上的,俩这如果都写在字段上,那么就不需要在写setter方法了。
2.不同点
(1)@Autowired
可以对成员变量、方法和构造函数进行标注,来完成自动装配工作。可以消除get,set方法。
@Autowired为Spring提供的注解,需要导入包org.springframework.beans.factory.annotation.Autowired;
只按照byType注入。

public class TestServiceImpl {
    					// 下面两种@Autowired只要使用一种即可
    					@Autowired
    					private UserDao userDao; // 用于字段上
    
   					 @Autowired
   					 public void setUserDao(UserDao userDao) { // 用于属性的方法上
       						 this.userDao = userDao;
    					}
				}

@Autowired注解是按照类型(byType)装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,
可以设置它的required属性为false。如果我们想使用按照名称(byName)来装配,可以结合@Qualifier注解
一起使用。如下:

public class TestServiceImpl {
    				@Autowired
  					@Qualifier("userDao")
  					private UserDao userDao; 
				}

(2)@Resource
@Resource默认按照ByName自动注入,由J2EE提供,需要导入包javax.annotation.Resource。
@Resource有两个重要的属性:name和type,而Spring将@Resource注解的name属性解析为bean的名字,
而type属性则解析为bean的类型。所以,如果使用name属性,则使用byName的自动注入策略,
而使用type属性时则使用byType自动注入策略。如果既不制定name也不制定type属性,
这时将通过反射机制使用byName自动注入策略。

public class TestServiceImpl {
    						// 下面两种@Resource只要使用一种即可
    						@Resource(name="userDao")
    						private UserDao userDao; // 用于字段上
    
    						@Resource(name="userDao")

   		 				public void setUserDao(UserDao userDao) { // 用于属性的setter方法上
        						this.userDao = userDao;
    						}
					}

:最好是将@Resource放在setter方法上,因为这样更符合面向对象的思想,通过set、get去操作属性,而不是直接去操作属性。
@Resource装配顺序:
①如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配, 找不到则抛出异常。
②如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常。
③如果指定了type,则从上下文中找到类似匹配的唯一bean进行装配,找不到或是找到多个, 都会抛出异常。
④如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配。
@Resource的作用相当于@Autowired,只不过@Autowired按照byType自动注入。

其实还有很多很多,多学习!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值