Spring common annotation classification summary

1. Spring Basics

The Spring framework itself has four principles:

  • Lightweight and minimally invasive development with POJOs
  • Loose coupling through dependency injection and interface-based programming
  • Declarative programming with AOP and default habits
  • Reducing patterned code with AOP and templates

1.1 Dependency Injection

Annotations for declaring beans. The following four annotations are equivalent and can be used as needed.

@component //组件,没有明确角色
@Service //在业务逻辑层(service层)使用
@Repository //在数据访问层(dao层)使用
@Controller //在展现层(Spring MVC)使用

Annotation for injecting beans

@Autowire //Spring提供的注解
@Inject //JSR-330提供的注解
@Resource //JSR-250提供的注解

Java configuration

@configuration //声明为配置类
@ComponentScan //自动扫描包名下所有声明为Bean的类
@EnableAspectJAutoProxy //开启Spring对AspectJ的支持
@Bean //注解在方法上,声明当前方法的返回值为一个Bean

1.2 AOP

@Aspect  //声明为切面
@PointCut //切点,定义拦截规则
//声明一个advice
@After
@Before
@Around

2. Spring common configuration

@scope //常用的包括Singleton、Prototype、Request、Session,默认为Singleton
@Profile //为不同环境下使用不同的配置提供支持,如开发环境、生产环境

Spring EL

@Value //Spring EL表达式的注入,包括字符、URL、普通文件、配置文件等

Bean initialization and destruction

@PostConstruct //在构造函数执行完后执行
@PreDestroy //在Bean销毁之前执行

3. Spring advanced topics

Multithreading

@EnableAsync //在配置类中开启对异步任务的支持
@Async //在Bean的方法中声明其是一个异步任务

Scheduled Tasks

@EnableScheduling //在配置类中开启对计划任务的支持
@Scheduled //在Bean的方法上声明一个计划任务,支持cron、fixedDelay、fixedRate等

Conditional annotation

@Conditional

@Enable* annotation

@EnableAspectJAutoProxy //开启对AspectJ自动代理支持
@EnableAsync //开启异步方法支持
@EnableScheduling //开启计划任务支持
@EnableWebMVC //开启Web MVC的配置支持
@EnableConfigurationProperties //开启对@ConfigurationProperties注解配置Bean的支持
@EnableJpaRepositories //开启对Spring Data JPA Repository的支持
@EnableTrasactionManagement //开启注解式事务支持
@EnableCache //开启注解式的缓存支持

test

@ContextConfiguration //用来加载配置ApplicationContext
@ActiveProfile //用来声明活动的profile

The org.springframework.boot.autoconfigure.condition package is an annotation for judging whether the injection action is executed. The commonly used ones are as follows. You can see the others by yourself. Simple English:

  • ConditionalOnClass: class or method level annotation, when there is a specified class in the classpath, the spring injection action will be executed
  • ConditionalOnProperty: class or method level annotation, the spring injection action will only be executed when there is a specified configuration value in the project environment (such as application.properties).
  • ConditionalOnMissingBean: class or method level annotation. When a bean has no instance in the spring context, the spring injection action will be executed. Note that this annotation cannot guarantee the loading order of the bean. Generally, it is mixed with AutoConfigureAfter or AutoConfigureBefore to ensure that the bean is at the end an execution injection
  • ConditionalOnExpression: class or method class annotation, when a certain condition is true, such as @ConditionalOnExpression("'${test.name}'.startsWith('hello')"), it means that the environment variable contains test.name value, and starts with hello, the spring injection action will be executed

org.springframework.boot.autoconfigure包下:

  • AutoConfigureAfter: Class-level annotation, the injection action will be executed after the specified bean is injected
  • AutoConfigureBefore: Class-level annotation, the specified bean will be injected after the bean is injected

 

 

4. Spring MVC

Common Notes

 

@Controller //声明为Spring MVC里的Controller
@RequestMapping //映射Web请求、处理类和方法
@ResposeBody //支持将返回值放在response体内,而不是返回一个页面
@RequestBody //允许request参数在request体内,而不是直接链接在地址后面
@PathVariable //用来接收路径参数
@RestController //是@Controller和@ResposeBody 的组合注解

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326288113&siteId=291194637