SpringBoot注解整理一(基础篇)

  |   0 评论   |   0 浏览

一、基础注解

  @SpringBootApplication:这是个复合注解,申明让spring boot自动给程序进行必要的配置,这个配置等同于:@Configuration(通过@SpringBootConfiguration引入) ,@EnableAutoConfiguration 和 @ComponentScan 三个配置。

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan 
public class Application { 
  public static void main(String[] args) { 
   SpringApplication.run(Application.class, args); 
  } 
}

  @Configuration:等同于spring的XML配置文件,是java配置的基础注解,官方给出的注释如下:

Indicates that a class declares one or more {@link Bean @Bean} methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
指明一个类定义了一个或多个bean方法,这些bean在系统运行时候能被spring bean容器管理或者被其他服务注入。
 代码样例:
 
 @Configuration
 public class AppConfig {
     @Bean
     public MyBean myBean() {
         // instantiate, configure and return bean ...
     }
}

  @EnableAutoConfiguration:让Spring Boot根据类路径中的jar包依赖为当前项目进行自动配置。例如添加了spring-boot-starter-web依赖,Spring Boot会自动添加Tomcat和Spring MVC的依赖,然后Spring Boot会对Tomcat和Spring MVC进行自动配置。
  @ComponentScan:这个注解完成的是自动扫描的功能,相当于Spring XML配置文件中的:context:component-scan,可以使用basePackages属性指定要扫描的包,以及扫描的条件。如果不设置的话默认扫描@ComponentScan注解所在类的同级类和同级目录下的所有类,所以对于一个Spring Boot项目,一般会把入口类放在顶层目录中,这样就能够保证源码目录下的所有类都能够被扫描到。
  @Import:用来导入其他配置类,那些没有被spring自动发现的类,可以采用这个注解直接导入到spring的bean容器中管理,在自定义注解的时候会用到这个注解。
  @ImportResource:用来加载xml配置文件,如下所示

@ImportResource("classpath:beans.xml")
@SpringBootApplication
public class Application { 
  public static void main(String[] args) { 
   SpringApplication.run(Application.class, args); 
  } 
}

  @PropertySource:导入properties配置文件。

@PropertySource(value = { "classpath:/jdbc.properties"})
public class XXXClass { 
}

二、Bean注入相关注解

  声明Bean的注解:
  @Bean:java配置的核心注解,作用于method,在有@Configuration注解的类中,用@Bean标注方法等价于XML中配置的bean。
  @Component:作用于class,组件,没有明确的功能定义。
  @Service:作用于class,服务,在业务逻辑层使用。
  @Repository:作用于class,DAO,在数据访问层使用。
  @Contoller:作用于class,MVC中的C。
  注入Bean的注解:
  @Autowired:Spring提供的注解。
  @Inject:JSR-330提供的注解。
  @Resource:JSR-250提供的注解。
  其他:
  @Scope:可以写在method和class上,指定bean的作用范围(生成规则),目前主要有五类:

 @Configuration
 public class AppConfig {
     @Bean
	  @Scope("prototype")
     public MyBean myBean() {
         // instantiate, configure and return bean ...
     }
}

1、Singleton:默认,单例,Spring容器中只有一个类对象。
2、Prototype:原型模式,每次调用都会生成一个新的bean实例。
3、Request:Web项目中,每个http request对应一个bean实例。
4、Session:Web项目中,每个session会话对应一个bean实例。
5、GlobalSession:Portal应用中,没一个global session对应一个bean实例。
  @Value:基于SpringEL表达式的属性值注入注解,注入bean的某个属性值。

	@Value("I Love U!")
	private String normal; //注入普通字符

	@Value("#{systemProperties['os.name']}")
	private String osName; //注入系统环境变量

	@Value("#{T(java.lang.Math).random()*100.0}")
	private double randomNum; //注入表达式运算结果

	@Value("#{demoService.another}")
	private String fromAnother; //注入其他bean属性

	@Value("classpath:io/github/junxworks/demo/ch2/el/mytest.txt")
	private Resource testFile;//注入文件内容

	@Value("http://www.baidu.com")
	private Resource testUrl;//注入网址内容

	@Value("${book.name:test}")
	private String bookName;//注入配置文件内容【最常用】

  @Profile:环境匹配,配置在不同环境下生效的bean。需要配合spring的spring.profiles.active配置使用,目前环境定义一般有3类,即开发环境dev、测试环境test、生产环境prod。

package io.github.junxworks.demo.ch2.profile;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class ProfileConfig {

	@Bean
	@Profile("dev")
	public DemoBean devBean() {
		return new DemoBean("dev");
	}

	@Bean
	@Profile("prod")
	public DemoBean prodBean() {
		return new DemoBean("prod");
	}
}

  @Qualifier:当有多个同一类型的Bean时,可以用@Qualifier(“name”)来指定。与@Autowired配合使用。@Qualifier限定描述符除了能根据名字进行注入,但能进行更细粒度的控制如何选择候选者,具体使用方式如下:

