In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to automatically assemble Sring Boot". In daily operation, I believe many people have doubts about how to assemble Sring Boot automatically. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to assemble Sring Boot automatically". Next, please follow the editor to study!
Annotation mode assembly
@ sevice @ Conrtroller @ Repository @ Component
These comments have already been found in the Srping source article, so I won't repeat them here.
Conditional (Condition) assembly
Condition annotation is used as a condition. If the condition is met, bean is injected into IOC. Otherwise, it is not injected. In fact, @ Conditional annotation is used to inherit the Condition interface, and the matches method is used to logically determine whether the condition is met.
1: create ConditionOnSysProperty comments
/ * @ Auther: lantao * @ Date: 2019-07-18 17:52 * @ Company: pay along the way * @ maill: lan_tao@suixingpay.com * @ Description: TODO * / @ Retention (RetentionPolicy.RUNTIME) @ Target ({ElementType.TYPE, ElementType.METHOD}) @ Documented@Conditional ({SysPropertyCondition.class}) public @ interface ConditionOnSysProperty {String value () default "lantao";}
2: create the conditional SysPropertyCondition required for @ Condtional
/ * @ Auther: lantao * @ Date: 2019-07-18 17:52 * @ Company: pay along the way * @ maill: lan_tao@suixingpay.com * @ Description: TODO * / public class SysPropertyCondition implements Condition {/ * matching method * * @ param context * @ param metadata * @ return * / @ Override public boolean matches (ConditionContext context AnnotatedTypeMetadata metadata) {/ / get the attribute of the ConditionOnSysProperty annotation Map attributes = metadata.getAnnotationAttributes (ConditionOnSysProperty.class.getName ()) String value = String.valueOf (attributes.get ("value")); / / get the local user.name value String propertieValue = System.getProperties (). Get ("user.name"). ToString (); / / compare return value.equals (propertieValue);}}
3: create a COnditionBootStrap test
/ * @ Auther: lantao * @ Date: 2019-07-18 17:44 * @ Company: accompanying payment Limited * @ maill: lan_tao@suixingpay.com * @ Description: TODO * / public class ConditionBootStrap {@ Bean / / because value is lantao and user.name happens to be lantao, the condition holds. Bean will be injected into ioc @ ConditionOnSysProperty (value = "lantao") private String helloWorld () {return "Hello World! Condition ";} public static void main (String [] args) {/ / this way can not use the SpringBootApplication annotation ConfigurableApplicationContext context = new SpringApplicationBuilder (ConditionBootStrap.class) .run (args); / / get the Bean named helloWorld to determine whether the ConditionOnSysProperty condition is valid String helloWorld = context.getBean (" helloWorld ", String.class); System.out.println (helloWorld) / / close context.close ();}} result: Hello World! Condition
@ Enable module assembly
Eanble annotations use @ Import to inject bean into ioc, and @ import annotations can be directly put into bean, or you can make more flexible configurations. Using bean, which inherits the ImportSeletor interface, you can make flexible dynamic judgments based on the attributes (attrbutes) of @ Enable annotations.
1: create EnableHelloWorld
/ * @ Auther: lantao * @ Date: 2019-07-18 18:03 * @ Company: accompanying payment Limited * @ maill: lan_tao@suixingpay.com * @ Description: TODO * / @ Target (ElementType.TYPE) @ Retention (RetentionPolicy.RUNTIME) @ Documented// No flexible judgment / / @ Import (TestConfiguration.class) / / more flexible judgment @ Import (HelloWorldImportSeletor.class) public can be obtained by using HelloWorldImportSeletor @ interface EnableHelloWorld {String name () default "lantao" }
2: create TestConfiguration and Test1Configuration
Public class TestConfiguration {@ Bean private String helloWorld () {return "helloWorld! Enable";}} public class Test1Configuration {@ Bean public String buzhidao () {return "don't know";}}
3: create HelloWorldImportSeletor
/ * @ Auther: lantao * @ Date: 2019-07-19 09:55 * @ Company: pay along the way * @ maill: lan_tao@suixingpay.com * @ Description: TODO * / public class HelloWorldImportSeletor implements ImportSelector {@ Override public String [] selectImports (AnnotationMetadata importingClassMetadata) {/ / get the attribute Map attributes = importingClassMetadata.getAnnotationAttributes (EnableHelloWorld.class.getName ()) of the EnableHelloWorld annotation / / flexibly judge which bean if ("lantao" .equals (attributes.get ("name")) is injected according to attributes {System.out.println (TestConfiguration.class.getName ()); return new String [] {TestConfiguration.class.getName ()};} System.out.println (TestConfiguration.class.getName ()); return new String [] {Test1Configuration.class.getName ()};}}
4: create a bootStrap test
/ * @ Auther: lantao * @ Date: 2019-07-18 18:04 * @ Company: pay along the way * @ maill: lan_tao@suixingpay.com * @ Description: TODO * / @ EnableHelloWorld//@EnableHelloWorld (name = "buzhidao") public class EnableBootStrap {public static void main (String [] args) {/ / this way can not use SpringBootApplication annotation ConfigurableApplicationContext context = new SpringApplicationBuilder (EnableBootStrap.class) .run (args) / / get the Bean named helloWorld to determine whether the ConditionOnSysProperty condition is valid String helloWorld = context.getBean ("helloWorld", String.class); / / you can get the bean name as' buzhidao', needs to be annotated @ EnableHelloWorld (name = "buzhidao"), / / because the name default value of @ EnableHelloWorld is lantao, and / / String helloWorld = context.getBean ("buzhidao", String.class) System.out.println (helloWorld); / / close context.close ();}} result: helloWorld! Enable
Factory mode assembly
Custom spring.factories, factory mode assembly can be customized starter.
1: create SpringFactoriesConfiguration
/ * @ Auther: lantao * @ Date: 2019-07-22 10:10 * @ Company: pay along with Limited * @ maill: lan_tao@suixingpay.com * @ Description: TODO * / @ EnableHelloWorld@ConditionOnSysProperty (value = "lantao") @ Configurationpublic class SpringFactoriesConfiguration {/ / there is no injection of bean written here, just refer to Condition and eanble module notes. / / import is used inside the eanble annotation to inject bean into ioc. @ import annotation can be directly put into bean, or you can make a more flexible configuration using bean that inherits the ImportSeletor interface. / / flexible dynamic judgment / / Condition annotation can be made according to the attribute (attrbutes) of enable annotation. If the condition is met, bean is injected into IOC, otherwise, it is not injected. In fact, @ Conditional annotation is used to achieve this. By inheriting the Condition interface, / / the matches method is used to logically determine whether the condition is met. }
2: create META-INF/spring.factories
# Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.lantao.springboot_leran.spring_factories_leran.config.SpringFactoriesConfiguration
3: create the SpringFactoriesBootStrap boot class
/ * @ Auther: lantao * @ Date: 2019-07-19 16:01 * @ Company: pay along the way * @ maill: lan_tao@suixingpay.com * @ Description: TODO * / @ EnableAutoConfigurationpublic class SpringFactoriesBootStrap {public static void main (String [] args) {ConfigurableApplicationContext context = new SpringApplicationBuilder (SpringFactoriesBootStrap.class) .run (args); String helloWorld = context.getBean ("helloWorld", String.class); System.out.println (helloWorld) Context.close ();}} result: hello World! Enable at this point, the study of "how to automatically assemble Sring Boot" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.