In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces what is the role of Configuration annotations in Spring5.0, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Spring5.0: the use of @ Configuration
@ Configuration is used to define the configuration class and can replace the XML configuration file. The annotated class contains one or more methods annotated by @ Bean. These methods will be scanned by the AnnotationConfigApplicationContext or AnnotationConfigWebApplicationContext class and used to build the Bean definition and initialize the Spring container.
@ Configurationpublic class MySpringConfig {@ Beanpublic UserEntity userEntity () {return new UserEntity ("xuyu", 21);}} AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext (MySpringConfig.class); first, load spring with @ Configuration
@ Configuration configure spring and start the spring container
1.2, @ Configuration launch Container + @ Bean Register Bean
1.3, @ Configuration launch Container + @ Component Register Bean
Registering the AppContext class using AnnotationConfigApplicationContext
Second, combine multiple configuration classes
2.1.Introducing other annotation configurations in @ configuration
2.2, @ configuration nesting
3. @ EnableXXX comment
4. @ Profile logical group configuration
Use external variables 1, @ Configuation to load Spring methods 1.1, @ Configuration to configure spring and start the spring container
@ Configuration is marked on the class, which is equivalent to using the class as the xml configuration file of spring. Its function is to configure the spring container (application context).
@ Configurationpublic class TestConfiguration {public TestConfiguration () {System.out.println ("TestConfiguration container initialization.") ;}}
Equivalent to:
The main method is tested:
Public class TestMain {public static void main (String [] args) {/ / @ Configuration annotated spring container loading mode, replace ClassPathXmlApplicationContext ApplicationContext context = new AnnotationConfigApplicationContext (TestConfiguration.class) with AnnotationConfigApplicationContext; / / if the spring-context.xml file is loaded: / / ApplicationContext context = new ClassPathXmlApplicationContext ("spring-context.xml");}}
As you can see from the result of running the main method, the spring container has been started:
1.2. @ Configuration launch Container + @ Bean Registration Bean,@Bean to manage the life cycle of bean
@ Bean is annotated on the method (the method that returns an instance), which is equivalent to that in the xml configuration file of spring. The function is to register the bean object.
Bean class:
Public class TestBean {private String username; private String url; private String password; public void sayHello () {System.out.println ("TestBean sayHello...");} public String toString () {return "username:" + this.username + ", url:" + this.url + ", password:" + this.password;} public void start () {System.out.println ("TestBean initialization.") ;} public void cleanUp () {System.out.println ("TestBean destruction.") ;}}
Configuration class:
Import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Scope;@Configurationpublic class TestConfiguration {public TestConfiguration () {System.out.println ("TestConfiguration container initialization.") ;} / / @ Bean annotations register bean, and you can specify initialization and destruction methods / / @ Bean (name= "testBean", initMethod= "start", destroyMethod= "cleanUp") @ Bean @ Scope ("prototype") public TestBean testBean () {return new TestBean ();}}
The main method test class:
Import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestMain {public static void main (String [] args) {/ / @ Configuration annotated spring container loading mode, replace ClassPathXmlApplicationContext ApplicationContext context = new AnnotationConfigApplicationContext (TestConfiguration.class) with AnnotationConfigApplicationContext; / / if the spring-context.xml file is loaded: / / ApplicationContext context = new / / ClassPathXmlApplicationContext ("spring-context.xml") / / get bean TestBean tb = (TestBean) context.getBean ("testBean"); tb.sayHello ();}}
Results:
Note:
(1) and @ Bean annotations on the method that returns the instance. If the name of the bean is not specified through @ Bean, the default is the same as the method name of the annotation.
(2) the default scope of @ Bean annotation is singleton singleton scope, which can be set to prototype scope by @ Scope ("prototype").
(3) since @ Bean is used to register bean objects, you can use @ Component, @ Controller, @ Service, @ Ripository and other annotations to register bean. Of course, you need to configure the @ ComponentScan annotation for automatic scanning.
Manage the life cycle of bean under @ Bean
You can use Java-based configurations to manage the lifecycle of bean. @ Bean supports two properties, initMethod and destroyMethod, which can be used to define lifecycle methods. The container can call the lifecycle method when the bean is instantiated or is about to be destroyed.
The lifecycle method is also called a callback method because it will be called by the container. Bean registered with the @ Bean annotation also supports the standard @ PostConstruct and @ PreDestroy annotations specified by JSR-250. If you are using the XML method to define the bean, you should use the bean element to define the lifecycle callback method.
The following code shows a method that typically uses the bean element to define callbacks in a XML configuration.
Package com.mayikt.v1.entity;public class TestBean {public void sayHello () {System.out.println ("TestBean sayHello...");} public void start () {System.out.println ("TestBean initialization.") ;} public void cleanUp () {System.out.println ("TestBean destruction.") ;} @ Configuration@ComponentScan (basePackages = "com.mayikt.v1.entity") public class TestConfiguration {public TestConfiguration () {System.out.println ("TestConfiguration container initialization.") ;} / @ Bean annotations register bean, and you can specify the initialization and destruction method @ Bean (name= "testBean", initMethod= "start", destroyMethod= "cleanUp") @ Scope ("prototype") public TestBean testBean () {return new TestBean ();}} public class TestMain {public static void main (String [] args) {ApplicationContext context = new AnnotationConfigApplicationContext (TestConfiguration.class) TestBean tb = (TestBean) context.getBean ("testBean"); tb.sayHello (); System.out.println (tb); TestBean tb2 = (TestBean) context.getBean ("testBean"); tb2.sayHello (); System.out.println (tb2);}}
Analysis:
1 in the result: indicates that initMethod is effective
2 in the result: indicates that @ Scope ("prototype") is valid
@ Configuration launch Container + @ Component Registration Bean// add Notes for Registration bean @ Componentpublic class TestBean {private String username; private String url; private String password; public void sayHello () {System.out.println ("TestBean sayHello...");} public String toString () {return "username:" + this.username + ", url:" + this.url + ", password:" + this.password " } public void start () {System.out.println ("TestBean initialization.") ;} public void cleanUp () {System.out.println ("TestBean destruction.") ;}} @ Configuration// add automatic scan annotation. BasePackages is the TestBean package path @ ComponentScan (basePackages = "com.mayikt.v1.entity") public class TestConfiguration {public TestConfiguration () {System.out.println ("TestConfiguration container initialization.") ;}} public class TestMain {public static void main (String [] args) {/ / @ Configuration annotated spring container loading mode, replace ClassPathXmlApplicationContext ApplicationContext context = new AnnotationConfigApplicationContext (TestConfiguration.class) with AnnotationConfigApplicationContext; / / if the spring-context.xml file is loaded: / / ApplicationContext context = new / / ClassPathXmlApplicationContext ("spring-context.xml") / / get bean TestBean tb = (TestBean) context.getBean ("testBean"); tb.sayHello ();}}
Registering AppContext classes using AnnotationConfigApplicationContext
The register method of AnnotationConfigApplicationContext passes in the configuration class to register the configuration class
Public class TestMain {public static void main (String [] args) {/ / @ Configuration annotated spring container loading mode, replace ClassPathXmlApplicationContext ApplicationContext context = new AnnotationConfigApplicationContext () with AnnotationConfigApplicationContext; ((AnnotationConfigApplicationContext) context) .register (TestConfiguration.class); String [] beanDefinitionNames = context.getBeanDefinitionNames (); for (int I = 0; I < beanDefinitionNames.length; iContainer +) {System.out.println (Bean DefinitionNams [I]) @ Configuation Summary
@ Configuation is equivalent to
@ Bean is equivalent to
@ ComponentScan is equivalent to
Combine multiple configuration classes 2.1.Introducing other annotated configurations @ Configuration@Import (TestConfiguration.class) public class WebConfig {} @ Configuration@ComponentScan (basePackages = "com.mayikt.v1.entity") public class TestConfiguration {public TestConfiguration () {System.out.println ("TestConfiguration container initialization.") ;} / / @ Bean annotation registers bean, and you can specify the initialization and destruction method @ Bean (name= "testBean2", initMethod= "start", destroyMethod= "cleanUp") @ Scope ("prototype") public TestBean2 testBean2 () {return new TestBean2 () }} public class TestMain2 {public static void main (String [] args) {/ / @ Configuration annotated spring container loading mode, replace ClassPathXmlApplicationContext ApplicationContext context = new AnnotationConfigApplicationContext (WebConfig.class) with AnnotationConfigApplicationContext; / / get bean TestBean2 tb2 = (TestBean2) context.getBean ("testBean2"); tb2.sayHello (); TestBean tb = (TestBean) context.getBean ("testBean"); tb.sayHello ();}}
Results:
2.2, @ configuration nesting @ Configurationpublic class MySpringConfig {@ Configuration static class entity {@ Bean UserEntity userEntity () {return new UserEntity ("mayikt", 21);}} / * @ Bean public UserEntity userEntity () {return new UserEntity ("xuyu", 21);} * /} public class V2TestSpring {private static AnnotationConfigApplicationContext annotationConfigApplicationContext Public static void main (String [] args) {annotationConfigApplicationContext = new AnnotationConfigApplicationContext (MySpringConfig.class); System.out.println ("Startup configuration loaded..."); UserEntity userEntity = annotationConfigApplicationContext.getBean ("userEntity", UserEntity.class); String [] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames (); for (int I = 0; I < beanDefinitionNames.length; iTunes +) {System.out.println (Bean DefinitionNams [I]) }}}
Result
3. @ EnableXXX comment
Used with @ Configuration, including @ EnableAsync, @ EnableScheduling, @ EnableTransactionManagement, @ EnableAspectJAutoProxy, @ EnableWebMvc.
@ EnableAspectJAutoProxy--- "spring AOP: @ Aspect Notes"
@ EnableScheduling-- "second of the new features of Spring 3.1: @ Enable* annotations source code, spring source code analysis timing task Scheduled annotations"
4. @ Profile logical group configuration
See @ PropertySource + Environment,@PropertySource (PropertySourcesPlaceholderConfigurer) + @ Value of Spring
5. Use external variables
1. @ PropertySource + Environment, the values in the properties configuration file are stored in the Environment of Spring through the @ PropertySource annotation. The Environment API provides a method to read the values in the configuration file, and the parameters are the key values defined in the properties file.
2. @ PropertySource (PropertySourcesPlaceholderConfigurer) + @ Value
See @ PropertySource + Environment,@PropertySource (PropertySourcesPlaceholderConfigurer) + @ Value of Spring
Question to consider: when was the springbean object created?
Answer: non-lazy loading by default.
@ Lazy
Lazy is represented as lazy loading, which will be loaded only when reference acquisition is really needed.
True is represented as non-lazy loading false is represented as created when the IOC container is loaded
@ Service@Lazy (true) public class UserService {public UserService () {System.out.println ("UserService no-parameter construction is loaded.")}} about what the role of Configuration annotations in Spring5.0 is shared here, I hope the above content can be helpful to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.