当前位置:网站首页>@Configuration注解的详细介绍
@Configuration注解的详细介绍
2022-07-07 21:53:00 【TPH-BETTER】
转自https://www.cnblogs.com/init-qiancheng/p/14623522.html
@Target({
ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(
annotation = Component.class
)
String value() default "";
boolean proxyBeanMethods() default true;
}
由该注解定义可知,该注解首先是一个组件,所以会被@SpringBootApplication注解扫描(扫描的前提是该注解必须在与启动类相同包或者子包中) 该注解中有一个属性 proxyBeanMethods,该属性的默认值是true,这个属性可以分为两种模式
- Full模式(即为默认值):proxyBeanMethods = true;
- Lite模式:proxyBeanMethods = false;
Full模式与Lite模式的区别
使用Full模式,这个配置类就被代理,就可以解决组件之间的依赖关系,他会先检查容器中是否存在组件的实例,如果存在就会直接使用,不存在才会进行创建;
如果是Lite模式,就不会产生代理类,每次调用都会执行一次,SpringBoot会跳过在容器中查找的步骤;
什么时候使用Full和Lite
当没有组件依赖的时候就是用Lite,这样会使程序启动更快;
当有组件依赖的时候就是用Full,即为默认值;
MyConfig.java
/** * 1、配置类里面使用@Bean标注在方法上给容器注册组件,默认也是单实例的 * 2、配置类本身也是组件 * 3、proxyBeanMethods:代理bean的方法 * Full(proxyBeanMethods = true)、【保证每个@Bean方法被调用多少次返回的组件都是单实例的】 * Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】 * 组件依赖必须使用Full模式默认。其他默认是否Lite模式 */
@Configuration(proxyBeanMethods = false)//告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {
/** * Full:外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象 * * @return */
@Bean//给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
public People people() {
return new People(1, "迁承", dog());
}
@Bean
public Dog dog() {
return new Dog(1, "嘻嘻");
}
}
启动类
@SpringBootApplication
public class QianchengApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(QianchengApplication.class, args);
/** * 使用Full模式的时候会输出 * [email protected]7aae97b * true */
/** * 使用Late模式的时候输出 * [email protected] * false */
MyConfig bean = context.getBean(MyConfig.class);
System.out.println(bean);
People people = context.getBean("people", People.class);
Dog dog1 = context.getBean("dog", Dog.class);
System.out.println(people.getDog() == dog1);
}
}
边栏推荐
- Explain
- FPGA basics catalog
- MySQL Index Optimization Practice II
- Slam interview summary
- 2022第六季完美童模陕西总决赛圆满落幕
- SAP HR reward and punishment information export
- 通达信买基金安全吗?
- B_QuRT_User_Guide(36)
- Anxinco EC series modules are connected to the multi protocol access products of onenet Internet of things open platform
- Live server usage
猜你喜欢
Take you hand in hand to build Eureka client with idea
Summary of SQL single table query 2020.7.27
Anxin can internally test offline voice module vb-01 to communicate with esp-c3-12f
Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function
平衡二叉樹【AVL樹】——插入、删除
Extended tree (I) - graphic analysis and C language implementation
2022 certified surveyors are still at a loss when preparing for the exam? Teach you how to take the exam hand in hand?
New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
Deep understanding of MySQL lock and transaction isolation level
Markdown
随机推荐
As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response
SAP memory parameter tuning process
Display the server hard disk image to the browser through Servlet
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
Given an array, such as [7864, 284, 347, 7732, 8498], now you need to splice the numbers in the array to return the "largest possible number."
Design and implementation of spark offline development framework
【路径规划】使用垂距限值法与贝塞尔优化A星路径
What if once again forgets the login password of raspberry pie? And you don't have a monitor yet! Today, I would like to introduce a method
HDU 4747 mex "recommended collection"
UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)
Have all the fresh students of 2022 found jobs? Is it OK to be we media?
Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function
B_ QuRT_ User_ Guide(36)
How can we make money by making video clips from our media?
648. Word replacement
USB (XVII) 2022-04-15
Possible SQL for Oracle table lookup information
2022 届的应届生都找到工作了吗?做自媒体可以吗?
网上买基金安全么?