Spring Boot创建非Web项目开发

添加依赖

<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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值