6.1 SpringBoot的核心:基本配置
6.1.1 入口类和SpringBootApplication
1.SpringBoot通常有*Application的入口类,入口类有一个Main方法。
main方法是标准的Java应用程序入口方法,
2.@SpringBootApplication是一个组合注解
@EnableAutoConfiguration可用让SpringBoot根据类中的jar依赖为当前的项目进行配置
eg. 添加了SpringBoot-starter-data-jpa.Spring会自动进行JPA的相关配置
3.SpringBoot会自动扫描@SpringBootApplication所在类的同级包及下级包的Bean
举例:
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
6.1.2 关闭特定的自动配置
根据SpringBootApplication可以看出只要加上exclude的参数就行
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
举例:
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude")
Class<?>[] exclude() default {};
6.1.3 定制banner
1.在src/main/resources下新建一个banner.txt
2.生成字符复制进去就行
6.1.4 SpringBoot的配置文件
1.SpringBoot使用一个全局的配置文件Application.properties或者.yml放在src/main.resources
2.支持proerties和yaml语言配置
3.配置文件的作用是对默认值的修改
举例:
server:
port: 8081
context-path: /chao
spring:
thymeleaf:
prefix: classpath:/templates/
suffix: .html
encoding: utf-8
content-type: text/html
mode: HTML5
cache: false
freemarker:
charset: UTF-8
content-type: text/html
cache: false
suffix: .html
url:
security:
## 拦截需要验证的url,数组的形式
## authenticated: /private/**,/framework/**
authenticated: /private/**
6.1.5 starter pom
1.Spring为我们提供了简化企业级开发的大多数场景下的starter pom
官方:
6.1.6 使用XML配置(感觉非常重要!因为传统的项目迁移为新的SpringBoot项目使用这个简单)
1.SpringBoot提供无XML配置,但是实际过程中,我们可以通过Spring提供的@ImportResource来加载XML配置
2.注意:
1.应该是SpringBoot只有一个properties,所以配置的参数必须在统一的文件里面
2.不能有EnableCaching的注解,它依赖RedisAuto
举例:
@SpringBootApplication(exclude = RedisAutoConfiguration.class)
//@EnableCaching//表示redis开启缓存
@ImportResource(value = "classpath:spring-redis.xml")
dev.properties
#redis配置信息
redis.host=139.198.176.43
redis.port=6379
redis.password=123456
redis.timeout=1000
redis.pool.maxActive=1024
redis.pool.maxIdle=200
redis.pool.minIdle=10
redis.pool.maxWait=3000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
spring-redis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
<!-- classpath:redis.properties; -->
<!-- 必须要把 redis.properties 中的参数迁移到统一的地方 application-dev.properties-->
<!--<bean id="propertyConfigurer"-->
<!--class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">-->
<!--<property name="location" value="classpath:redis.properties"/>-->
<!--</bean>-->
<!-- redis连接池的配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="minIdle" value="${redis.pool.minIdle}"/>
<property name="maxIdle" value="${redis.pool.maxIdle}"/>
<property name="maxTotal" value="${redis.pool.maxActive}"/>
<property name="maxWaitMillis" value="${redis.pool.maxWait}"/>
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
</bean>
<!-- redis连接器,不是必选项:timeout/password -->
<bean id="pcJedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="password" value="${redis.password}"></property>
<property name="timeout" value="${redis.timeout}"></property>
<property name="usePool" value="true"></property>
<property name="poolConfig" ref="jedisPoolConfig"></property>
</bean>
<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="pcJedisConnectionFactory"></property>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
</beans>