springboot使用乱记

springboot运行的三种方式
1.直接运行main
2.打成jar包,使用java -jar命令运行
3.spring-boot:run方法运行
 
Springboot热部署
依赖:
<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-devtools</artifactId>  
   <optional>true</optional><!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖该项目的项目如果想要使用devtools,需要重新引入 -->  
</dependency>  
 
SpringBoot重启是reload重启,通过监控classpath的变化,如果classpath中的文件发生变化,即触发重启。springboot通过两个classpath来完成reload,一个basic classloader中加载不变的类,一个restart classloader中加载classpath中的类,重启的时候,restart classloader中的类丢弃并重新加载;
 
排除资源:
spring.devtools.restart.exclude=static/**,templates/**
spring.devtools.restart.additional-exclude=public/** (处理默认配置排除之外的)
spring.devtools.restart.enabled=false  (禁用自动重启)
 
idea实现devtools热部署需要配置两步 :
1、CTRL + SHIFT + A --> 查找make project automatically --> 选中 
2、CTRL + SHIFT + A --> 查找Registry --> 找到并勾选compiler.automake.allow.when.app.running 
 
SpringBoot中的参数设置
①springboot中主要的参数来源及优先级:
     1,命令行参数;
     2,ServletConfig和ServletContext;
     3,操作系统环境变量;
     4,application-{profile}.properties或者YAML文件;
     5,application.properties或者YAML文件;
②springboot提供了方便的properties绑定机制
    1)默认从application.properties中加载配置;
app :
  myname : "leo"
   2)配置值:
@Controller
public class TestController {

    @Value("${app.myname}")
    private String myname;

    @RequestMapping(value = "test",method = RequestMethod.GET)
    @ResponseBody
    public String test(String name){
        System.out.println("============"+myname);
        return myname;
    }
}
③@ConfigurationProperties:
两种绑定方式:
1,直接在类上绑定
2.在bean标签后面绑定
参数绑定标签:可以非常方便的把资源文件中的内容绑定到对象上
资源文件:
app :
  config:
    myname : "leo"
    age : 20
    money : 30

bean:

@Getter
@Setter
@ConfigurationProperties(prefix = "app.config")
@Component
public class User {

    private String myname;
    private Integer age;
    private Integer money;
}
静态资源
1,默认情况下,Springboot会从classpath下的/static、/public、/resources、/META-INF/resources下加载静态资源;
2,可以通过修改spring.resources.staticLocations来修改静态资源加载地址;
3,因为应用是打成jar包,所以之前的src/main/webapp不会加载;
 
Springboot中的日志
日志依赖:
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
</dependency>
①Springboot推荐使用Logback作为日志框架
②springboot默认支持logback.xml或者logback-spring.xml,推荐使用logback-spring.xml,springboot会增加额外功能
③可以通过logging.config=classpath:mylogback.xml指定自己的logback配置文件(不推荐)
④logbak.xml文件结构说明
  • (1)configuration>:Logback根元素,它的属性如下
  • 1,scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。
  • 2,scanPeriod: 设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。
  • 3,debug: 当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。
  • (2)<contextName>:上下文名字
  • (3)<property>:定义属性,可以使用${}在配置文件中使用
  • (4)<appender>:在logback中是用于具体记录日志的组件,可以用来配置日志记录方式,日志记录格式等
  • 常见的appender:
  • 1,ch.qos.logback.core.ConsoleAppender:输出到控制台;
  • 2,ch.qos.logback.core.FileAppender:输出到文件;
  • 3,ch.qos.logback.core.rolling.RollingFileAppender:输出到文件,可以配置滚动策略,当日志达到某个条件之后分文件记录
springboot下打印mybatis日志最便捷的方法:
在application.properties中加入:logging.level.com.seemygo.server.goods.mapper=debug
 
SpringBoot中集成mybatis
1,引入依赖:
<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.0</version>
</dependency>
2,正常完成mapper接口和mapper.xml
3,mybatis-spring-boot-starter提供了以下配置:
(具体参考MyBatisProperties对象)
mybatis.configLocation:mybatis的配置文件地址;
mybatis.mapperLocations:com/xmg/springboot/p2p/mapper/*Mapper.xml
mybatis.typeAliasesPackage:别名扫描包;
4,扫描mapper接口:
第一种方式:使用@MapperScan标签
@SpringBootApplication
@MapperScan(basePackages="com.example.demo.mapper")
public class App {
public static void main(String[] args) {
  SpringApplication.run(App.class, args);
}
}
会自动扫描指定包下的mapper接口;
第二种方式:代码的方式,自己手动提供一个自动扫描配置对象:
@Configuration
@AutoConfigureAfter(MybatisAutoConfiguration.class)
public class MapperScanConfiguration {
@Bean
  public MapperScannerConfigurer mapperScannerConfigurer() {
    MapperScannerConfigurer msc = new MapperScannerConfigurer();
    msc.setSqlSessionFactoryBeanName("sqlSessionFactory");
    msc.setBasePackage("com.xmg.springboot.demo._10mybatis.mapper");
    return msc;
  }
}
SpringBoot事务处理
①配置事务管理器
1,直接开启@EnableTransactionManagement注解;相当于在xml中配置<tx:annotation-driven/>
@SpringBootApplication
@EnableTransactionManagement
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

2.依赖包:

		<dependency>
		    <groupId>org.mybatis.spring.boot</groupId>
		    <artifactId>mybatis-spring-boot-starter</artifactId>
		    <version>1.3.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
3事务控制
(1),如果开启了@EnableTransactionManagement,只需要在service上面使用@Transactional即可;
 
Springboot自动生成的项目框架时如果选择了数据源的问题
(1)如果暂时不需要数据源,可将pom文件中的mysql和mybatis(或其他数据源框架)注释掉,即可正常启动。
(2)在@SpringBootApplication中排除其注入
    @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
 
 

猜你喜欢

转载自www.cnblogs.com/tinyj/p/9788031.html
今日推荐