springboot2.0 auto configuration自动配置
概念
springboot auto configuration的本质就是自动配置spring的各种bean。然后使应用可以通过@Autowired等注入方式来直接使用bean。
比如自动配置redisTemplate,jdbcTemplate等bean。
原理
1. 开启@EnableAutoConfiguration注解
如果写了@SpringBootApplication注解,不必再写@EnableAutoConfiguration。\
@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
@SpringCloudApplication = @SpringBootApplication + @EnableDiscoveryClient + @EnableCircuitBreaker
2. 开启@EnableAutoConfiguration注解后,会自动引入一个AutoConfigurationImportSelector类
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; /** * Exclude specific auto-configuration classes such that they will never be applied. * @return the classes to exclude */ Class<?>[] exclude() default {}; /** * Exclude specific auto-configuration class names such that they will never be * applied. * @return the class names to exclude * @since 1.3.0 */ String[] excludeName() default {}; }
其会去扫描classpath下所有jar包内的META-INF/spring.factories文件
3. spring.factories文件
spring-boot-autoconfigure.jar的spring.factories文件
spring.factories文件内会有一个org.springframework.boot.autoconfigure.EnableAutoConfiguration的key
configure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\ org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\ org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\ org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\ org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\ org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\ org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\ org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\ org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\ org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\ org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\ org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration,\ org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\ org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration,\ org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\ org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\ org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\ org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\ org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\ org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\ org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\ org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\ org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\ org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\ org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\ org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\ org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\ org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\ org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\ org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\ org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\ org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\ org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\ org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\ org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\ org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\ org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\ org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\ org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\ org.springframework.boot.autoconfigure.reactor.core.ReactorCoreAutoConfiguration,\ org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\ org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\ org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\ org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\ org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\ org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\ org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\ org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientAutoConfiguration,\ org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\ org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\ org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\ org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\ org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\ org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\ org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\ org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\ org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\ org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\ org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration,\ org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\ org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\ org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\ org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\ org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\ org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\ org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\ org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration
spring boot 会自动配置org.springframework.boot.autoconfigure.EnableAutoConfiguration的key下所有自动配置类。
可以看到这里面包含RabbitMq,MongoDB,Redis,JdbcTemplate,Thymeleaf,WebFlux,WebSocket等等模块的自动配置类。
4. 一般自动配置类都有@Configuration注解,相当于一个spring的xml文件,里面可以通过@Bean注解配置bean
@Configuration @ConditionalOnClass(RedisOperations.class) @EnableConfigurationProperties(RedisProperties.class) @Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class }) public class RedisAutoConfiguration { @Bean @ConditionalOnMissingBean(name = "redisTemplate") public RedisTemplate<Object, Object> redisTemplate( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean @ConditionalOnMissingBean(StringRedisTemplate.class) public StringRedisTemplate stringRedisTemplate( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } }
@ConditionalOnXXX:
- @ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean
- @ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean)
- @ConditionalOnExpression(当表达式为true的时候,才会实例化一个Bean)
- @ConditionalOnMissingBean(仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean)
- @ConditionalOnMissingClass(某个class类路径上不存在的时候,才会实例化一个Bean)
- @ConditionalOnProperty(value = “spring.cloud.config.enabled”, matchIfMissing = true)(当某个配置的值为一定条件时,才会实例化一个Bean)
5. 配置bean的时候,少不了要获取properties或者yml文件的属性
5.1 通过@ConfigurationProperties(prefix=”xxxx”)映射配置文件的值到POJO
application.yml
test: id: 1 name: test
随意一个POJO类,例:Properties.java,类名随意,通过prefix指定配置属性的前缀
@ConfigurationProperties(prefix="test") public class Properties { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
5.2 将配置文件的POJO类转化为spring的bean
5.2.1. 在要使用配置的地方使用@EnableConfigurationProperties(Properties.class)将其转化为spring的bean
@RestController @EnableConfigurationProperties(Properties.class) public class CommonController extends BaseController { //转化为spring的bean后,就可以注入使用了 @Autowired private Properties properties; @RequestMapping(value = "/properties", method = RequestMethod.GET) public Properties getProperties() { log.info(properties.toString()); return properties; } }
如果直接写@EnableConfigurationProperties不指定Properties类的话,必须在同一目录或者子目录下才行。
5.2.2. 通过@Bean配置将其转化为spring的bean
@Configuration public class MyConfiguration { @Bean public Properties properties() { return new Properties(); } }
5.2.3. 通过@Component配置将其转化为spring的bean
@Component @ConfigurationProperties(prefix="test") public class Properties { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
转化为spring的bean后,就可以在想用的地方通过任意注入方式注入bean来使用了。
5.2.4. 总结
在写spring boot starter的时候,因为所有类都不在启动的@ComponentScan目录下,所以使用一般使用 5.2.1 或者 5.2.2 的方式获取配置文件信息
在写具体的spring boot应用服务时,配置类都在启动的@ComponentScan目录下,使用 5.2.3 的方式比较方便
原文地址:https://blog.csdn.net/ifrozen/article/details/79931143
实战--springboot配置 rocketmq
starters项目
1.pom.xml
<dependency> <groupId>com.aliyun.openservices</groupId> <artifactId>ons-client</artifactId> <version>1.8.4.Final</version> </dependency>
2.resources->META-INF->spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.bjgxzd.boot.config.MqAutoConfiguration
3.MqAutoConfiguration
@Configuration @ImportAutoConfiguration(value = {MqConfiguration.class}) public class MqAutoConfiguration { @Bean public CommandLineRunner logRunner (){ return args -> { System.out.println("==== GX rocket MQ loaded ===="); }; } }
4.MqConfiguration
@Configuration @EnableConfigurationProperties({MqProperties.class}) public class MqConfiguration { @Autowired private MqProperties mqProperties; @Bean(initMethod = "start", destroyMethod = "shutdown") @ConditionalOnMissingBean(ProducerBean.class) public ProducerBean buildProducer() { ProducerBean producer = new ProducerBean(); producer.setProperties(getMqProperties()); return producer; } private Properties getMqProperties(){ Properties properties = new Properties(); properties.setProperty(PropertyKeyConst.AccessKey, mqProperties.getAccessKey()); properties.setProperty(PropertyKeyConst.SecretKey, mqProperties.getSecretKey()); properties.setProperty(PropertyKeyConst.ONSAddr, mqProperties.getOnsAddr()); return properties; } @ConditionalOnMissingBean(OrderProducerBean.class) @Bean(initMethod = "start", destroyMethod = "shutdown") public OrderProducerBean buildOrderProducer() { OrderProducerBean orderProducerBean = new OrderProducerBean(); orderProducerBean.setProperties(getMqProperties()); return orderProducerBean; } }
5.MqProperties
@Data @ConfigurationProperties(prefix = "mq.rocketmq") public class MqProperties { private String accessKey; private String secretKey; private String onsAddr; }
syncsrv项目
application.yml
mq: rocketmq: access-key: 62f49160c57b4f158a8ab3ebd2ff66cc secret-key: J8H3v+Go55DIimy6gbKu3Bbnc0U= ons-addr: http://172.169.101.121:8080/rocketmq/nsaddr4broker-internal
pom.xml
mq 集成好了,直接依赖 <dependency> <groupId>com.bjgxzd</groupId> <artifactId>gx-mq-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
启动类 QfjsSyncApplication
@SpringBootApplication public class QfjsSyncApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(QfjsSyncApplication.class, args); } @Override public void run(String... args) throws Exception { } }
使用
直接注入就可以发消息了
private final ProducerBean producer; public TestController(ProducerBean producer) { this.producer = producer; }