spring boot学习笔记2

开场知识:

spring 容器注入bean,时容器初始化的一些接口以及接口调用的时间先后顺序:

1)BeanFactoryPostProcessor

容器初始化的回调方法

* BeanFactoryPostProcessor在spring容器初始化之后触发,而且只会触发一次

* 触发的时机比BeanPostProcessor早

@Component

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)

throws BeansException {

System.out.println("=========BeanFactoryPostProcessor========"+ beanFactory.getBeanPostProcessorCount());

}

}

2)BeanDefinitionRegistryPostProcessor//动态的注入bean

例子:

这里的例子是一次性注入10个person的bean,并为这10个person的bean的name属性赋值了

BeanDefinitionRegistryPostProcessor

可以拿到ConfigurableListableBeanFactory和BeanDefinitionRegistry两个对象

@Component

public class MyBeanDefinitionRegistryPostProcessor implements

BeanDefinitionRegistryPostProcessor {

public void postProcessBeanFactory(

ConfigurableListableBeanFactory beanFactory) throws BeansException {

}

public void postProcessBeanDefinitionRegistry(

BeanDefinitionRegistry registry) throws BeansException {

for(int i=1;i<=10;i++){

BeanDefinitionBuilder bdb=BeanDefinitionBuilder.rootBeanDefinition(Person.class);//这个是构造beanDefinition的

bdb.addPropertyValue("name", "admin"+i);

registry.registerBeanDefinition("person"+i, bdb.getBeanDefinition());

}

}

}

3)BeanPostProcessor

里面有两个方法:

postProcessBeforeInitialization(Object bean, String name)//在bean注入spring容器之前可以写一些逻辑

postProcessAfterInitialization(Object bean, String name)//在bean注入spring容器之后可以写一些逻辑

  1. spring boot aop开发流程:
  2. spring-boot-starter-aop 加入依赖,默认就开启了AOP的支持
  3. 写一个Aspect 封装横切关注点(日志、监控等等),需要配置通知(前置通知,后置通知等等)和切入点(包的哪些类,哪些方法)
  4. 这个Aspect需要纳入spring容器中管理,并需要加入@Aspect注解
  5. spring.aop.auto配置项决定是否启用AOP,默认启用
  6. spring.aop.proxy-target-class=true 是启用cglib的动态代理,如果是false 则是启用jdk的动态代理,  jdk的动态代理是需要接口的,如果配置了false ,而类中没有接口,则依然使用cglib
  7. 注解:@EnableAspectJAutoProxy 意思就是启用aop,并且默认是启用jdk的动态代理  就相当于在application.properties文件中配置了spring.aop.auto=true

spring.aop.proxy-target-class=false

  • 14.
  • @EnableConfigurationProperties注解的应用:

/**

  1. 因为我们在装配RedisAutoConfiguration这个类的时候,是会用到RedisProperties这个类的host,port的属性值
  2. @ConditionalOnClass(Jedis.class) 这个注解的意思是,只有下面的Jedis(redis的java实现类)存在时才进行bean的装配
  3. @ConditionalOnMissingBean  这个注解是当被@bean标注的bean在spring容器中不存在时,才注入到spring容器

**/

spring boot学习笔记2

下面这个类是把application.properties文件中的属性注入到了RedisProperties这个类中

@ConfigurationProperties(prefix=”redis”)

public class  RedisProperties{

private String host;

private Integer port;

//对应的get/set方法

}

15.从其他project中注入的bean如何引入到本project?

两种方法:

1):自己编写注解@EnableXXX  在这个注解中引入@Import(需要注入bean的.class)

spring boot学习笔记2

然后在本project中,用到需要注入的地方的类上面加入@EnableXXX(1.中编写的注解)

spring boot学习笔记2

这样就可以把不在同一个project的bean注入到其他prj里了

2):在resources文件夹下新建META-INF/spring.factories

在spring.factories文件中写入:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=自己要注入的类的全称

16.开发一个starter的步骤:

1):新建一个maven项目

2):需要一个配置类,配置类里面需要装配好,需要提供出去的类

3):

(1)使用@EnableXXX  配合@Import导入需要装配的类

(2)或者META-INF/spring.factories  在org.springframework.boot.autoconfigure.EnableAutoConfiguration配置需要装入的

17.spring boot的日志

Spring boot默认的日志级别是INFO,可以通过logging.level.*=debug  *可以是包,也可以是类,最上面的是root

日志级别有TRace Debug,warn error,fatal,off(表示关闭日志输出)

日志的文件输出:logging.file=e:/temp/my.log

也可以指定路径:logging.path=e:/temp/logs  指定日志输出目录,此时的日志名字默认是spring.log

日志文件输出,文件大小10M之后就分割了

logging.pattern.console=

logging.file.console=

指定控制台和文件输出的格式

Logback  学习下logback.xml配置  log4j2.xml