@Autowired 
@Qualifier(value = “demoInfoService”) 
private DemoInfoService demoInfoService;

  @DependsOn:该注解用于声明当前bean依赖于另外一个bean。所依赖的bean会被容器确保在当前bean实例化之前被实例化,如下:

    @Bean(name = "entityManager")
    @DependsOn("transactionManager")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Throwable {    
        LocalContainerEntityManagerFactoryBean entityManager = 
	        new LocalContainerEntityManagerFactoryBean();
	    // 省略无关的实现部分
	    // ...
        return entityManager;
    }

三、Spring MVC相关注解

  @Controller:作用于类上,表名这个类是一个controller。
  @RequestMapping:@RequestMapping(“/path”)表示该控制器处理所有“/path”的UR L请求。RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。
用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。该注解有六个属性:
params:指定request中必须包含某些参数值是,才让该方法处理。
headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。
value:指定请求的实际地址,指定的地址可以是URI Template 模式
method:指定请求的method类型, GET、POST、PUT、DELETE等
consumes:指定处理请求的提交内容类型(Content-Type),如application/json,text/html;
produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回
  @ResponseBody:作用于类或者方法上,表示方法返回值将直接写入到response中,而不是返回页面。
  @RequestBody:作用于参数前,表示允许Request的参数在request体中,而不是直接链接在地址后面,如:

RequestMapping(“user/get/mac”) 
public String getByMacAddress(@RequestBody UserDto dto){ //do something; 
}

  @RequestParam:用在方法的参数前面,指明入参的属性。

RequestMapping(“user/get/mac”) 
public String getByMacAddress(@RequestParam(value = "macAddress", required = true) String macAddress){ //do something; 
}

  @PathVariable:路径变量。如

RequestMapping(“user/get/mac/{macAddress}”) 
public String getByMacAddress(@PathVariable String macAddress){ //do something; 
}

  @RestController:是@Controller和@ResponseBody的复合注解,常用于前后台分离的业务场景,表示controller内方法返回值都是直接返回的数据而不是页面。
  @GetMapping:简化版的RequestMapping,method只支持get,类似的还有@PostMapping等等,对应了http的method。

四、@Enable*注解

  @EnableAspectJAutoProxy:开启对AspectJ自动代理的支持,基于AspectJ很容易实现AOP操作,下面是使用AspectJ实现的一个简单的AOP(包含注解式和方法规则表达式式)样例:

@EnableAspectJAutoProxy
@SpringBootApplication
public class Application { 
 public static void main(String[] args) { 
  SpringApplication.run(Application.class, args); 
 } 
}

1、定义注解

package io.github.junxworks.demo.ch1.aop;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
	String name();
}

2、定义拦截器

package io.github.junxworks.demo.ch1.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {

	@Pointcut("@annotation(io.github.junxworks.demo.ch1.aop.Action)")
	public void annotationPointCut() {
	}

	@After("annotationPointCut()")
	public void after(JoinPoint joinPoint){
		MethodSignature signature=(MethodSignature)joinPoint.getSignature();
		Method method = signature.getMethod();
		Action action = method.getAnnotation(Action.class);
		System.out.println("注解式拦截:"+action.name());
	}

	@Before("execution(* io.github.junxworks.demo.ch1.aop.DemoMethodService.*(..))")
	public void before(JoinPoint joinPoint){
		MethodSignature signature=(MethodSignature)joinPoint.getSignature();
		Method method = signature.getMethod();
		System.out.println("方法规则拦截:"+method.getName());
	}
}

3、被方法拦截的bean

package io.github.junxworks.demo.ch1.aop;

import org.springframework.stereotype.Service;

@Service
public class DemoAnnotationService {
	@Action(name="注解式拦截的add操作")
	public void add(){
		System.out.println("das add");
	}
}

  @EnableAsync:开启异步方法的支持,也就是说,spring注入的bean,在调用@Async注解方法的时候,会采用单独的线程池去执行,属于异步方法调用,具体示例如下:

@EnableAsync
@SpringBootApplication
public class Application { 
 public static void main(String[] args) { 
  SpringApplication.run(Application.class, args); 
 } 
}

1、定义线程池和异常处理类

@Configuration
@EnableAsync
public class TaskExecutorConfig implements AsyncConfigurer {

	@Override
	public Executor getAsyncExecutor() {
		// TODO Auto-generated method stub
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(10);
		executor.setMaxPoolSize(10);
		executor.setQueueCapacity(25);
		executor.initialize();
		return executor;
	}

	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		// TODO Auto-generated method stub
		return null;
	}

}

2、定义异步执行的bean方法

@Service
public class AsyncTaskService {
	@Async
	public void executeAsyncTask(Integer i) {
		System.out.println(Thread.currentThread().getName());
	}

}

  @EnableScheduling:开启定时任务的支持,此处仅仅是进程内的定时任务,如果需要完善的定时任务框架支持,例如支持集群等功能,请选择quartz(或者更强大的Azkaban)。

