- Spring Boot 构建
- Spring Cloud 协调
- Spring Cloud Data Flow 连接
Spring Framework
用于构建企业级应用的轻量级一站式解决方案
- 设计理念
- 力争让选择无处不在
- 体现海纳百川的精神
- 保持向后的兼容性
- 专注API设计
- 追求严苛的代码质量
Spring Boot
快速构建基于Spring的应用程序
- 快、很快、非常快
- 尽可开箱即用,退可按需改动
- 提供各种非功能特性
- 不用生成代码,没有XML配置
Spring Cloud
简化分布式系统的开发
- 配置管理
- 服务注册与发现
- 熔断
- 服务追踪
Hello Spring
- 通过Spring Initializr生成骨架(https://start.spring.io/)
- 编写第一段代码
- 运行你的程序
- 分析项目结构
我们使用IDE工具打开下载的代码,我们可以看到Spring的骨架结构如下:
接下来我们编写我们的第一个Spring页面,我们为了方便直接修改入口文件(HelloSpringApplication)中的代码来实现,下面为修改后的代码:
package com.Spring.hello.hellospring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
//增加RestController注解
@RestController
public class HelloSpringApplication {
public static void main(String[] args) {
SpringApplication.run(HelloSpringApplication.class, args);
}
//Hello页面
@RequestMapping("/hello")
public String Hello(){
return "Hello Spring";
}
}
然后运行项目就可以看见第一个SpringBoot项目了!
我们在构建项目的时候添加了Actuator所以,访问URL/actuator/health可以查看项目的运行状态,“up”为正常。
- 我们生成一个如下配置的Spring框架
- 引入对应的数据库驱动–H2
- 引入JDBC依赖–Spring-book-starter-jdbc
- 获取DataSource Bean,打印信息或者通过/acturator/beans查看Bean(必须引入Acturator)
package com.Spring.data.springdata;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@RestController
public class SpringDataApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDataApplication.class, args);
}
private void showConnection() throws SQLException{
log.info(dataSource.toString());
Connestion conn = dataSource.getConnection();
log.info(conn.toString());
conn.close();
}
}
数据源相关
DataSoure(根据选择的连接池实现决定)
事物相关(可选)
PlatformTransactionManager(DataSourceTransactionManager)
Transaction Template操作相关(可选)
Jdbc Template
@Configuration
@EnableTransactionManagemeent
public class DataSourceDemo{
@Autowired
private DataSource dataSource;
public sraric void main(String[] args) throws SQLException{
ApplicationContext applicationcontext = new CLassPathXmlApplicationContext("applicationcontext*.xml");
showBeans(applicationContext);
dataSourceDemo(applicationcontext);
}
@Bean(destrouMethod = "close")
public DataSource dataSource() throws Exception{
Properties properties = new Properties();
Properties.setProperty("driverClassName","org.h2.Driver");
Properties.setProperty("url","jdbc:h2:mem:textdb");
Properties.setProperty("username","sa");
return BasicDataSourceFactory.createDataSource(properties);
}
@Bean
pulic PlatformTransactionManager transactionManager() throws Exception{
return new DataSourceTransactionManager(dataSource());
}
}
DataSourceAutoConfiguration
配置DataSource
DataSourceTransactionManagerAutoConfiguration
配置DataSourceTransactionManager
Jdbc TemplateAutoConfiguration
配置Jdbc Template
如果自己配置了就不会再配置了
通用
spring.datasource.url = jdbc:mysql://localhost/text
spring.datasource.username = dbuser
spring.datasource.password = dbpaw
spring.datasource.driver-class-name = com.mysql.jdbc.Driver(可选)初始化内嵌数据库
spring.datasource.initialization-mode = embeddedj|always|never
spring.datasource.schema与spring.datasource.data确定初始化SQL文件
spring.datasource.platform = hsqldb | h2 | oracle | mysql | postgresql (与前者对应)
不同数据源的配置要分开
关注每次使用的数据源
有多个DataSource时系统如何判断
对应的设施(事物、ORM等)如何选择DataSource
手工配置两组 DataSource 及相关内容
与Spring Boot协同工作(二选一)
配置@Primary类型的Bean
排除SpringBoot的自动配置
DataSourceAutoConfiguration
DataSourceTransactionManagerAutoConfiguration
Jdbc TemplateAutoConfiguration
@SringBootApplication(exclude = { DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class,JdbcTemplateAutoConfiguration.class})
@Slf4j
public class MyltiDataSOurceDemoApplication{
@Bean
@configurationPropertes("fooDataSourceProgerties()")
public DataSourceProperties fooDataSourceProperties(){
return new DataSourcePerties();
}
@Bean
public DataSource fooDataSource(){
DataSourceProperties dataSourceProperties = fooDataSourceProperties();
log.info("foo datasource: {}",dataSourceProperties.getUrl());
return dataSourceProperties.initializeDataSourceBuilder().build();
}
@Bean
@Resource
public PlatformTransactionManager fooTxManager(DataSource fooDataSource){
return new DataSourceTransactionManager(fooDataSource);
}
@Bean
@ConfigurationProperties("bar.datasource")
public DataSourceProperties barDataSourceProperties(){
return new DataSourceProperties();
}
@Bean
public DataSource barBataSource(){
DataSourcePorperties datasourceporprties = barDataSourceProperties();
log.info("bar datasource:{}",dataSourceProperties.getUrl());
return dataSourceProperties.initializeDataSourceBuilder().build();
}
@Bean
public PlatformTransactionManager barTxManager(DataSource barDataSource){
return new DataSourceTransactionManager(barDataSource);
}
}
在Spring Boot中配置HikariCP
Spring Boot 2.X
默认使用HikariCP
配置 spring.datasource.hikari.*Spring Boot 1.X
默认使用Tomcat连接池,需要移除tomcat-jdbc依赖
spring.datasource.type = com.zaxxer.hikari.HikariDataSource
常用配置参数
spring.datasource.hikari.maximumPoolSize = 10
spring.datasource.hikari.minimumldle = 10
spring.datasource.hikari.idleTimeout = 600000
spring.datasource.hikari.connectionTimeout = 30000
spring.datasource.hikari.maxLifetime = 1800000
详细文档见官网
- 详细的监控
- ExceptionSorter,针对主流数据库的返回码都有支持
- SQL防注入
- 内置加密配置
- 众多扩展点,方便定制
官网(中文)
数据源配置
直接配置DruidDataSource
通过druid-spring-boot-starter
spring.datasource.druid.*
常见配置
spring.output.ansi.enablerd = ALWAYS
spring.datasource.druid.url = jdbc:h2:mem:foo
spring.datasource.druid.username =
spring.datasource.druid.password =
spring.datasource.druid.initial-size = 5
spring.datasource.druid.max-active = 5
spring.datasource.druid.min-idle = 5
spring.datasource.druid.filters = conn,config,stat,slf4j
spring.datasource.druid.connection-properties = config.decrypt = true;config.decrypt.key = ${public-key}
spring.datasource.druid.test-on-borrow = true
spring.datasource.druid.test-on-return = true
spring.datasource.druid.test-while-idle =true
Filter配置
spring.datasource.druid.filters = stat,config,wall,log4j
密码加密
spring.datasource.password = <加密密码>
spring.datasource.druid.filter.config.enabled = true
spring.datasource.druid.connection-properties = config.decrypt = true;config.decrypt.key =SQL防注入
spring.datasource.druid.filter.wall.enabled = true
spring.datasource.druid.filter.wall.db-type = h2
spring.datasource.druid.filter.wall.config.delete-allow = false
spring.datasource.druid.filter.wall.config.drop-table-allow = false
Druid Filter
用于定制连接池操作的各种环节
可以继承FilterEventAdapter以方便的实现Filter
修改META-INF/druid-filter.properties增加Filter配置
@Slf4j
public class ConnectionLogFilter extends FillterEventAdapter{
@Override
public void connection_connectBefore(FilterChain chain,Properties info){
log.info("BEFORE CONNECTION!")
}
@Override
public void connection_connectAfter(ConnectionProxy connection){
log.info("AFTER CONNECTION!")
}
}