1@EnableAsync
Spring框架中用于开启异步方法执行支持的注解,其核心作用是通过代理机制实现方法的异步调用
核心功能
开启异步支持:在Spring主配置类(如启动类)上添加@EnableAsync,框架会自动注册异步代理处理器,使标注@Async的方法异步执行。
配置线程池:通过实现AsyncConfigurer接口或配置TaskExecutionProperties,可自定义线程池参数(如核心线程数、队列容量等)。
2.@EnableConfigurationProperties
pring Boot中用于绑定配置属性的注解,主要作用是注册配置属性类为Bean并完成属性绑定
核心功能
-
注册Bean
将带有:ml-text[@ConfigurationProperties]注解的类注册为Spring Bean,确保其能被容器管理。 -
属性绑定
通过:ml-text[ConfigurationPropertiesBindingPostProcessor]后置处理器,在Bean初始化时自动将配置文件(如:ml-text[application.properties])中的属性值注入到类字段中
点击查看代码
@ConfigurationProperties(prefix = "my.config")
public class MyConfig {private String key;// getter/setter
}
点击查看代码
@EnableConfigurationProperties(MyConfig.class)
@Configuration
public class Config {
}
``` :ml-citation{ref="1,2" data="citationList"}
Spring Boot 中,若某类只用 @ConfigurationProperties 注解,然后该类:
没有在扫描路径下
或没用 @Component 等注解
就会导致无法被扫描为 bean,须在配置类用 @EnableConfigurationProperties 注解去指定这个类,才能使 @ConfigurationProperties 生效,并作为一个 bean 添加进 Spring 容器。
@EnableConfigurationProperties 不能单独使用:
@EnableConfigurationProperties 和 @ConfigurationProperties组合使用
@EnableConfigurationProperties将@ConfigurationProperties所修饰的类添加到IoC容器
https://www.cnblogs.com/JavaEdge/p/18354084
3.@Resource 摘自https://blog.csdn.net/java_wxid/article/details/131340720
@Resource注解是Java EE规范中的一种注入方式,用于进行依赖注入
它能够实现自动注入各种类型的依赖项,包括JNDI对象、EJB组件、Web服务和其他类型的资源。
它支持多种注入方式,包括按名称注入、按类型注入和按描述符注入
按名称注入是指通过@Resource(name=“{resource-name}”)指定注入的名称,然后在容器中查找名称与指定名称匹配的组件,进行注入。
点击查看代码
public class MyClass {@Resource(name = "myDependency")private MyDependency myDependency;// rest of the class
}
按类型注入是指通过@Resource(type=“{resource-type}”)指定注入的类型,然后在容器中查找类型与指定类型匹配的组件,进行注入。
点击查看代码
public class MyClass {@Resource(type = MyDependency.class)private MyDependency myDependency;// rest of the class
}
按描述符注入是指通过@Resource(mappedName=“{jndi-name}”)指定注入的描述符,然后在容器中查找描述符与指定描述符匹配的组件,进行注入。
点击查看代码
public class MyClass {@Resource(mappedName = "java:global/myapp/MyDependency")private MyDependency myDependency;// rest of the class
}
使用mappedBy属性:
点击查看代码
public class MyClass {@Resource(mappedName = "java:global/myapp/MyEJB")private MyEJB myEjb;@Resource(mappedName = "java:global/myapp/MyEJB/myDependency")private MyDependency myDependency;// rest of the class
}
使用shareable属性
点击查看代码
public class MyClass {@Resource(mappedName = "java:comp/env/jdbc/myDatabase", shareable = true)private Connection conn;// rest of the class
}
@Resource注解是Java EE中常用的依赖注入注解之一,它可以用于类、字段、方法和构造函数上,在不同作用域中进行依赖注入
4.@AllArgsConstructor
Lombok库提供的注解,用于自动生成包含类中所有非静态字段的全参数构造函数,可显著减少Java开发中重复的样板代码
适用于需要快速创建对象并初始化全部属性的场景,如DTO、实体类或依赖注入
5.@NoArgsConstructor
生成无参构造函数,常用于框架反射调用(如Hibernate)
6.@RequiredArgsConstructor
仅生成包含final或@NonNull字段的构造函数,适合依赖注入的字段选择性初始化。
*@Data注解默认包含@RequiredArgsConstructor,但不包含@AllArgsConstructor和@NoArgsConstructor,需显式补充。
在springboot中,对于一个bean类,注入其他bean得时候,最常见得时候是使用@Autowired,实际上也可以使用构造函数注入,这个时候可以使用@RequiredArgsConstructor和@AllArgsConstructor来代替
转自 @NoArgsConstructor、@AllArgsConstructor、@RequiredArgsConstructor的区别以及在springboot常用地方
点击查看代码
@Component
public class BeanTest1 {
}@Component
public class BeanTest2 {
}@Component
public class BeanTest3 {
}@Component
@AllArgsConstructor
@ToString
public class ConstructorDemo {// 注入三个bean对象,完全没有使用Autowired注解private BeanTest1 beanTest1;@NonNullprivate BeanTest2 beanTest2;private final BeanTest3 beanTest3;
}// 这只是个测试类
@Component
public class ConstructorRunner implements ApplicationRunner {@AutowiredConstructorDemo ConstructorDemo;@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println(ConstructorDemo);}
}
点击查看代码
@Component
@ToString
public class ConstructorDemo {private BeanTest1 beanTest1;@NonNullprivate BeanTest2 beanTest2;private BeanTest3 beanTest3;@Value("${constructor.name:hello}")private String name;}先思考一下:
一个bean如果使用构造函数进行bean属性注入,那么当然构造函数不能加上name。
因为加上,在创建ConstructorDemo该bean的时候,需要找类型为String,名字是name的bean对象,当然是不存在,必然会报错。
因此,当然不能使用@AllArgsConstructor了,只能使用@RequiredArgsConstructor
所以,在一个@Component或者@Configuration配置得类上(bean上)要谨慎使用@AllArgsConstructor进行bean注入.以防存在string等不是bean类型得变量注入报错。