SpringBoot 创建非web工程
SpringBoot Java工程
创建一个SpringBoot项目,但不是web工程,哦~ 好像有点多余,,,好奇心驱使着写完了,没有web怎么调用接口的实现类呢,一起期待一波~
1. 第一种实现——在启动类中获取SpringBoot容器ConfigurableApplicationContext
1.1 引入依赖
pom.xml
<dependencies>
<!--SpringBoot框架非web应用 起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--SpringBoot框架测试起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<!--SpringBoot框架编译打包插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
1.2 创建接口
StudentService.java
package com.guo.springboot.service;
public interface StudentService {
String say();
}
1.3 实现接口
StudentServiceImpl.java
package com.guo.springboot.service.impl;
import com.guo.springboot.service.StudentService;
import org.springframework.stereotype.Service;
@Service //创建对象交给Spring容器管理
public class StudentServiceImpl implements StudentService {
@Override
public String say() {
return "HelloWord!";
}
}
1.4 核心启动类
Application.java
package com.guo.springboot;
import com.guo.springboot.service.StudentService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
/**
* SpringBoot程序启动后,返回值是ConfigurableApplicationContext,它也是一个Spring容器
* 它其实相当于原来Spring容器中启动容器ClasspathXmLApplicationContext
*/
//获取SpringBoot容器
ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
//从spring容器中获取指定bean对象
StudentService studentService = (StudentService) applicationContext.getBean("studentServiceImpl");
//调用业务方法
String say = studentService.say();
System.out.println(say);
}
}
1.5 运行结果
2. 第二种实现——在启动类实现CommandLineRunner接口,重写run()方法
2.1引入依赖
同第一种
2.2 创建接口
同第一种
2.3 实现接口
同第一种
2.4 核心启动类
核心启动类实现 CommandLineRunner接口 重写run()方法
package com.guo.springboot;
import com.guo.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private StudentService studentService;
public static void main(String[] args) {
//SpringBoot启动程序,会初始化Spring容器
SpringApplication.run(Application.class, args);
}
//重写CommandLineRunner类中的run方法
@Override
public void run(String... args) throws Exception {
//调用业务方法
String say = studentService.say();
System.out.println(say);
}
}