文章目录
1.common-log4j2-starter 1.目录结构 2.Log4j2Properties.java 新增两个属性 3.Log4j2AutoConfiguration.java 条件注入LogAspect 4.ApplicationEnvironmentPreparedListener.java 从Log4j2Properties.java中定义的配置读取信息
2.common-minio-starter
3.common-mybatis-plus-starter 1.目录结构 2.BaseEntity.java 3.SunRaysBaseServiceImpl.java 删除掉事务注解 4.MyBatisPlusProperties.java 统一配置 5.MybatisPLusAutoConfiguration.java 6.application.yml 提供通用配置 7.pom.xml 排除logging
4.common-openai-starter-demo 1.目录结构 2.OpenAiProperties.java
1.common-log4j2-starter
1.目录结构
2.Log4j2Properties.java 新增两个属性
package com. sunxiansheng. log4j2. config. properties ;
import lombok. Data ;
import org. springframework. boot. context. properties. ConfigurationProperties ;
@ConfigurationProperties ( prefix = "sun-rays.log4j2" )
@Data
public class Log4j2Properties {
private boolean logAspectEnable = true ;
private String home = "./logs" ;
private String module = "defaultModule" ;
}
3.Log4j2AutoConfiguration.java 条件注入LogAspect
package com. sunxiansheng. log4j2. config ;
import com. sunxiansheng. log4j2. aspectj. LogAspect ;
import com. sunxiansheng. log4j2. config. properties. Log4j2Properties ;
import com. sunxiansheng. log4j2. trace. TraceIdFilter ;
import lombok. extern. slf4j. Slf4j ;
import org. springframework. boot. autoconfigure. condition. ConditionalOnMissingBean ;
import org. springframework. boot. autoconfigure. condition. ConditionalOnProperty ;
import org. springframework. boot. context. properties. EnableConfigurationProperties ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
import javax. annotation. PostConstruct ;
@Configuration
@EnableConfigurationProperties ( { Log4j2Properties . class } )
@Slf4j
public class Log4j2AutoConfiguration {
@PostConstruct
public void logConfigSuccess ( ) {
log. info ( "Log4j2AutoConfiguration has been loaded successfully!" ) ;
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty ( name = "sun-rays.log4j2.log-aspect-enable" , havingValue = "true" , matchIfMissing = true )
LogAspect logAspect ( ) {
log. info ( "LogAspect 成功注入!" ) ;
return new LogAspect ( ) ;
}
@Bean
@ConditionalOnMissingBean
TraceIdFilter traceIdFilter ( ) {
return new TraceIdFilter ( ) ;
}
}
4.ApplicationEnvironmentPreparedListener.java 从Log4j2Properties.java中定义的配置读取信息
package com. sunxiansheng. log4j2. listener ;
import org. springframework. boot. context. event. ApplicationEnvironmentPreparedEvent ;
import org. springframework. boot. context. logging. LoggingApplicationListener ;
import org. springframework. context. ApplicationListener ;
import org. springframework. core. Ordered ;
import org. springframework. core. env. ConfigurableEnvironment ;
public class ApplicationEnvironmentPreparedListener implements ApplicationListener < ApplicationEnvironmentPreparedEvent > , Ordered {
@Override
public void onApplicationEvent ( ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event. getEnvironment ( ) ;
String logHome = environment. getProperty ( "sun-rays.log4j2.home" , "./logs" ) ;
if ( "./logs" . equals ( logHome) ) {
System . err. println ( "WARNING: sun-rays.log4j2.home 属性未设置,使用默认值: " + logHome) ;
}
String logModule = environment. getProperty ( "sun-rays.log4j2.module" , "defaultModule" ) ;
if ( "defaultModule" . equals ( logModule) ) {
System . err. println ( "WARNING: sun-rays.log4j2.module 属性未设置,使用默认值: " + logModule) ;
}
System . setProperty ( "log.home" , logHome) ;
System . setProperty ( "log.module" , logModule) ;
}
@Override
public int getOrder ( ) {
return LoggingApplicationListener . DEFAULT_ORDER - 1 ;
}
}
2.common-minio-starter
1.MinioProperties.java
package com. sunxiansheng. minio. config. properties ;
import lombok. Data ;
import org. springframework. boot. context. properties. ConfigurationProperties ;
@ConfigurationProperties ( prefix = "sun-rays.minio" )
@Data
public class MinioProperties {
private String endpoint;
private String accessKey;
private String secretKey;
}
3.common-mybatis-plus-starter
1.目录结构
2.BaseEntity.java
package com. sunxiansheng. mybatis. plus. base. entity ;
import com. baomidou. mybatisplus. annotation. TableField ;
import lombok. * ;
import java. io. Serializable ;
import java. util. Date ;
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L ;
@TableField ( "create_by" )
private String createBy;
@TableField ( "create_time" )
private Date createTime;
@TableField ( "update_by" )
private String updateBy;
@TableField ( "update_time" )
private Date updateTime;
}
3.SunRaysBaseServiceImpl.java 删除掉事务注解
package com. sunxiansheng. mybatis. plus. base. service. impl ;
import com. baomidou. mybatisplus. core. conditions. query. QueryWrapper ;
import com. baomidou. mybatisplus. core. mapper. BaseMapper ;
import com. sunxiansheng. mybatis. plus. base. service. SunRaysBaseService ;
import java. io. Serializable ;
import java. util. List ;
public class SunRaysBaseServiceImpl < M extends BaseMapper < T > , T , ID extends Serializable >
implements SunRaysBaseService < T , ID> {
private M mybatisPlusMapper;
public void setMybatisPlusMapper ( M mybatisPlusMapper) {
this . mybatisPlusMapper = mybatisPlusMapper;
}
@Override
public boolean existsById ( ID id) {
T t = mybatisPlusMapper. selectById ( id) ;
return t != null ;
}
@Override
public boolean exists ( T po) {
QueryWrapper < T > queryWrapper = new QueryWrapper < > ( po) ;
return mybatisPlusMapper. exists ( queryWrapper) ;
}
@Override
public Long count ( T po) {
QueryWrapper < T > queryWrapper = new QueryWrapper < > ( po) ;
return mybatisPlusMapper. selectCount ( queryWrapper) ;
}
@Override
public T listById ( ID id) {
return mybatisPlusMapper. selectById ( id) ;
}
@Override
public T listOne ( T po) {
return mybatisPlusMapper. selectOne ( new QueryWrapper < > ( po) ) ;
}
@Override
public List < T > listAll ( T po) {
QueryWrapper < T > queryWrapper = new QueryWrapper < > ( po) ;
return mybatisPlusMapper. selectList ( queryWrapper) ;
}
@Override
public int insertOne ( T po) {
return mybatisPlusMapper. insert ( po) ;
}
@Override
public int updateById ( T po) {
return mybatisPlusMapper. updateById ( po) ;
}
@Override
public int update ( T po, T condition) {
QueryWrapper < T > queryWrapper = new QueryWrapper < > ( condition) ;
return mybatisPlusMapper. update ( po, queryWrapper) ;
}
@Override
public int deleteById ( ID id) {
return mybatisPlusMapper. deleteById ( id) ;
}
@Override
public int deleteBatchByIds ( List < ID> ids) {
return mybatisPlusMapper. deleteBatchIds ( ids) ;
}
@Override
public int delete ( T po) {
QueryWrapper < T > queryWrapper = new QueryWrapper < > ( po) ;
return mybatisPlusMapper. delete ( queryWrapper) ;
}
}
4.MyBatisPlusProperties.java 统一配置
package com. sunxiansheng. mybatis. plus. config. properties ;
import lombok. Data ;
import org. springframework. boot. context. properties. ConfigurationProperties ;
@ConfigurationProperties ( prefix = "sun-rays.mybatis-plus" )
@Data
public class MyBatisPlusProperties {
private boolean sqlBeautyEnabled = true ;
}
5.MybatisPLusAutoConfiguration.java
package com. sunxiansheng. mybatis. plus. config ;
import com. sunxiansheng. mybatis. plus. config. properties. MyBatisPlusProperties ;
import com. sunxiansheng. mybatis. plus. interceptor. SqlBeautyInterceptor ;
import lombok. extern. slf4j. Slf4j ;
import org. springframework. boot. autoconfigure. condition. ConditionalOnMissingBean ;
import org. springframework. boot. autoconfigure. condition. ConditionalOnProperty ;
import org. springframework. boot. context. properties. EnableConfigurationProperties ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
import javax. annotation. PostConstruct ;
@Configuration
@EnableConfigurationProperties ( { MyBatisPlusProperties . class } )
@Slf4j
public class MybatisPLusAutoConfiguration {
@PostConstruct
public void logConfigSuccess ( ) {
log. info ( "MybatisPLusAutoConfiguration has been loaded successfully!" ) ;
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty ( prefix = "sun-rays.mybatis-plus" , value = "sql-beauty-enabled" , havingValue = "true" , matchIfMissing = true )
public SqlBeautyInterceptor sqlBeautyInterceptor ( ) {
log. info ( "SqlBeautyInterceptor 成功注入!" ) ;
return new SqlBeautyInterceptor ( ) ;
}
}
6.application.yml 提供通用配置
spring :
datasource :
driver-class-name : com.mysql.cj.jdbc.Driver
type : com.alibaba.druid.pool.DruidDataSource
druid :
initial-size : 10
min-idle : 10
max-active : 50
max-wait : 30000
timeBetweenEvictionRunsMillis : 60000
minEvictableIdleTimeMillis : 300000
testWhileIdle : true
testOnBorrow : true
removeAbandoned : true
removeAbandonedTimeout : 180
logAbandoned : true
7.pom.xml 排除logging
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-jdbc</ artifactId>
< exclusions>
< exclusion>
< artifactId> spring-boot-starter-logging</ artifactId>
< groupId> org.springframework.boot</ groupId>
</ exclusion>
</ exclusions>
</ dependency>
4.common-openai-starter-demo
1.目录结构
2.OpenAiProperties.java
package com. sunxiansheng. openai. config. properties ;
import lombok. Data ;
import org. springframework. boot. context. properties. ConfigurationProperties ;
@Data
@ConfigurationProperties ( prefix = "sun-rays.openai" )
public class OpenAiProperties {
private String apiKey;
private String apiUrl = "https://api.openai.com/v1/chat/completions" ;
}