Spring boot is able to automatically configure beans through the ability to configure beans based on conditions.
The commonly used conditions are as follows
@ConditionalOnBean: When the specified bean is present in the container
@ConditionalOnClass: Under the condition that the specified class exists under the current class path
@ConditionalOnExpression: Based on spel expression as the judging condition
@ConditionalOnJava: Based on the JVM version as the judging condition
@ConditionalOnJndi: Finds the specified location under the condition of Jndi presence
@ConditionalOnMissingBean: When the container does not have the specified bean condition
@ConditionalOnMissingClass: Under the condition of the current class path without the specified class
@ConditionalOnNotWebApplication: The current project is not under the condition of a Web project
@ConditionalOnProperty: Specifies whether the property has the specified value under conditions
@ConditionalOnResource: Whether there is a specified value under the Classpath
@ConditionalOnSingleCandidate: Specifies that the bean has only one in the container, or although there are multiple but specified preferred beans
@ConditionalOnWebApplication: The current project is under the condition of a Web project
These annotations all combine @conditional annotations, but use different conditions.
Next we write an automatic configuration and encapsulate it into starter pom.
First create a MAVEN project and import the following dependencies:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-autoconfigure</artifactid> <version>1.3.8.RELEASE</version> </dependency>
The engineering structure is as follows
650) this.width=650; "src=" Http://www.weare.net.cn/EasyWeb/upload/image/20161220/1482199427021008754.png "title=" 1482199427021008754.png "alt=" Jiegou.png "style=" Border:0px;vertical-align:middle;height:auto; "/>
Create Authorproperties class as a carrier of configuration information
package com.springboot.springboot_starter_hello;import Org.springframework.boot.context.properties.ConfigurationProperties, @ConfigurationProperties (prefix= "author")/ /Specifies the prefix of the configuration content Public class authorproperties {private static final string name = "Weibo blog";p rivate static final int age = 18;private string name = name;//default to micro-blog private int age = age;//default to 18public string GetName () {return name;} Public void setname (String name) {this.name = name;} Public int getage () {return age;} Public void setage (Int age) {this.age = age;}}
package com.springboot.springboot_starter_hello;public class authorservice {private String name;private int age;public string who () {return "Name:" +name+ ", Age:" +age;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} Public int getage () {return age;} Public void setage (Int age) {this.age = age;}}
Create configuration class Authorserviceautoconfiguration, responsible for configuring bean
package com.springboot.springboot_starter_hello;import org.springframework.beans.factory.annotation.autowired;import org.springframework.boot.autoconfigure.condition.conditionalonclass;import org.springframework.boot.autoconfigure.condition.conditionalonmissingbean;import org.springframework.boot.autoconfigure.condition.conditionalonproperty;import org.springframework.boot.context.properties.enableconfigurationproperties;import org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;@ configuration//declares the configuration class @enableconfigurationproperties (Authorproperties.class) @ConditionalOnClass ( Authorservice.class)//class path exists under the condition of AuthorService class @conditionalonproperty (prefix= "Author", value= "enabled", Matchifmissing=true)//In the case where the prefix author is configured to Enabled, that is, author=enabled, no configuration defaults to enabledpublic class authorserviceautoconfiguration {@Autowiredprivate authorproperties authorproperties;@ Bean@conditionalonmissingbeAn (Authorservice.class)//container with no AuthorService beans configured under the condition of the Beanpublic authorservice authorservice () { Authorservice authorservice = new authorservice (); Authorservice.setname ( Authorproperties.getname ()); Authorservice.setage (Authorproperties.getage ()); return authorservice;}}
If you want automatic configuration to take effect, you need to register the automatic configuration class. Create the Meta-inf/spring.factories file under Src/main/resources and write the following:
Org.springframework.boot.autoconfigure.enableautoconfiguration=com.springboot.springboot_starter_hello. Authorserviceautoconfiguration
If there are multiple auto-configuration classes, the "," is separated, where "\" is to be able to read the property after a newline.
Perform maven install after the above operation is complete.
The dependency is then introduced in the Spring Boot project:
<dependency><groupid>com.springboot</groupid><artifactid>springboot-starter-hello</ Artifactid><version>0.0.1-snapshot</version></dependency>
New AutoConfig class:
package com.springboot.autoconfig;import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.springapplication;import org.springframework.boot.autoconfigure.springbootapplication;import org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.restcontroller;import com.springboot.springboot_starter_hello. AuthorService, @SpringBootApplication @restcontrollerpublic class autoconfig {@Autowiredprivate AuthorService authorService; @RequestMapping ("/") public string who () {return Authorservice.who ();} Public static void main (String[] args) {springapplication.run (AutoConfig.class, args);}}
Run the class, Access localhost:8080, and the following results appear: Name: Weibo blog, age:18
650) this.width=650; "src=" Http://www.weare.net.cn/EasyWeb/upload/image/20161220/1482200276538026580.png "title=" 1482200276538026580.png "alt=" 123.png "style=" Border:0px;vertical-align:middle;height:auto; "/>
Next, create a new application.properties file under Src/main/resources and write the following:
Author.name=weareauthor.age=19
Rerun AutoConfig class: Name:weare,age:19
650) this.width=650; "src=" Http://www.weare.net.cn/EasyWeb/upload/image/20161220/1482200449810029960.png "title=" 1482200449810029960.png "alt=" 123.png "style=" Border:0px;vertical-align:middle;height:auto; "/>
Original link: Click to visit
More articles please visit Weibo blog
Spring Boot Auto Configuration instance