Spring注解⼤全
⼀、Spring bean注解
1.1、@SpringBootApplication
申明让spring boot⾃动给程序进⾏必要的配置,这个配置等同于:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置。
1.2、@Component
泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使⽤@Component来标注这个类,把普通pojo实例化到spring容器中,相当于配置⽂件中的:<bean id="" class=""/>。
所以可以理解为@Component可以细化为@Reposity,@Service,@Controller。
@Component("conversionImpl")
//其实默认的spring中的Bean id 为 conversionImpl(⾸字母⼩写)
public class ConversionImpl implements Conversion {
@Autowired
private RedisClient redisClient;
}
1.3、@ComponentScan
@ComponentScan主要就是定义扫描的路径从中出标识了需要装配的类⾃动装配到spring的bean容器中。前⾯说到过@Controller注
解,@Service,@Repository注解,它们其实都是组件,属于@Component注解,⽽@ComponentScan注解默认会装配标识了
@Controller,@Service,@Repository,@Component注解的类到spring容器中。
//扫描com.demo下的组件
@ComponentScan(value="com.demo")
@Configuration
public class myConfig {
}
1.4、@Service
⼀般⽤于修饰service层的组件
@Service()
public class UserService{
@Resource
private UserDao userDao;
public void add(){
userDao.add();
}
}
1.5、@EnableAutoConfiguration
SpringBoot⾃动配置(auto-configuration):尝试根据你添加的jar依赖⾃动配置你的Spring应⽤。例如,如果你的classpath下存在HSQLDB,并且你没有⼿动配置任何数据库连接beans,那么我们将⾃动配置⼀个内存型(in-memory)数据库。你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到⼀个@Configuration类上来选择⾃动配置。如果发现应⽤了你不想要的特定⾃动配置类,你可以使⽤@EnableAutoConfiguration注解的排除属性来禁⽤它们。
1.6、@Configuration
Spring3.0,相当于传统的xml配置⽂件,如果有些第三⽅库需要⽤到xml⽂件,建议仍然通过@Configuration类作为项⽬的配置主类可以使⽤@ImportResource 注解加载xml配置⽂件。
proxyBeanMethods属性默认值是true,也就是说该配置类会被代理(CGLIB),在同⼀个配置⽂件中调⽤其它被@Bean注解标注的⽅法获取对象时会直接从IOC 容器之中获取。
注意:@Configuration注解的配置类有如下要求:
@Configuration不可以是final类型;
@Configuration不可以是匿名类;
嵌套的configuration必须是静态类。
@Configuration
public class AppConfig {
// 未指定bean 的名称,默认采⽤的是 "⽅法名" + "⾸字母⼩写"的配置⽅式
@Bean
public MyBean myBean(){
return new MyBean();
}
}
1.7、@ConfigurationProperties
前缀定义了哪些外部属性将绑定到类的字段上
根据 Spring Boot 宽松的绑定规则,类的属性名称必须与外部属性的名称匹配
我们可以简单地⽤⼀个值初始化⼀个字段来定义⼀个默认值
类本⾝可以是包私有的
类的字段必须有公共 setter ⽅法
激活 @ConfigurationProperties
只有当类所在的包被 Spring @ComponentScan 注解扫描到才会⽣效,默认情况下,该注解会扫描在主应⽤类下的所有包结构。
可以通过添加 @Component 注解让 Component Scan 扫描到。
@Component
@ConfigurationProperties(prefix = "demo")
class DemoProperties {
}
也可以通过 Spring 的 Java Configuration 特性实现同样的效果。
@Configuration
class PropertiesConfig {
@Bean
public DemoProperties demoProperties(){
return new DemoProperties();
}
}
使⽤ @EnableConfigurationProperties 注解让我们的类被 Spring Boot 所知道,在该注解中其实是⽤了@Import(EnableConfigurationPropertiesImportSelecto r.class) 实现
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
class PropertiesConfig {
}
Duration
Spring Boot 内置⽀持从配置参数中解析 durations (持续时间),给出了明确的说明。
配置 duration 不写单位,默认按照毫秒来指定,我们也可已通过 @DurationUnit 来指定单位。常⽤单位如下:
ns for nanoseconds (纳秒)
us for microseconds (微秒)
ms for milliseconds (毫秒)
s for seconds (秒)
m for minutes (分)
h for hours (时)
d for days (天)
@Data
@ConfigurationProperties(prefix = "demo")
class DemoProperties {
@DurarionUnit(ChronoUnit.SECONDS)
private Duration timeout;
}
DataSize
与 Duration 的⽤法⼀⽑⼀样,默认单位是 byte (字节),可以通过 @DataSizeUnit 单位指定
但是,我测试的时候打印出来结果都是以 B (bytes) 来显⽰。常见单位如下
B for bytes
KB for kilobytes
MB for megabytes
GB for gigabytes
TB for terabytes
1.8、@Import
@Import注解是引⼊带有@Configuration的java类。
@Configuration
@Import({MyImportSelector.class, MyImportBeanDefinitionRegistrar.class, MyImportSelector1.class})
public static class Config {
@Bean
public BeanDemo0 beanDemo() {
System.out.println("----beanDemo----");
springboot结构BeanDemo0 beanDemo0 = new BeanDemo0();
beanDemo0.setId(1);
beanDemo0.setName("123");
return beanDemo0;
}
}
1.9、@ImportResource
@ImportResource是引⼊spring配置⽂件.xml
@Configuration
@ImportResource(locations = {"l"})
public class BeanConfigTest {
}
1.10、@Resource、@Autowired和@Inject
@Resource和@Autowired都是做bean注⼊时使⽤。
@Resource的作⽤相当于@Autowired,只不过@Autowired按照byType⾃动注⼊。
1、共同点
两者都可以写在字段和setter⽅法上。两者如果都写在字段上,那么就不需要再写setter⽅法。
2、不同点
1.10.1、@Autowired
Spring 2.5 引⼊。@Autowired为Spring提供的注解,需要导⼊包org.springframework.beans.factory.annotation.Autowired,只按照byType注⼊。
@Autowired注解是按照类型(byType)装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它的required属性为false。如果我们想使⽤按照名称(byName)来装配,可以结合@Qualifier注解⼀起使⽤。
public class TestServiceImpl {
// 下⾯两种@Autowired只要使⽤⼀种即可
@Autowired
private UserDao userDao; // ⽤于字段上
@Autowired
public void setUserDao(UserDao userDao) { // ⽤于属性的⽅法上
this.userDao = userDao;
}
@Autowired
@Qualifier("userDao")
private UserDao userDao;
}
1.10.2、@Resource
@Resource默认按照ByName⾃动注⼊,由J2EE提供,是JSR250规范的实现,需要导⼊javax.annotation实现注⼊。
@Resource有两个重要的属性:name和type,⽽Spring将@Resource注解的name属性解析为bean的名字,⽽type属性则解析为bean的类型。所以,如果使⽤name属性,则使⽤byName的⾃动注⼊策略,⽽使⽤type属性时则使⽤byType⾃动注⼊策略。如果既不制定name也不制定type属性,这时将通过反射机制使⽤byName⾃动注⼊策略。
public class TestServiceImpl {
// 下⾯两种@Resource只要使⽤⼀种即可
@Resource(name="userDao")
private UserDao userDao; // ⽤于字段上
@Resource(name="userDao")
public void setUserDao(UserDao userDao) {
// ⽤于属性的setter⽅法上
this.userDao = userDao;
}
}
注:最好是将@Resource放在setter⽅法上,因为这样更符合⾯向对象的思想,通过set、get去操作属性,⽽不是直接去操作属性。
@Resource装配顺序:
如果同时指定了name和type,则从Spring上下⽂中到唯⼀匹配的bean进⾏装配,不到则抛出异常。
如果指定了name,则从上下⽂中查名称(id)匹配的bean进⾏装配,不到则抛出异常。
如果指定了type,则从上下⽂中到类似匹配的唯⼀bean进⾏装配,不到或是到多个,都会抛出异常。
如果既没有指定name,⼜没有指定type,则⾃动按照byName⽅式进⾏装配;如果没有匹配,则回退为⼀个原始类型进⾏匹配,如果匹配则⾃动装配。
1.10.3、@Inject
@Inject是JSR330 (Dependency Injection for Java)中的规范,需要导⼊javax.inject.Inject;实现注⼊。@Inject可以作⽤在变量、setter⽅法、构造函数上。根据类型进⾏⾃动装配的,如果需要按名称进⾏装配,则需要配合@Named。
@Named("XXX") 中的 XXX是 Bean 的名称,所以 @Inject和 @Named结合使⽤时,⾃动注⼊的策略就从 byType 转变成 byName 了。
public class User{
private Person person;
@Inject
pbulic void setPerson(Person person){
this.person = person;
}
@Inject
pbulic void setPerson1(@Named("main")Person person)
{
this.person = person;
}
}
1.11、@Repository
⽤于注解dao层,在daoImpl类上⾯注解。
1.12、@Bean
产⽣⼀个Bean对象,然后这个Bean对象交给Spring管理。⽤@Bean标注⽅法等价于XML中配置的bean。
产⽣这个Bean对象的⽅法Spring只会调⽤⼀次,随后这个Spring将会将这个Bean对象放在⾃⼰的IOC容器中。SpringIOC 容器管理⼀个或者多个bean,这些bean都需要在@Configuration注解下进⾏创建,在⼀个⽅法上使⽤@Bean注解就表明这个⽅法需要交给Spring进⾏管理。
@Bean
public class UserTest(){
public User getUser(){
System.out.println("创建user实例");
return new User("张三",26);
}
}
@Bean 注解的属性有:value、name、autowire、initMethod、destroyMethod。
name 和 value 两个属性是相同的含义的,在代码中定义了别名。为 bean 起⼀个名字,如果默认没有写该属性,那么就使⽤⽅法的名称为该 bean 的名称。autowire指定 bean 的装配⽅式,根据名称和根据类型装配,⼀般不设置,采⽤默认即可。autowire指定的装配⽅式有三种Autowire.NO (默认设置)、Autowire.BY_NAME、Autowire.BY_TYPE。
initMethod和destroyMethod指定bean的初始化⽅法和销毁⽅法,直接指定⽅法名称即可,不⽤带括号。
public class MyBean {
public MyBean(){
System.out.println("MyBean Initializing");
}
public void init(){
System.out.println("Bean 初始化⽅法被调⽤");
}
public void destroy(){
System.out.println("Bean 销毁⽅法被调⽤");
}
}
@Configuration
public class AppConfig {
@Bean(initMethod = "init", destroyMethod = "destroy")
public MyBean myBean(){
return new MyBean();
}
}
1.13、@Scope
@Scope注解默认的singleton单例模式。
@Scope注解是springIoc容器中的⼀个作⽤域,在 Spring IoC 容器中具有以下⼏种作⽤域:基本作⽤域singleton(单例)、prototype(多例),Web 作⽤域(reqeust、session、globalsession),⾃定义作⽤域。
prototype原型模式:
@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)在每次注⼊的时候回⾃动创建⼀个新的bean实例
singleton单例模式:
@Scope(value=ConfigurableBeanFactory.SCOPE_SINGLETON)单例模式,在整个应⽤中只能创建⼀个实例
globalsession模式:
@Scope(value=WebApplicationContext.SCOPE_GLOBAL_SESSION)全局session中的⼀般不常⽤
@Scope(value=WebApplicationContext.SCOPE_APPLICATION)在⼀个web应⽤中只创建⼀个实例
request模式:
@Scope(value=WebApplicationContext.SCOPE_REQUEST)在⼀个请求中创建⼀个实例
session模式:
@Scope(value=WebApplicationContext.SCOPE_SESSION)每次创建⼀个会话中创建⼀个实例
@Configuration
public class myConfig {
//默认是单例的。不需要特别说明
@Bean("person")
public Person person(){
return new Person("binghe002", 18);
}
}
@Configuration
public class myConfig {
//Person对象的作⽤域修改成prototype,多例的
@Scope("prototype")
@Bean("person")
public Person person(){
return new Person("binghe002", 18);
}
}
1.14、@Value
@Value的作⽤是通过注解将常量、配置⽂件中的值、其他bean的属性值注⼊到变量中,作为变量的初始值。
(1)、普通注⼊
@Value("张三")
private String name; // 注⼊普通字符串
(2)、bean属性、系统属性、表达式注⼊,使⽤@Value("#{}")。bean属性注⼊需要注⼊者和被注⼊者属于同⼀个IOC容器,或者⽗⼦IOC容器关系,在同⼀个作⽤域内。
// 注⼊其他Bean属性:注⼊beanInject对象的属性another,类具体定义见下⾯
@Value("#{beanInject.another}")
private String fromAnotherBean;
// 注⼊操作系统属性
@Value("#{systemProperties['os.name']}")
private String systemPropertiesName;
//注⼊表达式结果
@Value("#{T(java.lang.Math).random() * 100.0 }")
private double randomNumber;
(3)、配置⽂件属性注⼊@Value("${}")
@Value("#{}")读取配置⽂件中的值,注⼊到变量中去。配置⽂件分为默认配置⽂件application.properties和⾃定义配置⽂件。
1.15、@RestController
Spring4,注解是@Controller和@ResponseBody的合集,表⽰这是个控制器bean,并且是将函数的返回值直接填⼊HTTP响应体中,是REST风格的控制器。
@RestController
@RequestMapping(“/demoInfo2”)
publicclass DemoController2 {
@RequestMapping("/test")
public String test(){
return "ok";
}
}
1.16、@Controller
在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把⽤户请求的数据经过业务处理层处理之后封装成⼀个Model ,然后再把该Model 返回给对应的View 进⾏展⽰。在SpringMVC 中提供了⼀个⾮常简便的定义Controller 的⽅法,你⽆需继承特定的类或实现特定的接⼝,只需使⽤
@Controller 标记⼀个类是Controller ,然后使⽤@RequestMapping 和@RequestParam 等⼀些注解⽤以定义URL 请求和Controller ⽅法之间的映射,这样的Controller 就能被外界访问到。此外Controller 不会直接依赖于HttpServletRequest 和HttpServletResponse 等HttpServlet 对象,它们可以通过Controller 的⽅法参数灵活的获取到。
⽤于定义控制器类,在spring 项⽬中由控制器负责将⽤户发来的URL请求转发到对应的服务接⼝(service层),⼀般这个注解在类中,通常⽅法需要配合注解@RequestMapping。⽰例代码:
@Controller
@RequestMapping(“/demoInfo”)
public class DemoController {
@Autowired
private DemoInfoService demoInfoService;
@RequestMapping("/hello")
public String hello(Map<String,Object> map){
System.out.println("DemoController.hello()");
map.put("hello","from TemplateController.helloHtml");
//会使⽤hello.html或者hello.ftl模板进⾏渲染显⽰.
return"/hello";
}
}
1.17、@RequestMapping
提供路由信息,是⼀个⽤来处理请求地址映射的注解,可⽤于类或⽅法上。⽤于类上,表⽰类中的所
有响应请求的⽅法都是以该地址作为⽗路径。RequestMapping注解有六个属性,下⾯我们把她分成三类进⾏说明(下⾯有相应⽰例)。
1、 value, method;
value:指定请求的实际地址,指定的地址可以是URI Template 模式(后⾯将会说明);
method:指定请求的method类型, GET、POST、PUT、DELETE等;
2、consumes,produces
consumes:指定处理请求的提交内容类型(Content-Type),例如application/json, text/html; produces:    指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
3、params,headers
params:指定request中必须包含某些参数值是,才让该⽅法处理。
headers:指定request中必须包含某些指定的header值,才能让该⽅法处理请求。
@RestController
@RequestMapping("/home")
public class IndexController {
/**
* 将多个请求映射到⼀个⽅法上去
*/
@RequestMapping(value = {
“”,
“/page”,
"page*”,
"view/*,**/msg"
})
String indexMultipleMapping() {
return "Hello from index multiple mapping.”;
}
/**
* 是否是必须传参
* /home/name?person=xyz 或 /home/name
*/
@RequestMapping(value = “/name”)
String getName(@RequestParam(value = "person”, required = false) String personName) {
return "Required element of request param”;
}
/
**
* 请求类型,请求参数,默认值
*/
@RequestMapping(value = "/name", method = RequestMethod.GET)
String getName(@RequestParam(value = "person", defaultValue = "John") String personName) {        return "Required element of request param";
}
/**
* 产⽣⼀个 JSON 响应
*/
@RequestMapping(value = "/prod", produces = {
"application/JSON"
})
@ResponseBody
String getProduces() {
return "Produces attribute";
}
/**
* 可以同时处理请求中的 JSON 和 XML 内容
*/
@RequestMapping(value = "/cons", consumes = {
"application/JSON",
"application/XML"
})
String getConsumes() {
return "Consumes attribute";
}
/**
* 根据请求中的消息头内容缩⼩请求映射的范围
*/
@RequestMapping(value = “/head”, headers = {
"content-type=text/plain”,
"content-type=text/html"
})
String post() {
return "Mapping applied along with headers”;
}
/**
* 可以让多个处理⽅法处理到同⼀个URL 的请求, ⽽这些请求的参数是不⼀样的
*/
@RequestMapping(value = “/fetch”, params = {
"personId=10"
})
String getParams(@RequestParam(“personId”) String id) {
return "Fetched parameter using params attribute = " + id;
}
/**
* 使⽤正则表达式来只处理可以匹配到正则表达式的动态 URI
*/
@RequestMapping(value = “/fetch/{id:[a-z]+}/{name}”, method = RequestMethod.GET)