@EnableAsync
@SpringBootApplication
public class Application { 
 public static void main(String[] args) { 
  SpringApplication.run(Application.class, args); 
 } 
}
package io.github.junxworks.demo.ch3.taskscheduler;

import java.text.SimpleDateFormat;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class ScheduledTaskService {
   private static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

   @Scheduled(fixedRate = 5000)
   public void reportCurrentTime() {
   	System.out.println("Every 5 seconds:" + sdf.format(System.currentTimeMillis()));
   }

   @Scheduled(cron = "10 38 21 ? * *")
   public void fixTimeExecution() {
   	System.out.println("21:38:10 every day:" + sdf.format(System.currentTimeMillis()));
   }
}

  @EnableWebMvc:是使用Java 注解快捷配置Spring Webmvc的一个注解,开启项目对Web MVC的配置支持。在使用该注解后配置一个继承于WebMvcConfigurerAdapter的配置类即可配置好Spring Webmvc,通过查看@EnableWebMvc的源码,可以发现该注解就是为了引入一个DelegatingWebMvcConfiguration Java 配置类,并翻看DelegatingWebMvcConfiguration的源码会发现该类似继承于WebMvcConfigurationSupport的类,其实不使用@EnableWebMvc注解也是可以实现配置Web MVC,只需要将配置类继承于WebMvcConfigurationSupport类即可。
  @EnableConfigurationProperties:开启对@ConfigurationProperties注解配置Bean的支持,这个是用于规范化配置的注解,将配置跟Java Bean关联起来,同时可以结合spring-boot-configuration-processor一起使用,代码样例如下:

@Configuration
@EnableConfigurationProperties({ MyConfig.class })
public class Beans {
	@Bean(name = "nettyServer", initMethod = "start", destroyMethod = "stop")
	public XXXXX xxBean(MyConfig config) throws IOException {
		String a=config.getA(); //Bean注入时候直接使用
		//xxxxxxx
		return XXXXX;
	}
}
@ConfigurationProperties(prefix = "junxworks.test")
public class MyConfig {
	private String a;
	private String b;

	public String getA() {
		return a;
	}

	public void setA(String a) {
		this.a = a;
	}

	public String getB() {
		return b;
	}

	public void setB(String b) {
		this.b = b;
	}

}

  @EnableJpaRepositories:开启对Spring Data JPA Repository的支持。
  @EnableTransactionManagement:开启注解式事务的支持。
  @EnableCaching:开启注解式的缓存支持,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使用的抽象,通过在既有代码中添加少量它定义的各种 annotation,即能够达到缓存方法的返回对象的效果。Spring 的缓存技术还具备相当的灵活性,不仅能够使用 SpEL(Spring Expression Language)来定义缓存的 key 和各种 condition,还提供开箱即用的缓存临时存储方案,也支持和主流的专业缓存例如 EHCache 集成。

五、JPA注解

  @Entity:@Table(name=”“):表明这是一个实体类。一般用于jpa这两个注解一般一块使用,但是如果表名和实体类名相同的话,@Table可以省略
  @MappedSuperClass:用在确定是父类的entity上。父类的属性子类可以继承。
  @NoRepositoryBean:一般用作父类的repository,有这个注解,spring不会去实例化该repository。
  @Column:如果字段名与列名相同,则可以省略。
  @Id:表示该属性为主键。
  @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = “repair_seq”):表示主键生成策略是sequence(可以为Auto、IDENTITY、native等,Auto表示可在多个数据库间切换),指定sequence的名字是repair_seq。
  @SequenceGeneretor(name = “repair_seq”, sequenceName = “seq_repair”, allocationSize = 1):name为sequence的名称,以便使用,sequenceName为数据库的sequence名称,两个名称可以一致。
  @Transient:表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性。如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则,ORM框架默认其注解为@Basic。@Basic(fetch=FetchType.LAZY):标记可以指定实体属性的加载方式
  @JsonIgnore:作用是json序列化时将Javabean中的一些属性忽略掉,序列化和反序列化都受影响。
  @JoinColumn(name=”loginId”):一对一:本表中指向另一个表的外键。一对多:另一个表指向本表的外键。
  @OneToOne、@OneToMany、@ManyToOne:对应hibernate配置文件中的一对一,一对多,多对一。

六、全局异常处理

  @ControllerAdvice:统一处理异常。
  @ExceptionHandler(Exception.class):用在方法上面表示遇到这个异常就执行以下方法。

@RestControllerAdvice
public class GlobalExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@ExceptionHandler(BaseRuntimeException.class)
	public Result handleRRException(BaseRuntimeException e) {
		return Result.error(getCause(e));
	}

	@ExceptionHandler(Exception.class)
	public Result handleException(Exception e) {
		logger.error(e.getMessage(), e);
		return Result.error(getCause(e));
	}

	private String getCause(Throwable e) {
		Throwable t = e;
		while (t.getCause() != null) {
			t = t.getCause();
		}
		return t.getMessage();
	}
}

标题:SpringBoot注解整理一(基础篇)
作者:michael
地址:https://blog.junxworks.cn/articles/2019/03/10/1552183307301.html