1
核心注解
@Component
public class User {
@Autowired
private Address address;
}
@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;
}
}
<bean id="user" class="xx.User"/>
@Component
public class User {
@Autowired
@Qualifier("address1")
private Address address;
...
}
@Configuration
@Configuartion
public class SpringCoreConfig {
@Bean
public AdminUser adminUser() {
AdminUser adminUser = new AdminUser();
return adminUser;
}
}
@ComponentScan
2
Spring MVC和REST注解
@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配置。
@ReuestMapping("/cookieValue")
public void getCookieValue(@CookieValue("JSESSIONID") String cookie){
}
@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/users")
public class AccountController {
@CrossOrigin(origins = "http://xx.com")
@RequestMapping("/login")
public Result userLogin() {
// ...
}
}
// GET /pets/42;q=11;r=22
@RequestMapping(value = "/pets/{petId}")
public void findPet(@PathVariable String petId, @MatrixVariable int q) {
// petId == 42
// q == 11
}
<mvc:annotation-driven 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);
}
}
此注解使用在请求handler方法的参数上。@RequestMapping可以定义动态路径,如:
@RequestMapping("/users/{uid}")
@RequestMapping("/users/{uid}")
public String execute(@PathVariable("uid") String uid){
}
@ModelAttribute("user")
public PUser getUser() {}
// controller和上面的代码在同一controller中
@Controller
@SeesionAttributes(value = "user", types = {
User.class
})
public class UserController {}
3
Spring Boot注解
@Configuration @EnableAutoConfiguration @ComponentScan
4
Stereotype注解
5
数据访问注解
6
任务执行、调度注解
@Scheduled(fixedDelay=1000)
public void schedule() {
}
@Scheduled(fixedRate=1000)
public void schedulg() {
}
7
测试注解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringCoreConfig.class)
public class UserServiceTest {
}
作者:Java程序媛环环
https://blog.csdn.net/Lubanjava/article/details/100579554
点击「阅读原文」和栈长学更多~