Spring boot 默认支持logback 也就是说需要在classpath下放置logback.xml或者logback-spring.xml即可定制日志的输出

使用其他的日志组件步骤:

  1. 排除掉默认的日志组件spring-boot-logging
  2. 加入新的日志组件依赖
  3. 把相应的配置文件放在classpath下

18.spring boot的监控和度量

Pom.xml文件中需要引入依赖:spring-boot-actuator

actuator是spring boot提供的对应用系统的自省和监控的集成功能

学习actuator资料:https://segmentfault.com/a/1190000004318360?_ea=568366

监控就是可以在浏览器中查看一些beans等等,比如:http://localhost:8080/beans

浏览器最好安装JSONView插件(火狐)

http://localhost:8080/env 查看spring boot中的一些配置

http://localhost:8080/mappings 查看spring boot以及系统中的一些映射(包括一些controller)

http://localhost:8080/autoconfig 通过autoconfig配置到spring boot中的一些类,查看自动配置的使用情况

自定义实现健康状态监测,实现HealthIndiacator接口,并纳入到spring容器的管理之中

CounterService  用来计数的服务,可以直接使用

spring boot学习笔记2

统计/user/home被访问的次数

在浏览器中输入:http://localhost:8080/metrics  查看user.home.request.count的属性值就可以知道这个url被访问的次数

19.spring boot的测试

需要在测试类上加上注解:@RunWith(SpringRunner.class) @SpringBootTest

Spring boot 测试步骤:

直接在测试类上加如下两个注解:

@RunWith(SpringRunner.class) @SpringBootTest

19.1 测试Dao的方法的步骤

spring boot学习笔记2

19.2测试spring容器中是否有某个bean:

spring boot学习笔记2

只在测试环境下注入某些bean的方法:(在测试环境下注入Runnable) 注意是@TestConfiguration,不能用@Configuration ,并且这个类只会在测试环境下有效,

spring boot学习笔记2

spring boot学习笔记2

19.3测试application.properties文件中的值的方法:

Spring boot中使用 Environment来获取文件中的属性值

spring boot学习笔记2

在测试环境中,spring boot优先获取test resources下的配置文件application.properties,如果test resources下没有配置文件,那么它就会去 main resources文件夹下找配置文件

两种给配置加配置项的方法:

  1. 在@SpringBootTest注解中添加属性properties
  2. 利用@Before  和ConfigurableEnvironment.addEnvironment()

spring boot学习笔记2

20.spring boot + mybatis环境搭建

<parent>

Spring-boot-starter-parent

</parent>

<properties>

<maven.compiler.source>1.8</maven.compiler.source>

<maven.compiler.target>1.8</maven.compiler.target>

</properties>

<dependeccies>

<dependency>

Spring-boot-starter-web

</dependency>

<dependency>

Spring-boot-starter-jdbc

</dependency>

<dependency>

mybatis

</dependency>

<dependency>

Mybatis-spring-boot-starter

</dependency>

<dependency>

Mysql-connector-java

</dependency>

</dependencies>

编写接口Mapper的时候需要在其上加入注解@Mapper

例如:

@Mapper

Public interface ProductMapper{

}

Mybatis 基于注解的方式:

spring boot学习笔记2

其实这个Mapper上没有类似@Component注入到spring容器中,但它确实已经存在spring容器中了,所以可以通过ConfigurableApplicationContext.getBean(XXXMapper.class)获取,获取完之后就可以直接用这个获取的类调用接口中的增删改查,往数据进行数据读写等操作了,所以spring boot 与mybatis集成很方便

21.spring boot 的打包:

使用插件:这个插件介绍的地址是:

http://www.mojohaus.org/appassembler/appassembler-maven-plugin/

spring boot学习笔记2

我们使用的是第一个:appassembler-assemble是指打包一个可执行的命令

需要在pom.xml文件中加入配置

<build>

<plugins>

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>appassembler-maven-plugin</artifactId>

<version>1.10<version>

<configuration>

spring boot学习笔记2

</configuration>

.......

<plugin>

</plugins>

</build>

注意上面的<mainClass></mainClass>是配置启动类,需要配置App.class那个的包名:

例如:<mainClass>com.zhangshitong.springboot.App</mainClass>

那么如何进行打包呢:

如果是windows平台,那么cmd命令切换到工程名的文件夹下,然后执行:

mvn clean package appassembler:assembler

接着就能看见平台进行打包,一段时间过后,在工程名的target文件夹下会生成打包生成的一些文件,那么我们的可执行文件就在bin文件夹下会生成.bat文件,然后在该目录下执行就是部署了项目

Linux平台下的运行,用windows平台打好包之后传到linux平台,首先给目标文件赋予权限chmod +x

然后执行bin文件夹下的linux平台下的执行文件./xxxx

上一篇:Java线程池参数


下一篇:keras 中如何自定义损失函数