SpringBoot启动器简单案例
1.创建一个maven项目
2.项目结构及代码
HelloAutoConfiguration
package test.com;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 声明为配置类
*/
@Configuration
public class HelloAutoConfiguration {
@Bean
@ConditionalOnMissingBean //容器中缺少该bean时进行创建
public HelloService helloService(){
return new HelloService();
}
}
HelloService
package test.com;
public class HelloService {
public String sayHello(String name){
System.out.println("hello"+name);
return name;
}
}
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=test.com.HelloAutoConfiguration
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot<