# springboot_rabbitMQ
**Repository Path**: yunds/spring-boot-examples
## Basic Information
- **Project Name**: springboot_rabbitMQ
- **Description**: springboot + rabbitMQ例子
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2018-10-09
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
## spring boot综合示例,集成了spring mvc + spring data jpa + spring cache
## 1.使用io.spring.platform管理依赖
Spring IO Platform,简单的可以认为是一个依赖维护平台,该平台将相关依赖汇聚到一起,针对每个依赖,都提供了一个版本号;
这些版本对应的依赖都是经过测试的,可以保证一起正常使用。
> 参考:http://platform.spring.io/platform/
>
> pom依赖配置如下:
```xml
io.spring.platform
platform-bom
Cairo-SR1
pom
import
```
## 2.替换默认的Logback
> 需要先将默认的日志排除,然后再引入log4j2
```xml
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j2
```
## 3.导入web项目常用的一些场景的包
### 3.1.导入测试相关包
```xml
org.springframework.boot
spring-boot-starter-test
```
### 3.2.导入测试相关包
```xml
org.springframework.boot
spring-boot-starter-test
```
### 3.3.导入spring mvc相关包
```xml
org.springframework.boot
spring-boot-starter-web
org.springframework
spring-web
org.springframework
spring-webmvc
com.fasterxml.jackson.core
jackson-databind
```
### 3.4.导入spring data jpa相关包
> 这边使用一个内存数据库H2作为demo数据库,实际情况请替换相关的数据库
```xml
org.springframework.boot
spring-boot-starter-data-jpa
com.h2database
h2
```
### 3.5.导入spring cache相关包
> spring boot最新版本已经将默认的guava缓存替换成了caffeine,感兴趣的可以自行百度,后者缓存效率要比guava高很多
```xml
org.springframework.boot
spring-boot-starter-cache
com.github.ben-manes.caffeine
caffeine
```
### 3.6.导入Apache commons工具包
> spring boot最新版本已经将默认的guava缓存替换成了caffeine,感兴趣的可以自行百度,后者缓存效率要比guava高很多
```xml
org.apache.commons
commons-lang3
commons-beanutils
commons-beanutils
commons-collections
commons-collections
```
## 4.设置maven打包插件,指定入口文件
```xml
spring-boot-examples
org.springframework.boot
spring-boot-maven-plugin
com.future.AppApplication
true
repackage
```
## 5.在resources目录下添加相关配置文件
假如我们有3个环境分别是:开发环境=dev,测试环境=test,生产环境=prod,则我们可以建立如下配置文件:
> application.yml
>
> application-dev.yml
>
> application-test.yml
>
> application-prod.yml
>
> log4j2-dev.yml
>
> log4j2-test.yml
>
> log4j2-prod.yml
>
spring的一些通用配置可以放在application.yml中,其它环境相关的配置可以放到相关环境里面,在打包部署的时候可以通过以下方式进行指定:
>
# 默认的profile为dev,其它环境通过指定启动参数使用不同的profile,比如
# 开发环境:java -jar my-spring-boot.jar --spring.profiles.active=dev
# 生产环境:java -jar my-spring-boot.jar --spring.profiles.active=prod
spring:
profiles:
active: dev
在不同环境下的一些差异配置如:
>
# 不同环境的日志文件
logging:
config: classpath:log4j2-dev.xml
# 不同环境的服务端口
server:
port: 8080
# 不同环境的数据库配置
spring:
# 数据库配置
datasource:
url: jdbc:h2:file:~/dev
username: sa
password: 123
driver-class-name: org.h2.Driver
h2:
console:
enabled: true
path: /h2
jpa:
generate-ddl: false
show-sql: true
properties:
hibernate:
format_sql: true
# 缓存配置
cache:
type: caffeine
cache-names: demos
caffeine:
spec: maximumSize=500,expireAfterWrite=5s
# 自定义属性配置
demo:
p1: this is p1 dev env properties
yunpian:
apikey: xxxxxxxxxx
## 6.编写Application文件
>
package com.future;
/**
* 入口文件
*
* @author WUWY
* @create 2018-05-12 上午10:10
**/
@SpringBootApplication
@EnableCaching
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
该入口文件启用了缓存管理,指定了自定义属性配置,注意在main这种方式启动的应用适合直接运行jar包,如果需要部署到tomcat,请参考如下代码:
>
package com.future;
/**
* 入口文件
*
* @author WUWY
* @create 2018-05-12 上午10:10
**/
@SpringBootApplication
@EnableCaching
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
public class Application extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
## 7.项目启动时执行特定方法
spring boot启动之后有时候需要做些数据初始化操作,Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner,以下是使用CommandLineRunner的方式实现示例代码:
>
package com.future.init;
/**
* 在spring boot启动完成之后会执行的方法
*
* 这里只是举例,比如初始化一些数据,当然初始化数据和脚本升级管理可以考虑集成flyway
*
* @author WUWY
* @create 2018-05-12 上午10:55
**/
@Component
public class InitData implements CommandLineRunner {
@Autowired
private PersonService personService;
@Override
public void run(String... args) throws Exception {
Person person = new Person();
person.setFirstname("wu");
person.setLastname("wenyu");
Person person1 = new Person();
person1.setFirstname("zou");
person1.setLastname("shuiyun");
Person person2 = new Person();
person2.setFirstname("wu");
person2.setLastname("baiyun");
personService.add(person);
personService.add(person1);
personService.add(person2);
}
}
## 8.集成spring data jpa
通常是直接继承JpaRepository,JpaRepository定义了基本的CRUD和分页排序操作,spring帮我们做了默认实现,通常情况下我们不需要自己编写这些方法如:
>
/**
* 基础数据DAO
*
* @author WUWY
* @create 2018-05-12 上午11:20
**/
@NoRepositoryBean
public interface BaseRepository, I extends Serializable> extends JpaRepository {
}
接下去我们的业务对应的dao需要继承自baseRepository
>
/**
* PERSON DAO
*
* @author WUWY
* @create 2018-05-12 上午10:50
**/
@Repository
public interface PersonRepository extends BaseRepository {
/**
* 参考官方文档:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/
* 可以简单的搜索findBy,OrderBy等
* @param firstName
* @return
*/
List findByFirstnameLikeOrderByLastnameDesc(String firstName);
}
自此我们已经实现了一个Person实体对应的dao,并可以通过接口方法名称增加自己的业务逻辑,而不需要做任何实现,如上面的findByFirstnameLikeOrderByLastnameDesc,通过firstname的模糊匹配查询并按照lastname做降序排序
## 9.集成缓存能力
通常我们需要定义缓存名称常量类
>
package com.future.common;
/**
* 定义了缓存名称常量
*
* @author WUWY
* @create 2018-05-12 上午11:44
**/
public interface CacheNames {
String DEMOS = "demos";
}
接下来在我们的service中使用缓存,@CacheConfig设置缓存名称,@Cacheable设置缓存内容,@CacheEvict移除指定缓存
>
/**
* PERSON SERVICE
*
* @author WUWY
* @create 2018-05-12 上午10:51
**/
@Service
@CacheConfig(cacheNames = CacheNames.DEMOS)
public class PersonService extends BaseService {
private static final Logger log = LoggerFactory.getLogger(PersonService.class);
@Autowired
private PersonRepository repository;
@Cacheable(key = "#p0")
@Override
public Person findStrictOne(String id) {
log.debug("穿透缓存进来了");
return super.findStrictOne(id);
}
@CacheEvict(key = "#p0")
@Override
public Person update(String id, Map map) {
return super.update(id, map);
}
@CacheEvict(key = "#p0")
@Override
public void delete(String id) {
super.delete(id);
}
public List findByFirstnameLikeOrderByLastnameDesc(String firstName) {
return repository.findByFirstnameLikeOrderByLastnameDesc(firstName);
}
}