springboot(1) spring项目的搭建

说明:

  1. 该文章是基于springboot在maven中的搭建。
  2. 该文章用的springboot版本为2.1.3.RELEASE。
  3. 测试使用的jdk版本为jdk1.8.0_201。
  4. 文章使用的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方法是可选的。

项目源码下载地址:

springboot.rar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值