Spring-boot实例学习之Simple Command line application
来源:互联网 发布:杭州皓辰网络法人 编辑:程序博客网 时间:2024/06/16 08:03
前言
本文将分析spring-boot在git上的一个sample code,一个简单的命令行应用(Simple command line application)的实现。这个例子通过启动main方法,来加载相关配置,并打印一段简单的文字。
实现CommandLineRunner接口并输出数据
当SpringApplication启动后,我们希望执行某些代码,这是可以通过实现ApplicationRunner或CommandLineRunner接口来实现我们的需求。两个接口的工作方式相同,并且都提供了一个run方法,该方法将在SpringApplication.run(…)完成之后被调用。
为了实现上述功能,我们需要先加添加依赖。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
当启动main方法,SpringApplication.run(…)运行后,我们会看到helloworld输出。
@SpringBootApplicationpublic class SampleSimpleApplication implements CommandLineRunner { @Override public void run(String... args) { System.out.println("hello world"); ); } public static void main(String[] args) throws Exception { SpringApplication.run(SampleSimpleApplication.class, args); }}
定义CommandLineRunner调用顺序
当有多个CommandLineRunner的实现时,可以通过实现 Ordered接口来定义调用顺序。代码如下。
import org.springframework.boot.CommandLineRunner;import org.springframework.core.Ordered;import org.springframework.stereotype.Component;@Componentpublic class HelloCommandLine implements CommandLineRunner,Ordered{ @Override public void run(String... args) throws Exception { System.out.println(this.getClass().getName()); } @Override public int getOrder() { return 11; }}
定义HelloWorldService服务
接着定义HelloWorldService服务,该服务的getHelloMessage方法返回一段文本。这个类中@Value注解,该注解会从属性文件中获取数据作为成员的默认值。@Value(“${name:world}”),从classpath下的application.properties文件中寻找key为name的行,并将其值注入name,如果找不到则用world作为默认值。
@Componentpublic class HelloWorldService { @Value("${name:world}") private String name; public String getHelloMessage() { return "Hello world" + this.name; }}
application.properties文件
name=Philsample.name=Andy
接着在启动程序中调用,会看到获取的输出。
@SpringBootApplicationpublic class SampleSimpleApplication implements CommandLineRunner,Ordered { @Autowired HelloWorldService helloWorldService; @Override public void run(String... args) { System.out.println(helloWorldService.getHelloMessage()); } @Override public int getOrder() { return 5; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleSimpleApplication.class, args); }}
属性配置对象SampleConfigurationProperties
SampleConfigurationProperties的作用是,
这里用到了@ConfigurationProperties注解,该注解将properties或者yml配置直接转成对象。该例中的属性配置文件仍为上文的application.properties。详情参考Externalized Configuration
。
@Component@ConfigurationProperties(prefix = "sample")public class SampleConfigurationProperties { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}
在启动方法中注入该对象并打印,可以看到输出的配置信息。
@SpringBootApplicationpublic class SampleSimpleApplication implements CommandLineRunner{ @Autowired HelloWorldService helloWorldService; @Autowired SampleConfigurationProperties properties; @Override public void run(String... args) { System.out.println(helloWorldService.getHelloMessage()); System.out.println(properties.getName()); } public static void main(String[] args) throws Exception { SpringApplication.run(SampleSimpleApplication.class, args); }}
学习这个Sample遇到的问题
1.产生警告
WARNING : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
因为application.Java 文件不能直接放在main/java文件夹下,必须要建一个包把类放进去,否则会产生该警告。
2.系统启动后看不到输出
开始引入了两个starter pom,一个是web的starter,一个是普通的starter,最后将web的starter以来去掉后程序可以产生输出了。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
3.Could not autowire.No beans of HelloWorldService type found.
开始启动类和注入的服务分别在如下的两个包中。
- sample.SampleSimpleApplication
- service.HelloWorldService
SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描! “Application类”是指SpringBoot项目入口类。如果Application类所在的包为:com.boot.app,则只会扫描com.boot.app包及其所有子包,如果service或dao所在包不在com.boot.app及其子包下,则不会被扫描! 即, 把Application类放到dao、service所在包的上级,com.boot.Application 知道这一点非常关键,不知道spring文档里有没有给出说明,如果不知道还真是无从解决。
参考:
1.simple command line sample,
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-simple
2.spring-boot reference guide,
http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/#boot-features-command-line-runner
- Spring-boot实例学习之Simple Command line application
- Spring-boot实例学习之 Command line application单元测试
- Spring boot学习笔记 002——Spring Boot的自动配置、Command-line Runner
- Spring-boot实例学习之 自定义starter
- 《spring-boot学习》-13-spring boot application properties配置详解
- Simple Spring Boot Demo
- CXF之spring配置实例(Simple Frontend)
- Spring Boot Security Application
- Spring Boot application.propertis
- Spring Boot Application 运行
- spring boot 学习之controller
- Spring Boot 学习之旅
- spring-boot 学习之路
- Caffe interface 之 Command Line
- spring boot 学习笔记(004)application.properties配置
- spring boot 学习笔记(009)application.yml
- Spring-Boot初学之配置文件application.properties(web性能)
- Spring-Boot初学之配置文件application.properties(thymeleaf模板)
- javascript怎么实现类似如C++函数中使用静态数据
- LINQ系列:Linq to Object分区操作符
- 开源跨平台多媒体开发库SDL初学编程练习--偷UP主的
- 暑假第一周 C . CodeForces 660A Co-prime Array
- 树状数组(基本操作)
- Spring-boot实例学习之Simple Command line application
- qDebug日志写入本地文件
- 结构和共用体
- Kinect ROS Gmapping 2D地图
- 正向代理、反向代理总结
- 采购单价PB00价税分离实际操作之一
- Android的动画移动后点击事件失效问题
- java语言基础(80)——递归获取指定目录下所有的文件
- javascript原型和继承