springboot-配置

1.配置Tomcat端口 和项目名称
server.port = 8080
server.context-path=/springboot
2.配置热部署
第一步:添加依赖

<dependency>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-devtools</artifactId>
	        <optional>true</optional>
</dependency>
第二步:配置插件
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
	      <fork>true</fork>
</configuration>

第三步:打开自动编译

File -> Settings -> Build,Execution,Deployment->Compiler下,
打开Build project automatically选项
第四步:打开运行时编译
按快捷键 Shift+Ctrl+Alt+/ ,选择 Registr,打开
compiler.automake.allow.when.app.running选项
3.配置filter
第一步:创建自己的filter类实现filter接口,重写doFilter方法
第二步:在核心类中添加bean的管理

@Bean
	public FilterRegistrationBean init(){
		FilterRegistrationBean rbean = 	new FilterRegistrationBean();
		rbean.setFilter(new MyFilter());
		rbean.addUrlPatterns("/*");
		rbean.setName("myFilter");
		return  rbean;
	}

4.配置application.properties中配置数据:
com.name = hello
在bean中注入配置数据
@Value("${com.name}")
修改默认的配置文件

SpringApplication app = new SpringApplication(SpringbootTestApplication.class);
		Properties properties = new Properties();
		properties.load(SpringbootTestApplication.class.getClassLoader().getResourceAsStream("test.properties"));
		app.setDefaultProperties(properties);
		app.run(args);

5.配置自定义父工程,修改pom文件,将原来的parent节点换成指定依赖

<dependencyManagement>
		<dependencies>
			<dependency>
				<!-- Import dependency management from Spring Boot -->
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>1.5.10.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

6.springboot使用yml文件配置
springboot使用两种配置文件,一种是application.properties,另外一种是application.yml文件。yml文件层次感强些如下:

spring:
  profiles:
    active: prod
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test
    username: root
    password: root

yml 文件在写的时候层次感强,而且少写了代码。

yml的一些配置:

server:  
  port: 8090  //配置端口
  session-timeout: 30  
  tomcat.max-threads: 0  
  tomcat.uri-encoding: UTF-8  
  
spring:  
  datasource:  //数据库配置
    url : jdbc:mysql://localhost:3306/mydb  
    username : root  
    password : root  
    driverClassName : com.mysql.jdbc.Driver

在代码中获取yml文件的数据与之前是一致的。
配置格式:
spring:
datasource:
driver: abc

获取方式:
@Value("${spring.datasource.driver}")
private String driver;
yml的多套配置:
根据不同的环境编写多套配置:
application-aa.yml和application-bb.yml
在主配置中(application.yml)编写如下代码:
spring:
profiles:
active: aa

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值