Spring Boot创建非Web项目开发

添加依赖


       ...
        
            org.springframework.boot
            spring-boot
            2.0.3.RELEASE
            compile
        
        
            org.springframework.boot
            spring-boot-autoconfigure
            2.0.3.RELEASE
            compile
        
    ...
    

编写服务接口及服务类

// 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

 

你可能感兴趣的:(spring,boot)