SpringBoot2.X开发规范解读

SpringBoot2.X开发规范解读
简介:讲解SpringBoot目录文件结构和官方推荐的目录规范、静态资源访问

1.目录讲解
src/main/java:存放代码
src/main/resources
static: 存放静态文件,比如 css、js、image, (访问方式 http://localhost:8080/js/main.js)
templates:存放静态页面jsp,html,tpl
config:存放配置文件,application.properties

2.同个文件的加载顺序,静态资源文件 Spring Boot 默认会挨个从
META/resources >
resources >
static >
public >
里面找是否存在相应的资源,如果有则直接返回,不在默认加载的目录,则找不到
默认配置(application.properties)
spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
需要加载其他配置文件,修改spring.resources.static-locations

3.SpringBoot2.x启动类位置常见形式,三种形式
3.1当启动类和controller在同一类中时,在该类上添加注解@Controller即可;
3.2当启动类和controller分开时,启动类要放在根目录下,启动类上只需要注解@SpringBootApplication;
3.3当启动类和controller分开时,如果启动类在非根目录下,需要在启动类中增加注解@ComponentScan,并配置需要扫描的包名,如(basePackages = )
@ComponentScan(basePackages ={“net.xdclass.controller”,“net.xdclass.service”})
强烈推荐第二种方式,不然漏配置扫描包,项目庞大,出现问题则难排查

4.IDEA中SpringBoot2.X热部署Dev-tool和常见问题(热部署一般在本地使用,方便调试)
4.1 pom文件添加依赖包

org.springframework.boot
spring-boot-devtools
true

   <build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<fork>true</fork><!--必须添加这个配置-->
				</configuration>
			</plugin>
		</plugins>
	</build>
4.2 idea里面要设置,window和mac一样
File ——》setting ——》compiler ——》Build projectautomitically(勾选)

4.3使用快捷键打开,选择Registry
  注意默认快捷键:
  window快捷键 Shift+Ctrl+Alt+/
  mac快捷键 Shift+Command+Alt+/
  选择Registry
  选择compiler.automake.allow.when.app.running ,重启idea就行!!!

5.SpringBoot2.x常见配置文件形式
SpringBoot2.x常见的配置文件形式 xml、yml、properties的区别和使用
5.1 Springboot里面常用xx.yml
YAML(Yet Another Markup Language)
写 YAML 要比写 XML 快得多(无需关注标签或引号) 使用空格 Space 缩进表示分层,不同层次之间的缩进可以使用不同的空格数目
注意:key后面的冒号,后面一定要跟一个空格,树状结构
server:
port: 8080 //设置启动端口号为8080
house:
family:
name: Doe
parents:
- John
- Jane
children:
- Paul
- Mark
- Simone
address:
number: 34
street: Main Street
city: Nowheretown
zipcode: 12345

5.2 Springboot里面常用 xx.properties(推荐)
Key=Value格式
语法简单,不容易出错
server.port=8082

#session失效时间,30m表示30分钟
server.servlet.session.timeout=30m

# Maximum number of connections that the server accepts and processes at any given time.
server.tomcat.max-connections=10000 

# Maximum size of the HTTP post content.
server.tomcat.max-http-post-size=2MB 
server.tomcat.max-http-form-post-size=2MB

# Maximum amount of worker threads
server.tomcat.max-threads=200 

6.SpringBoot注解配置文件映射属性和实体类
6.1配置文件加载 ,实例:pay.properties
wx.pay.appId=542136451241
wx.pay.configId=W12233333
wx.pay.token=12544855511224478855551
方式一
1、Controller上面配置 @PropertySource({“classpath:resource.properties”})
2、增加属性 @Value("${test.name}") private String name;

	@RestController
	@PropertySource({"classpath:pay.properties"})
	@RequestMapping("api/v1/pub/test")
	public class TestController {
		@Value("${wx.pay.appId}")
		private String payAppId;
		@Value("${wx.pay.configId}")
		private String payConfigId;

		@RequestMapping("findPayWX")
		public JsonData findPayWX(){
			Map<String,String> map = new HashMap<>();
			map.put("payAppId",payAppId);
			map.put("payConfigId",payAppId);
			return JsonData.buildSuccess(0,map);
		}
	}
	
	前端返回
	{
		"code": 0,
		"data": {
			"payConfigId": "542136451241",
			"payAppId": "542136451241"
		},
		"msg": null
	}

方式二:实体类配置文件
1、添加 @Component 注解;
2、使用 @PropertySource 注解指定配置文件位置;
3、使用 @ConfigurationProperties 注解,设置相关属性;
4、必须 通过注入IOC对象Resource 进来 ,才能在类中使用获取的配置文件值。@Autowired private ServerSettings serverSettings;

	@Component
	@PropertySource({"classpath:pay.properties"})
	public class PayConfig implements Serializable {
		@Value("${wx.pay.appId}")
		private String appId;
		@Value("${wx.pay.configId}")
		private String configId;
		@Value("${wx.pay.token}")
		private String token;

		public String getAppId() {
			return appId;
		}

		public void setAppId(String appId) {
			this.appId = appId;
		}

		public String getConfigId() {
			return configId;
		}

		public void setConfigId(String configId) {
			this.configId = configId;
		}

		public String getToken() {
			return token;
		}

		public void setToken(String token) {
			this.token = token;
		}
	}

	@RestController
	@RequestMapping("api/v1/pub/test")
	public class TestController {
		@Autowired
		private PayConfig config;
		@RequestMapping("readConfig")
		public JsonData readConfig(){
			Map<String,Object> map = new HashMap<>();
			map.put("appId",config.getAppId());
			map.put("configId",config.getConfigId());
			map.put("token",config.getToken());
			return JsonData.buildSuccess(0,map);
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值