我们可以在 https://start.spring.io/ 创建项目:
# 示例 ## 项目结构 本文的项目是用它生成的,最终项目结构:.
├── build.gradle
├── settings.gradle
└── src
└── main
├── java
│ └── demo
│ ├── BusinessService.java
│ ├── DemoApplication.java
│ ├── SayHello.java
│ └── SayHelloIml.java
└── resources
└── application.properties
配置文件applacation.properties
内容为空,这里可以先不关心。
build.gradle
build.gradle
内容:
buildscript {
ext {
springBootVersion = '2.1.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
maven { url 'http://mirrors.cloud.tencent.com/nexus/repository/maven-public/' }
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
SayHello.java
接口 SayHello 代码:
package demo;
public interface SayHello {
String sayHello(String name);
}
SayHelloIml.java
接口实现类 SayHelloIml 代码:
package demo;
import org.springframework.stereotype.Component;
@Component
public class SayHelloIml implements SayHello {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
BusinessService.java
BusinessService 类:
package demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@Service
public class BusinessService {
@Autowired
private SayHello sayHello;
public BusinessService(){
System.out.println("construct");
}
@PostConstruct
public void postConstruct(){
System.out.println("postConstruct");
System.out.println(sayHello.sayHello("world"));
}
}
被@PostConstruct
注解修饰的方法,会在执行构造函数后被执行。
DemoApplication.java
主程序入口 DemoApplication :
package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
执行结果
在 IDE 中运行 DemoApplication ,输出:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.2.RELEASE)
2018-07-17 09:19:13.329 INFO 68004 --- [ main] demo.DemoApplication : Starting DemoApplication on Myhost with PID 68004
2018-07-17 09:19:13.334 INFO 68004 --- [ main] demo.DemoApplication : No active profile set, falling back to default profiles: default
2018-07-17 09:19:13.409 INFO 68004 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3f200884: startup date [Tue Jul 17 09:19:13 CST 2018]; root of context hierarchy
construct
postConstruct
Hello, world
2018-07-17 09:19:14.045 INFO 68004 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-07-17 09:19:14.059 INFO 68004 --- [ main] demo.DemoApplication : Started DemoApplication in 1.172 seconds (JVM running for 1.718)
2018-07-17 09:19:14.062 INFO 68004 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3f200884: startup date [Tue Jul 17 09:19:13 CST 2018]; root of context hierarchy
2018-07-17 09:19:14.063 INFO 68004 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
注意中间的:
construct
postConstruct
Hello, world
也可以通过
gradle bootRun
运行程序。