【发布时间】:2015-03-27 19:26:43
【问题描述】:
如果我正在开发一个相当简单的基于 Spring Boot 控制台的应用程序,我不确定主要执行代码的位置。我应该把它放在 public static void main(String[] args) 方法中,还是让主应用程序类实现 CommandLineRunner 接口并将代码放在 run(String... args) 方法?
我将使用一个示例作为上下文。假设我有以下 [基本] 应用程序(编码为接口,Spring 风格):
Application.java
public class Application {
@Autowired
private GreeterService greeterService;
public static void main(String[] args) {
// ******
// *** Where do I place the following line of code
// *** in a Spring Boot version of this application?
// ******
System.out.println(greeterService.greet(args));
}
}
GreeterService.java(接口)
public interface GreeterService {
String greet(String[] tokens);
}
GreeterServiceImpl.java(实现类)
@Service
public class GreeterServiceImpl implements GreeterService {
public String greet(String[] tokens) {
String defaultMessage = "hello world";
if (args == null || args.length == 0) {
return defaultMessage;
}
StringBuilder message = new StringBuilder();
for (String token : tokens) {
if (token == null) continue;
message.append(token).append('-');
}
return message.length() > 0 ? message.toString() : defaultMessage;
}
}
Application.java 的等效 Spring Boot 版本大致如下:
GreeterServiceImpl.java(实现类)
@EnableAutoConfiguration
public class Application
// *** Should I bother to implement this interface for this simple app?
implements CommandLineRunner {
@Autowired
private GreeterService greeterService;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
System.out.println(greeterService.greet(args)); // here?
}
// Only if I implement the CommandLineRunner interface...
public void run(String... args) throws Exception {
System.out.println(greeterService.greet(args)); // or here?
}
}
【问题讨论】:
-
使用
CommandLineRunner,这就是它的设计目的,您还可以创建它的一个实例并将依赖项注入其中。这在您的第一个场景中会失败,因为您不会注入依赖项或无法从静态方法访问它。 -
@M.Deinum 所以这样的基于控制台的应用程序必须实现 CommandLineRunner 接口作为一般经验法则?这为我提供了足够的清晰度,因为即使是文档也没有关注这一点(可能是因为它对大多数人来说是隐含的!)。
-
那么你最好创建一个实现这个接口的bean(而不是你的
Application类)。它与关注点分离(启动应用程序并执行逻辑)有关。 -
你指的是
GreeterService[implementation] bean吗?因为我的Application班级只使用这个服务。所以此时已经存在关注点分离,不是吗? -
不,我不是指那个。对于一些阅读,您一直在提到您希望您的
Application类实现CommandLineRunner接口。您可能不希望这样做,而是在单独的课程中这样做。
标签: java console-application spring-boot