说明:
- 该文章是基于springboot在maven中的搭建。
- 该文章用的springboot版本为2.1.3.RELEASE。
- 测试使用的jdk版本为jdk1.8.0_201。
- 文章使用的eclipse版本为:2018-12 (4.10.0)
springboot作为spring的简化版,可以以最少的配置、最高的效率新建一个springboot项目。从而节省框架搭建的时间和提高开发效率。springboot项目的搭建只需要简单的几个步骤:
1. 引用springboot父项目。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
2. 添加springboot项目依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
3. 添加项目启动类。
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
4. 运行main方法。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)
2019-08-27 15:54:33.486 INFO 3648 --- [ main] com.test.App : Starting App on DEVPNLN15YF127 with PID 3648 (E:\Users\c_yinjicheng-002\bqpt_boot\springboot\target\classes started by c_yinjicheng-002 in E:\Users\c_yinjicheng-002\bqpt_boot\springboot)
2019-08-27 15:54:33.502 INFO 3648 --- [ main] com.test.App : No active profile set, falling back to default profiles: default
2019-08-27 15:54:34.143 INFO 3648 --- [ main] com.test.App : Started App in 1.063 seconds (JVM running for 1.5)
一个简单的springboot项目就搭建完成了。
springboot的基本知识:
1. 默认扫描的包路径设置:默认以项目启动类为根路径,如上述例子,那么默认扫描路径即为:com.test。如果需要指定路径,则需要使用org.springframework.context.annotation.ComponentScan注解指定扫描的路径。
2. 测试支持。
springboot中如果要添加测试支持,需要添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
测试类中需要添加注解:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
分别指定测试运行类和springboot程序的启动类。
我们可以新建测试Service,具体代码清单如下:
package com.test.service.interfaces;
public interface TestService {
public String test();
}
package com.test.service.impl;
import org.springframework.stereotype.Service;
import com.test.service.interfaces.TestService;
@Service
public class TestServiceImpl implements TestService {
@Override
public String test() {
return "test";
}
}
package com.test.service;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.test.App;
import com.test.service.interfaces.TestService;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class TestServiceTest {
@Autowired
private TestService testService;
@Test
public void test() {
LogFactory.getLog(getClass()).info(testService.test());
}
}
运行测试类结果如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)
2019-08-27 16:19:43.966 INFO 3192 --- [ main] com.test.service.TestServiceTest : Starting TestServiceTest on DEVPNLN15YF127 with PID 3192 (started by c_yinjicheng-002 in E:\Users\c_yinjicheng-002\bqpt_boot\springboot)
2019-08-27 16:19:43.966 INFO 3192 --- [ main] com.test.service.TestServiceTest : No active profile set, falling back to default profiles: default
2019-08-27 16:19:44.669 INFO 3192 --- [ main] com.test.service.TestServiceTest : Started TestServiceTest in 1.047 seconds (JVM running for 1.988)
2019-08-27 16:19:44.856 INFO 3192 --- [ main] com.test.service.TestServiceTest : test
3. 例子中App.class的main方法是可选的。
项目源码下载地址: