当前位置:网站首页>@Detailed introduction of configuration annotation
@Detailed introduction of configuration annotation
2022-07-07 23:44:00 【TPH-BETTER】
from 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;
}
According to the definition of this annotation , The annotation is first a component , So it will be @SpringBootApplication Annotation scan ( The premise of scanning is that the annotation must be in the same package or sub package as the startup class ) There is an attribute in this annotation proxyBeanMethods, The default value for this property is true, This attribute can be divided into two modes
- Full Pattern ( Is the default ):proxyBeanMethods = true;
- Lite Pattern :proxyBeanMethods = false;
Full Patterns and Lite The difference between patterns
Use Full Pattern , This configuration class is represented , You can solve the dependencies between components , He will first check whether there are instances of components in the container , If it exists, it will be used directly , If it doesn't exist, it will be created ;
If it is Lite Pattern , There will be no proxy class , Each call will be executed once ,SpringBoot Will skip the steps of searching in the container ;
When to use Full and Lite
When there is no component dependency, it is used Lite, This will make the program start faster ;
When there is component dependency, it is used Full, Is the default ;
MyConfig.java
/** * 1、 The configuration class uses @Bean Label the method to register the component to the container , The default is also single instance * 2、 The configuration class itself is also a component * 3、proxyBeanMethods: agent bean Methods * Full(proxyBeanMethods = true)、【 Make sure that each @Bean Method is called many times and the returned component is single instance 】 * Lite(proxyBeanMethods = false)【 Every @Bean Method is called many times and the returned component is newly created 】 * Component dependencies must use Full Mode default . Other defaults are Lite Pattern */
@Configuration(proxyBeanMethods = false)// tell SpringBoot This is a configuration class == The configuration file
public class MyConfig {
/** * Full: No matter how many times you call the component registration method in the configuration class, you get the single instance object in the previous registration container * * @return */
@Bean// Add components to the container . Using the method name as the name of the component id. The return type is the component type . The value returned , Is an instance of a component in a container
public People people() {
return new People(1, " Transference ", dog());
}
@Bean
public Dog dog() {
return new Dog(1, " Hee hee ");
}
}
Start class
@SpringBootApplication
public class QianchengApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(QianchengApplication.class, args);
/** * Use Full Mode will output * [email protected]7aae97b * true */
/** * Use Late Mode output * [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);
}
}
边栏推荐
- An example analysis of MP4 file format parsing
- Anti climbing means cracking the second
- SAP HR reward and punishment information export
- MySQL Architecture
- Open source hardware small project: anxinco esp-c3f control ws2812
- HB 5469民用飞机机舱内部非金属材料燃烧试验方法
- Possible SQL for Oracle table lookup information
- Dataguard 主备清理归档设置
- 解析token的网址
- Ora-02437 failed to verify the primary key violation
猜你喜欢
Map operation execution process
【LeetCode】20、有效的括号
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
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."
Idea automatically generates serialVersionUID
Take you hand in hand to build feign with idea
SAP HR labor contract information 0016
FFA与ICGA造影
postgis学习
Open source hardware small project: anxinco esp-c3f control ws2812
随机推荐
P2141 [noip2014 popularization group] abacus mental arithmetic test
一份假Offer如何盗走了「Axie infinity」5.4亿美元?
SAP HR 劳动合同信息 0016
数据库面试题+解析
C cat and dog
Summary of SQL single table query 2020.7.27
How to change the formula picture in the paper directly into the formula in word
[stm32+esp8266 connects to Tencent cloud IOT development platform 3] stm32+esp8266-01s dynamically registers devices on Tencent cloud (at instruction mode) -- with source code
Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function
【leetcode】day1
激光slam学习(2D/3D、偏实践)
【汇总】看过的一些Panel与视频
Right click the idea file to create new. There is no solution to create new servlet
Chisel tutorial - 00 Ex.scala metals plug-in (vs Code), SBT and coursier exchange endogenous
C number of words, plus ¥, longest word, average value
List. How to achieve ascending and descending sort() 2020.8.6
StringUtils工具类
P1067 [noip2009 popularity group] polynomial output (difficult, pit)
SAP HR奖罚信息导出
MP4文件格式解析之结合实例分析