spring中的常用注解
@Controller
标识一个该类是Spring MVC controller处理器,用来创建处理http请求的对象.
@Controller
@RequestMapping("/userControl")
public class userControl{
}
@RestController
Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseBody来配合,如果直接用@RestController替代@Controller就不需要再配置@ResponseBody,默认返回json格式。
@RestController
@RequestMapping("/userControl")
public class userControl{
@RequestMapping("/queryUserInfoById")
//@ResponseBody
public UserVo queryUserInfoById(String userId){
return UserVo;
}
}
@Service
用于标注业务层组件,说白了就是加入你有一个用注解的方式把这个类注入到spring配置中
@Repository
用于标注数据访问组件,即DAO组件
@RequestMapping
类定义处: 提供初步的请求映射信息,相对于 WEB 应用的根目录。
方法处: 提供进一步的细分映射信息,相对于类定义处的 URL。
我们一般需要在控制器类的内部编写一些响应的处理方法。要让spring知道要用哪个特定的发放来处理某个请求动作,则需要使用
- org.springframework.web.bind.annotation.RequestMapping
使用RequestMapping的注解类型映射的URI方法。
RequestMapping注释类型的作用同其名字所暗示的:映射一个请求和一种方法。可以使用@RequestMapping注释一种 方法或类。
一个采用@RequestMapping注释的方法将成为一个请求处理方法,并由调度程序在接收到对应URL请求时调用。
例如:
@Controller
@RequestMapping("/userController")
public class UserController {
@Autowired
private IUserService userServiceImpl;
@RequestMapping("/queryUserAll")
public String queryUserAll(){
System.out.println("UserController queryUserAll");
String result = userServiceImpl.queryUserAll();
System.out.println("this result is:"+result);
return "success";
}
}
请求路径如下:
http://localhost:8080/springmvc-day02/userController/queryUserAll
RequestMapping注解作用在控制器类上时
@Controller
@RequestMapping("/userController")
public class UserController {
....
}
在这种情况下,所有的方法都将映射为相对于类级别的请求。在访问UserController控制器中的方法时,都需要在前面增加/userController
例如:
http://localhost:8080/springmvc-day02/userController/queryUserAll
RequestMapping的属性使用
- value
由于value属性是RequestMapping注解的默认属性,因此如果只有唯一的属性,则可以省略属性的名称。换句话说,如下两个标注的含义是一样的。
@RequestMapping("/queryUserAll")
@RequestMapping(value="/queryUserAll")
如果有超过一个属性的时候,则value属性则是必须的。
@RequestMapping(value="/queryUserAll",method = RequestMethod.GET)
- method
指定请求的method类型, 分为GET、POST、PUT、DELETE等。也可以处理多个请求方式。如果没有method属性的时候则可以处理任意的请求。例如
@RequestMapping(value="/queryUserAll",method = {RequestMethod.PUT,RequestMethod.POST})
-
consumes
指定处理请求的提交内容类型(Content-Type),例如application/json, text/html; -
produces
指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回; -
params
指定request中必须包含某些参数值时,才让该方法处理。 -
headers
指定request中必须包含某些指定的header值,才能让该方法处理请求。
@Autowired
用来装配bean,可以写在字段上,或者方法上。
默认情况下必须要求依赖对象必须存在,如果要允许对象null值,可以设置它的required属性为false,例如:@Autowired(required=false)
自动化装配@Autowired的更详细描述请查看下列文章
链接: 《spring隐式的bean发现机制和自动化装配,基于注解的自动化装配》.
@Qualifier
当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean 将会被装配来消除奇异。
@Resource
@Resource的作用相当于@Autowired
只不过@Autowired按byType自动注入,
而@Resource默认按 byName自动注入罢了。
@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
@Resource装配顺序:
如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;
@Autowired和 @Resource注解的区别
链接: 《spring中@Autowired和 @Resource注解的区别》.
@RequestParam
用于将请求参数区数据映射到功能处理方法的参数上
例如
public Resp test(@RequestParam Integer id){
return Resp.success(customerInfoService.fetch(id));
}
这个id就是要接收从接口传递过来的参数id的值的,如果接口传递过来的参数名和你接收的不一致,也可以如下
public Resp test(@RequestParam(value="course_id") Integer id){
return Resp.success(customerInfoService.fetch(id));
}
其中course_id就是接口传递的参数,id就是映射course_id的参数名
@requestparam( required = false) 的作用
不传值后台也不会报错,但是如果@requestparam( required = false)的括号中指定了基本数据类型,例如(@requestparam(value = ‘num’ required = false) int num) 这个时候如果不传值是会报错的,因为不传值就赋null,但是int类型不能为null,解决办法,修改成Integer即可
@ModelAttribute
使用地方有三种:
- 标记在方法上。
标记在方法上,会在每一个@RequestMapping标注的方法前执行,如果有返回值,则自动将该返回值加入到ModelMap中。
A. 在有返回的方法上:
当ModelAttribute设置了value,方法返回的值会以这个value为key,以参数接受到的值作为value,存入到Model中,如下面的方法执行之后,最终相当于 model.addAttribute(“user_name”, name);假如 @ModelAttribute没有自定义value,则相当于
model.addAttribute(“name”, name);
@ModelAttribute(value = "user_name")
public String before2(@RequestParam(required = false) String name, Model model){
System.out.println("springMvcStudyController before name :" + name);
return name;
}
B. 在没返回的方法上:
需要手动model.add方法
@ModelAttribute
public void before(@RequestParam(required = false) int age, Model model){
model.addAttribute("age",age);
System.out.println("springMvcStudyController before age :" + age);
}
我们在当前类下建一个请求方法:
@Controller
@RequestMapping("/springMvcStudyController")
public class SpringMvcStudyController {
@ModelAttribute
public void before(@RequestParam(required = false) int age, Model model){
model.addAttribute("age",age);
System.out.println("springMvcStudyController before age :" + age);
}
@ModelAttribute(value = "user_name")
public String before2(@RequestParam(required = false) String name, Model model){
System.out.println("springMvcStudyController before name :" + name);
return name;
}
@RequestMapping(value = "/queryUser" , method = RequestMethod.GET)
public String queryUser(@RequestParam(required = false)String name,
@RequestParam(required = false)int age,
Model model){
System.out.println("进入QueryUser");
System.out.println("name为:"+name);
System.out.println("age为:"+age);
System.out.println("model"+model.toString());
return "success";
}
}
http://localhost:8080/study01/springMvcStudyController/queryUser?name=张三&age=18
[2020-11-05 05:35:37,599] Artifact springmvc_study01:war exploded: Artifact is deployed successfully
[2020-11-05 05:35:37,599] Artifact springmvc_study01:war exploded: Deploy took 1,928 milliseconds
springMvcStudyController before age :18
springMvcStudyController before name :张三
进入QueryUser
name为:张三
age为:18
model{age=18, user_name=张三}
- 标记在方法的参数上。
标记在方法的参数上,会将客户端传递过来的参数按名称注入到指定对象中,并且会将这个对象自动加入ModelMap中,便于View层使用.我们在上面的类中加入一个方法如下
@Controller
@RequestMapping("/springMvcStudyController")
public class SpringMvcStudyController {
@RequestMapping(value="queryUser" , method = RequestMethod.GET)
public String queryUser(@ModelAttribute(value = "name") String name,
@ModelAttribute(value = "age") String age,
Model model){
System.out.println("进入QueryUser");
System.out.println("name为:"+name);
System.out.println("age为:"+age);
System.out.println("model"+model.toString());
return "success";
}
}
http://localhost:8080/study01/springMvcStudyController/queryUser?name=张三&age=18
进入QueryUser
name为:张三
age为:18
model{name=张三, org.springframework.validation.BindingResult.name
=org.springframework.validation.BeanPropertyBindingResult: 0 errors,
age=18,org.springframework.validation.BindingResult.age
=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
@Component
泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注
@PostConstruct
用来标记是在项目启动的时候执行这个方法。用来修饰一个非静态的void()方法
也就是spring容器启动时就执行,多用于一些全局配置、数据字典之类的加载
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。PreDestroy()方法在destroy()方法执行执行之后执
@PreDestroy
被@PreDestroy修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreDestroy修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前
@Scope
用来配置 spring bean 的作用域,它标识 bean 的作用域。
默认值是单例
- singleton:单例模式,全局有且仅有一个实例
- prototype:原型模式,每次获取Bean的时候会有一个新的实例
- request:request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前- - - HTTP request内有效
- session:session作用域表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在- 当前HTTP session内有效
- global session:只在portal应用中有用,给每一个 global http session 新建一个Bean实例。
@Required
适用于bean属性setter方法,并表示受影响的bean属性必须在XML配置文件在配置时进行填充。否则,容器会抛出一个BeanInitializationException异常。
@SessionAttributes
默认情况下Spring MVC将模型中的数据存储到request域中。当一个请求结束后,数据就失效了。如果要跨页面使用。那么需要使用到session。而@SessionAttributes注解就可以使得模型中的数据存储一份到session域中
参数:
- names:这是一个字符串数组。里面应写需要存储到session中数据的名称。
- types:根据指定参数的类型,将模型中对应类型的参数存储到session中
- value:和names是一样的。
@Controller
@SessionAttributes(value={"names"},types={Integer.class})
public class ScopeService {
@RequestMapping("/testSession")
public String test(Map<String,Object> map){
map.put("names", Arrays.asList("a","b","c"));
map.put("age", 12);
return "hello";
}
}
@CacheEvict
用来标记要清空缓存的方法,当这个方法被调用后,即会清空缓存。@CacheEvict(value=”UserCache”)
参数列表
@Cacheable
用来标记缓存查询。可用用于方法或者类中,
当标记在一个方法上时表示该方法是支持缓存的,
当标记在一个类上时则表示该类所有的方法都是支持缓存的。
参数列表
比如@Cacheable(value=“UserCache”) 标识的是当调用了标记了这个注解的方法时,逻辑默认加上从缓存中获取结果的逻辑,如果缓存中没有数据,则执行用户编写查询逻辑,查询成功之后,同时将结果放入缓存中。
但凡说到缓存,都是key-value的形式的,因此key就是方法中的参数(id),value就是查询的结果,而命名空间UserCache是在spring*.xml中定义.
@Cacheable(value="UserCache")// 使用了一个缓存名叫 accountCache
public Account getUserAge(int id) {
//这里不用写缓存的逻辑,直接按正常业务逻辑走即可,
//缓存通过切面自动切入
int age=getUser(id);
return age;
}