好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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 根目录,注解在方法上的是相对于类上的路径

?

1

2

3

4

5

6

7

@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的英文含义是合格者的意思,通过此注解,标注那个实现类才是这次要用到的实现类。如:

?

1

2

3

4

5

6

7

8

9

10

11

12

@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,要哪一个实现类,代码如下

?

1

2

3

@Autowired

@Qualifier ( "service" )

EmployeeService employeeService;

此处是service,而不是service1。

@Bean 与@Configuration标注类配合使用,等同于xml文件配置的bean。如:

?

1

2

3

4

< bean id = "user" class = "com.zhang.bean.User" >

      < property name = "userName" value = "zhangsan" ></ property >

      < property name = "age" value = "26" ></ property >

</ bean >

等同于

?

1

2

3

4

5

6

7

@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注解绑定它传过来的值到方法的参数上,如:

?

1

2

3

4

5

6

7

8

@Controller

@RequestMapping ( "/user/{Id}" )

public class DemoController {

   @RequestMapping ( "/pets/{petId}" )

   public void queryPetByParam( @PathVariable String Id, @PathVariable String petId) {   

     // implementation

   }

}

以上就是Spring框架学习常用注解汇总的详细内容,更多关于Spring框架注解的资料请关注其它相关文章!

原文链接:https://blog.csdn.net/guoyp2126/article/details/110221840

查看更多关于Spring框架学习常用注解汇总的详细内容...

  阅读:15次