In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
这篇文章主要为大家展示了"Spring框架学习常用注解有哪些",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Spring框架学习常用注解有哪些"这篇文章吧。
类注解
@component 标注类,泛指各种组件,类不属于各种分类的时候,用它做标注。
@Service 标注类,声明该类为业务层组件,用于处理业务逻辑
@Repositor 标注类,声明该类为持久层的接口。使用后,在启动主程序类上需要添加@MapperScan("xxx.xxx.xxx.mapper")注解
@Mapper 标注类,用在持久层的接口上,注解使用后相当于@Reponsitory加@MapperScan注解,会自动进行配置加载
@Configuration Spring3.0以上,声明该类是一个配置类,可以使用@Configuration用于定义配置类,可替换xml配置文件。被注解的类内部包含有一个或多个被@Bean注解的方法。
@Aspect 标注类 声明这个类是一个切面类
@Controller 标注类,声明该类为Spring MVC controller处理器组件,用于创建处理http请求的对象。
@RestController 标注类,声明该类为Rest风格控制器组件,该注解是Spring4之后加入的注解,用它替代@Controller就不需要再配置@ResponseBody,默认返回json格式
@RequestMapping:既可以注解在类上,也可以注解在类的方法上,该类提供初步的请求映射信息。注解在类上是相对于 Web 根目录,注解在方法上的是相对于类上的路径
@Controller@RequestMapping("/user")public class UserController { @RequestMapping("/login") public String login() { return "success"; }
此时,调用时使用:http://IP地址:端口号/网站根路径/user/login
方法或属性上注解
@Autowired 用来装配bean,可以写在字段或者方法上。默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如:@Autowired(required=false)
@Qualifier 如果一个接口有两个或者两个以上的实现类,就要使用到@Qualifier注解,qualifier的英文含义是合格者的意思,通过此注解,标注那个实现类才是这次要用到的实现类。如:
@Service("service")public class EmployeeServiceImpl implements EmployeeService { public EmployeeDto getEmployeeById(Long id) { return new EmployeeDto(); }}@Service("service1")public class EmployeeServiceImpl1 implements EmployeeService { public EmployeeDto getEmployeeById(Long id) { return new EmployeeDto(); }}
service和service1同时实现接口EmployeeService,@Autowired注入时,通过@Qualifier告诉spring,要哪一个实现类,代码如下
@Autowired@Qualifier("service")EmployeeService employeeService;
此处是service,而不是service1。
@Bean 与@Configuration标注类配合使用,等同于xml文件配置的bean。如:
等同于
@Bean public User getUser(){ User user = new User(); user.setUserName("zhangsan"), user.setAge(26), return user; }
@After、@Before、@Around:与@Aspect配合使用,直接将切点作为参数,在方法执行之后执行、之前执行及之前和之后均执行。
@RequestBody:可用在方法上,也可以用在参数上。注解在方法上,代表用户返回json数据,而不是页面。
参数注解
@RequestBody:注解在方法的参数上,代表接收的参数是来自requestBody中,即请求体。用于处理非 Content-Type: application/x-www-form-urlencoded编码格式的数据,如:application/json、application/xml等类型的数据,使用注解@RequestBody可以将body里面所有的json数据传到后端,后端再进行解析
@RequestParam:使用在方法参数参数上,接收的参数是来自HTTP请求体或请求url的QueryString中。可以接受简单类型的属性,也可以接受对象类型。@RequestParam用来处理 Content-Type 为 application/x-www-form-urlencoded 编码的内容,Content-Type默认为该属性。
@PathVariable: 使用在方法参数参数上。当@RequestMapping URI template 样式映射时, paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上,如:
@Controller@RequestMapping("/user/{Id}")public class DemoController { @RequestMapping("/pets/{petId}") public void queryPetByParam(@PathVariable String Id,@PathVariable String petId) { // implementation }}以上是"Spring框架学习常用注解有哪些"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.