springBoot的使用二

本文介绍如何在Spring Boot项目中管理依赖及配置日志,包括使用自定义parent、依赖覆盖、日志输出格式等关键内容。

每个项目都要继承parent、

<!-- Inherit defaults from Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
</parent>

但是现在公司很少使用都是自己封装的parent作为父,对于依赖管理可能会使用scope=import 依赖保证依赖管理的好处

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.7.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
进行这个操作时,就不能管理我们自己定义的依赖,这时我们需要设置一个入口,让自己的依赖在spring-boot-dependencies依赖之前执行就可以

例如:

<dependencyManagement>
    <dependencies>
        <!-- Override Spring Data release train provided by Spring Boot -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Fowler-SR2</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.7.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
如果想换一个版本使用就添加属性,不然就使用默认的1.6版本

<properties>
    <java.version>1.8</java.version>
</properties>
使用maven 插件,把项目打成可执的jar包,需要在票plugins中添加:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

如果你想使用spring  和 Jpa   加入一个 spring-boot-starter-data-jpa

spring-boot-starter-*   *为你想导入的jar


详细:https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-dependency-management





注解:

@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。


配置日志文件使用:

 protected static Logger logger=LoggerFactory.getLogger(HelloController.class); 

logger.debug("访问helloName,Name={}",name);                     访问helloName,Name=都是

  1. <configuration>    
  2.     <!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,,,, -->    
  3.     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">    
  4.         <encoder>    
  5.             <pattern>%d %p (%file:%line\)- %m%n</pattern>  
  6.             <charset>UTF-8</charset>   
  7.         </encoder>    
  8.     </appender>    
  9.     <appender name="baselog"    
  10.         class="ch.qos.logback.core.rolling.RollingFileAppender">    
  11.         <File>log/base.log</File>    
  12.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">    
  13.             <fileNamePattern>log/base.log.%d.%i</fileNamePattern>    
  14.             <timeBasedFileNamingAndTriggeringPolicy  class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">    
  15.                 <!-- or whenever the file size reaches 64 MB -->    
  16.                 <maxFileSize>64 MB</maxFileSize>    
  17.             </timeBasedFileNamingAndTriggeringPolicy>    
  18.         </rollingPolicy>    
  19.         <encoder>    
  20.             <pattern>    
  21.                 %d %p (%file:%line\)- %m%n  
  22.             </pattern>    
  23.             <charset>UTF-8</charset> <!-- 此处设置字符集 -->   
  24.         </encoder>    
  25.     </appender>    
  26.     <root level="info">    
  27.         <appender-ref ref="STDOUT" />    
  28.     </root>    
  29.     <logger name="com.example" level="DEBUG">    
  30.         <appender-ref ref="baselog" />    
  31.     </logger>    
  32. </configuration> 



对于spring boot整合redis的使用:需要使用到一个StringRedisTemplate这个类

public class StringRedisTemplate extends RedisTemplate<String, String>

使用redis还需要使用这样一个接口:

  1.  @Resource(name="stringRedisTemplate")  
  2.     ValueOperations<String,String> valOpsStr;

@Autowire 针对类型进行注入,resource针对对象名作为参数进行注入使用

Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型


Springboot  只会扫描main所在的包的子包或者本包


在mybatis中xml配置中生成ID,可以使用(replace(uuid(), '-', '')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值