添加依赖
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.0.3.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.0.3.RELEASE</version>
<scope>compile</scope>
</dependency>
...
</dependencies>
编写服务接口及服务类
// HelloService.class
public interface HelloService {
String sayHello(String name);
}
// HelloServiceImp.class
@Service("helloService")
public class HelloServiceImp implements HelloService {
public String sayHello(String name) {
return "Hello," + name;
}
}
实现CommandLineRunner接口
// SpringBootTestApplication.class 程序入口
@SpringBootApplication
public class SpringBootTestApplication implements CommandLineRunner {
@Autowired
HelloService helloService;
@Override
public void run(String... args) throws Exception {
String s = this.helloService.sayHello("jack");
System.out.println(s);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootTestApplication.class, args);
}
}
调试,查看日志输出:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.3.RELEASE)
Hello,jack
Process finished with exit code 0