1
核心注解
@Required 此注解用于bean的setter方法上。表示此属性是必须的,必须在配置阶段注入,否则会抛出BeanInitializationExcepion。 @Autowired 此注解用于bean的field、setter方法以及构造方法上,显式地声明依赖。根据type来autowiring。 当在field上使用此注解,并且使用属性来传递值时,Spring会自动把值赋给此field。也可以将此注解用于私有属性(不推荐),如下。@Component
public class User {
@Autowired
private Address address;
}
最经常的用法是将此注解用于settter上,这样可以在setter方法中添加自定义代码。如下:
@Component
public class User {
private Address address;
@AutoWired
public setAddress(Address address) {
// custom code
this.address=address;
}
}
当在构造方法上使用此注解的时候,需要注意的一点就是一个类中只允许有一个构造方法使用此注解。
@Component
public class User {
private Address address;
public User(Address address) {
this.address=address;
}
}
"user" class="xx.User"/>
@Qualifier
@Component
public class User {
@Autowired
@Qualifier("address1")
private Address address;
...
}
@Configuration
此注解用在class上来定义bean。其作用和xml配置文件相同,表示此bean是一个Spring配置。此外,此类可以使用@Bean注解来初始化定义bean。
@Configuartion
public class SpringCoreConfig {
@Bean
public AdminUser adminUser() {
AdminUser adminUser = new AdminUser();
return adminUser;
}
}
@ComponentScan
此注解一般和@Configuration注解一起使用,指定Spring扫描注解的package。如果没有指定包,那么默认会扫描此配置类所在的package。
@Lazy 此注解使用在Spring的组件类上。默认的,Spring中Bean的依赖一开始就被创建和配置。如果想要延迟初始化一个bean,那么可以在此类上使用Lazy注解,表示此bean只有在第一次被使用的时候才会被创建和初始化。 此注解也可以使用在被@Configuration注解的类上,表示其中所有被@Bean注解的方法都会延迟初始化。
@Value 此注解使用在字段、构造器参数和方法参数上。@Value可以指定属性取值的表达式,支持通过#{}使用SpringEL来取值,也支持使用${}来将属性来源中(Properties文件、本地环境变量、系统属性等)的值注入到bean的属性中。 推荐大家看下: Java 必须掌握的 12 种 Spring 常用注解,这篇也是必看了。
2
Spring MVC和REST注解
@Controller 此注解使用在class上声明此类是一个Spring controller,是@Component注解的一种具体形式。@Controller
@RequestMapping("/users")
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String getUserList() {
return "users";
}
}
这篇也推荐大家看下:Spring MVC常用注解。
此
外,Spring4
.3之后引入了一系列@RequestMapping的变种。如下:
@GetMapping @PostMapping @PutMapping @PatchMapping @DeleteMapping
分别对应了相应method的RequestMapping配置。
关注微信公众号:Java技术栈,在后台回复:spring,可以获取我整理的 N 篇最新 Spring 教程,都是干货。 @CookieValue 此注解用在@RequestMapping声明的方法的参数上,可以把HTTP cookie中相应名称的cookie绑定上去。@ReuestMapping("/cookieValue")
public void getCookieValue(@CookieValue("JSESSIONID") String cookie){
}
cookie即http请求中name为JSESSIONID的cookie值。
@CrossOrigin 此注解用在class和method上用来支持跨域请求,是Spring 4.2后引入的。
@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/users")
public class AccountController {
@CrossOrigin(origins = "http://xx.com")
@RequestMapping("/login")
public Result userLogin() {
// ...
}
}
@ExceptionHandler
// GET /pets/42;q=11;r=22
@RequestMapping(value = "/pets/{petId}")
public void findPet(@PathVariable String petId, @MatrixVariable int q) {
// petId == 42
// q == 11
}
需要注意的是默认Spring mvc是不支持矩阵变量的,需要开启。
enable-matrix-variables="true" />
注解配置则需要如下开启:
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
@PathVariable
@RequestMapping("/users/{uid}")
可以使用@PathVariable将路径中的参数绑定到请求方法参数上。
@RequestMapping("/users/{uid}")
public String execute(@PathVariable("uid") String uid){
}
关注微信公众号:Java技术栈,在后台回复:spring,可以获取我整理的 N 篇最新 Spring 系列程,都是干货。
@ModelAttribute("user")
public PUser getUser() {}
// controller和上面的代码在同一controller中
@Controller
@SeesionAttributes(value = "user", types = {
User.class
})
public class UserController {}
3
Spring Boot注解
@EnableAutoConfiguration 此注解通常被用在主应用class上,告诉Spring Boot自动基于当前包添加Bean、对bean的属性进行设置等。 @SpringBootApplication 此注解用在Spring Boot项目的应用主类上(此类需要在base package中)。 使用了此注解的类首先会让Spring Boot启动对base package以及其sub-pacakage下的类进行component scan。 这篇整理的也非常全:Spring Boot 最核心的 25 个注解,建议大家看下。 此注解同时添加了以下几个注解:@Configuration @EnableAutoConfiguration @ComponentScan
4
Stereotype注解
@Component 此注解使用在class上来声明一个Spring组件(Bean), 将其加入到应用上下文中。 @Controller 前文已经提到过 @Service 此注解使用在class上,声明此类是一个服务类,执行业务逻辑、计算、调用内部api等。是@Component注解的一种具体形式。 @Repository 此类使用在class上声明此类用于访问数据库,一般作为DAO的角色。 此注解有自动翻译的特性,例如:当此种component抛出了一个异常,那么会有一个handler来处理此异常,无需使用try-catch块。5
数据访问注解
@Transactional 此注解使用在接口定义、接口中的方法、类定义或者类中的public方法上。需要注意的是此注解并不激活事务行为,它仅仅是一个元数据,会被一些运行时基础设施来消费。6
任务执行、调度注解
@Scheduled 此注解使用在方法上,声明此方法被定时调度。使用了此注解的方法返回类型需要是Void,并且不能接受任何参数。@Scheduled(fixedDelay=1000)
public void schedule() {
}
@Scheduled(fixedRate=1000)
public void schedulg() {
}
第二个与第一个不同之处在于其不会等待上一次的任务执行结束。
@Async 此注解使用在方法上,声明此方法会在一个单独的线程中执行。不同于Scheduled注解,此注解可以接受参数。 使用此注解的方法的返回类型可以是Void也可是返回值。但是返回值的类型必须是一个Future。
7
测试注解
@ContextConfiguration 此注解使用在Class上,声明测试使用的配置文件,此外,也可以指定加载上下文的类。 此注解一般需要搭配SpringJUnit4ClassRunner使用。@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringCoreConfig.class)
public class UserServiceTest {
}
作者:Java程序媛环环
https://blog.csdn.net/Lubanjava/article/details/100579554
- END -点击「阅读原文」和栈长学更多~