欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring框架学习常用注解汇总

程序员文章站 2022-03-25 13:34:02
目录类注解@component 标注类,泛指各种组件,类不属于各种分类的时候,用它做标注。@service 标注类,声明该类为业务层组件,用于处理业务逻辑@repositor 标注类,声明该类为持久层...

类注解

@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 id="user" class="com.zhang.bean.user">
     <property name="username" value="zhangsan"></property>
     <property name="age" value="26"></property>
</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框架学习常用注解汇总的详细内容,更多关于spring框架注解的资料请关注其它相关文章!