Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use @ Configuration annotations

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

The purpose of this article is to share with you how to use @ Configuration annotations. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

@ Configuration configure spring and start the spring container

Startup class

@ SpringBootApplicationpublic class SpirngdemoApplication {public static void main (String [] args) {/ / SpringApplication.run (SpirngdemoApplication.class, args); ApplicationContext context = new AnnotationConfigApplicationContext (TestConfiguration.class); System.out.println (".Service starts successfully.");}}

Configuration class

@ Configurationpublic class TestConfiguration {public TestConfiguration () {System.out.println ("TestConfiguration container initialization...");}}

Output result:

The TestConfiguration container initializes. Service starts successfully.

From the order of the output results, we can see that Spring initializes the container first during startup.

2. @ Configuration launch container + @ Bean register Bean,@Bean to manage the life cycle of bean

Startup class

@ SpringBootApplicationpublic class SpirngdemoApplication {public static void main (String [] args) {/ / SpringApplication.run (SpirngdemoApplication.class, args); ApplicationContext context = new AnnotationConfigApplicationContext (TestConfiguration.class); System.out.println (".service starts successfully."); TestBean tb = (TestBean) context.getBean ("testBean") Tb.sayHello ();}}

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

@ Configurationpublic class TestConfiguration {public TestConfiguration () {System.out.println ("TestConfiguration Container initialization...");} @ Bean (name= "testBean", initMethod= "start", destroyMethod= "cleanUp") @ Scope (value = "prototype") public TestBean testBean () {return new TestBean ();} by the way @ scope Note

Scope, also known as scope, in the Spring IoC container refers to the visible scope of the request for Bean objects that it creates relative to other Bean objects. There are several scopes in the Spring IoC container: basic scope (singleton, prototype), Web scope (reqeust, session, globalsession), and custom scope.

Singleton: singleton mode, Bean defined with singleton will have only one instance in the entire Spring IoC container

Prototype: prototype pattern, each time a Bean defined by prototype is obtained through the container's getBean method, a new Bean instance is generated.

Request: for each HTTP request, the Bean defined using request will generate a new instance, that is, a different Bean instance will be generated for each HTTP request. This scope is valid only if Spring is used in Web applications

Session: for each HTTP Session, a Bean defined with session generates a new instance. Again, this scope is valid only if Spring is used in Web applications.

Globalsession: for each global HTTP Session, a new instance of the Bean defined with session will be generated. Typically, it works only when using portlet context. Again, this scope is valid only if Spring is used in Web applications.

Output result

Service container initialization. Service starts successfully.TestBean initialization. TestBean sayHello...

@ Bean annotation registers bean, and you can specify initialization and destruction methods. You can see that @ Bean can also manage the life cycle of bean.

3. @ Configuration launch Container + @ Component Register Bean

Startup is the same as above

Configuration class:

@ Configuration@ComponentScan (basePackages = "com.wenxuan.springdemo.bean") public class TestConfiguration {public TestConfiguration () {System.out.println ("TestConfiguration container initialization...");}}

Bean class:

@ 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.") ;}}

The @ Component annotation is added to the bean class, and the @ Bean manual declaration is no longer added to the configuration class, but the @ ComponentScan annotation is added, indicating the configured package, which also achieves the purpose of initializing bean. Output result:

Service container initializes .service starts successfully.TestBean sayHello... The above is how to use @ Configuration annotations. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report