Don't read this article, just one for Xiaobai. The preface of this article will continue to be updated
@Bean:
Adding components to the container generally works on methods, and the effect is the same as writing bean tags in the configuration file, and then the method name is equivalent to the id name;
So in the main startup class, how to get it?
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
In this way, we get all the bean s, and then we get the components through getBean("component id", entity class). Class) and get the return value;
Cat cat1 = run.getBean("cat", Cat.class); System.out.println(cat1);
@SpringBootApplication:
@There are three core annotations in the SpringBootApplication annotation: @ SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan annotation; It is usually placed in the startup class of the project, because SpringBoot does not have an xml file to configure bean s. If there is an xml configuration file in the project, how can we register it in the container?, We can use @ ImportResource() to put the configuration file in the startup class, and then inject the startup class into the container through @ SpringBootApplication;
Let's briefly analyze its three notes:
@SpringBootConfiguration: used to inject beans into the container, because there is a @ Configuration under the annotation, and then @ Component is encapsulated under @ Configuration, which goes back to the feeling of Spring. The function of @ Component is to inject beans into the container, so @ SpringBootConfiguration is mainly used to register beans into the container; Note that the scanned components must be at the same level or lower than it, unless you add location attributes such as com. Com in @ SpringBootConfiguration XXX or something;
//@Source code of SpringBootConfiguration @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration @Indexed public @interface SpringBootConfiguration { @AliasFor( annotation = Configuration.class ) boolean proxyBeanMethods() default true; }
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Configuration { @AliasFor( annotation = Component.class ) String value() default ""; boolean proxyBeanMethods() default true; }
@Configuration: Here we can see that it has the function of an agent, proxyBeanMethods. When it is true, its role is: in the same configuration file, calling other @Bean tagged methods to get objects will be obtained directly from the IOC container, and will be obtained from the IOC container by proxy method, that is, singleton, while false, it does not go IOC container at all. There is no proxy. Every time @ bean is called, it is new;
MyConfig bean = run.getBean(MyConfig.class); User user = bean.user();//Get the bean instance user from the configuration class object System.out.println(user); System.out.println(user.getCat()==cat);
@EnableAutoConfiguration: automatic Configuration, the old rule, first look at its source code (a lot) and see if there is anything we can understand. First, you can see @ Import(EnableAutoConfigurationImportSelector.class). What is its function? Load the qualified @ Configuration configuration into the springboot to create it, and use the IOC container,
@ComponentScan: it has two functions: component scanning and automatic assembly;
@ConditionalOnMissingBean(name="xxx"): a conditional annotation that can be placed on both classes and methods. If the condition is not met, the components under In will not be registered;
@Import: ordinary classes can be introduced to help us define ordinary classes as beans, and @ import can be added to the startup class @ SpringBootApplication, @ Configuration class, @ Component component, and the class of @ import can be registered in the IOC container;
@SpringBootApplication @Import(Fairy.class) // Add Fairy to the IOC container through the @ Import annotation public class MyBatisApplication { public static void main(String[] args) { SpringApplication.run(MyBatisApplication.class, args); } }
After our bean is registered in the container, we can inject this bean into the Controller layer. When accessing the url, the corresponding json string appears;
@RestController public class HelloController { // Verify whether the import is successful under the controller @Autowired Fairy fairy; @RequestMapping("/fairy") public Fairy getFairy(){ return fairy;//Visual inspection here is successful } }
Then the corresponding entity class should also pay attention to + @ ConfigurationProperties(prefix="xxx"): it means in application Properties: prefix with xxx Configure the attribute name;
fairy.name=Fairy fairy.age=19 fairy.ability=big