开场知识:java
spring 容器注入bean,时容器初始化的一些接口以及接口调用的时间前后顺序:linux
1)BeanFactoryPostProcessorweb
容器初始化的回调方法redis
* BeanFactoryPostProcessor在spring容器初始化以后触发,并且只会触发一次spring
* 触发的时机比BeanPostProcessor早sql
@Componentsegmentfault
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {windows
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)浏览器
throws BeansException {springboot
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容器以后能够写一些逻辑
spring.aop.proxy-target-class=false
/**
**/
下面这个类是把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)
而后在本project中,用到须要注入的地方的类上面加入@EnableXXX(1.中编写的注解)
这样就能够把不在同一个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便可定制日志的输出
使用其余的日志组件步骤:
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 用来计数的服务,能够直接使用
统计/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的方法的步骤
19.2测试spring容器中是否有某个bean:
只在测试环境下注入某些bean的方法:(在测试环境下注入Runnable) 注意是@TestConfiguration,不能用@Configuration ,而且这个类只会在测试环境下有效,
19.3测试application.properties文件中的值的方法:
Spring boot中使用 Environment来获取文件中的属性值
在测试环境中,spring boot优先获取test resources下的配置文件application.properties,若是test resources下没有配置文件,那么它就会去 main resources文件夹下找配置文件
两种给配置加配置项的方法:
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 基于注解的方式:
其实这个Mapper上没有相似@Component注入到spring容器中,但它确实已经存在spring容器中了,因此能够经过ConfigurableApplicationContext.getBean(XXXMapper.class)获取,获取完以后就能够直接用这个获取的类调用接口中的增删改查,往数据进行数据读写等操做了,因此spring boot 与mybatis集成很方便
21.spring boot 的打包:
使用插件:这个插件介绍的地址是:
http://www.mojohaus.org/appassembler/appassembler-maven-plugin/
咱们使用的是第一个:appassembler-assemble是指打包一个可执行的命令
须要在pom.xml文件中加入配置
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.10<version>
<configuration>
</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