当前位置: 首页 > news >正文

招生网站建设的意义广州 网站建设 020

招生网站建设的意义,广州 网站建设 020,做众筹网站,网站空间查询工具一、为什么用SpringBootSpringBoot优点创建独立Spring应用内嵌web服务器自动starter依赖#xff0c;简化构建配置自动配置Spring以及第三方功能提供生产级别的监控、健康检查及外部化配置无代码生成、无需编写XMLSpringBoot缺点人称版本帝#xff0c;迭代快#xff0c;需要时…一、为什么用SpringBootSpringBoot优点创建独立Spring应用内嵌web服务器自动starter依赖简化构建配置自动配置Spring以及第三方功能提供生产级别的监控、健康检查及外部化配置无代码生成、无需编写XMLSpringBoot缺点人称版本帝迭代快需要时刻关注变化封装太深内部原理复杂不容易精通官网文档架构二、入门maven配置给maven 的settings.xml配置文件的profiles标签添加 profilesprofileidjdk-1.8/idactivationactiveByDefaulttrue/activeByDefaultjdk1.8/jdk/activationpropertiesmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.targetmaven.compiler.compilerVersion1.8/maven.compiler.compilerVersion/properties/profile/profiles2、Spring Boot HelloWorld实现功能浏览发送/hello请求响应 HelloSpring Boot 2 引入依赖 parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion1.5.9.RELEASE/version/parentdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency/dependencies创建主程序类/*** 主程序类* SpringBootApplication这是一个SpringBoot应用*/ SpringBootApplication public class MainApplication {public static void main(String[] args) {SpringApplication.run(MainApplication.class,args);} }编写业务RestController public class HelloController {RequestMapping(/hello)public String handle01(){return Hello, Spring Boot 2!;} }测试直接运行main方法简化配置server.port8888简化部署 buildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins /build把项目打成jar包直接在目标服务器执行即可。三、了解自动配置原理依赖管理1父项目做依赖管理依赖管理 parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.4.RELEASE/version /parent它的父项目 parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion2.3.4.RELEASE/version /parent几乎声明了所有开发中常用的依赖的版本号,自动版本仲裁机制2开发导入starter场景启动器dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId /dependencyspring-boot-starter-web帮我们导入了web模块正常运行所依赖的组件见到很多 spring-boot-starter-* *就某种场景Spring Boot将所有的功能场景都抽取出来做成一个个的starters启动器只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器SpringBoot所有支持的场景支持场景见到的 *-spring-boot-starter 第三方为我们提供的简化开发的场景启动器。所有场景启动器最底层的依赖dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactIdversion2.3.4.RELEASE/versionscopecompile/scope /dependency3无需关注版本号自动版本仲裁引入依赖默认都可以不写版本引入非版本仲裁的jar要写版本号。4可以修改默认版本号1、查看spring-boot-dependencies里面规定当前依赖的版本 用的 key。 2、在当前项目里面重写配置propertiesmysql.version5.1.43/mysql.version/properties2、自动配置1自动配好Tomcat引入了Tomcat依赖dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-tomcat/artifactIdversion2.3.4.RELEASE/versionscopecompile/scope /dependency2自动配好SpringMVC引入SpringMVC全套组件自动配好SpringMVC常用组件功能3自动配好Web常见功能如字符编码问题SpringBoot帮我们配置好了所有web开发的常见场景4默认的包结构主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来无需以前的包扫描配置想要改变扫描路径SpringBootApplication(scanBasePackagescom.atguigu)或者ComponentScan 指定扫描路径SpringBootApplication 等同于 SpringBootConfiguration EnableAutoConfiguration ComponentScan(com.atguigu.boot)5各种配置拥有默认值默认配置最终都是映射到某个类上如MultipartProperties配置文件的值最终会绑定每个类上这个类会在容器中创建对象6按需加载所有自动配置项非常多的starter引入了哪些场景这个场景的自动配置才会开启SpringBoot所有的自动配置功能都在 spring-boot-autoconfigure 包里面3、容器功能3.1 组件添加ConfigurationFull模式与Lite模式配置类组件之间无依赖关系用Lite模式加速容器启动过程减少判断配置类组件之间有依赖关系方法会被调用得到之前单实例组件用Full模式#############################Configuration使用示例###################################################### /*** 1、配置类里面使用Bean标注在方法上给容器注册组件默认也是单实例的* 2、配置类本身也是组件* 3、proxyBeanMethods代理bean的方法* Full(proxyBeanMethods true)、【保证每个Bean方法被调用多少次返回的组件都是单实例的】* Lite(proxyBeanMethods false)【每个Bean方法被调用多少次返回的组件都是新创建的】* 组件依赖必须使用Full模式默认。其他默认是Lite模式****/ Configuration(proxyBeanMethods false) //告诉SpringBoot这是一个配置类 配置文件 public class MyConfig {/*** Full:外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象* return*/Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值就是组件在容器中的实例public User user01(){User zhangsan new User(zhangsan, 18);//user组件依赖了Pet组件zhangsan.setPet(tomcatPet());return zhangsan;}Bean(tom)public Pet tomcatPet(){return new Pet(tomcat);} }################################Configuration测试代码如下######################################## SpringBootConfiguration EnableAutoConfiguration ComponentScan(com.atguigu.boot) public class MainApplication {public static void main(String[] args) {//1、返回我们IOC容器ConfigurableApplicationContext run SpringApplication.run(MainApplication.class, args);//2、查看容器里面的组件String[] names run.getBeanDefinitionNames();for (String name : names) {System.out.println(name);}//3、从容器中获取组件Pet tom01 run.getBean(tom, Pet.class);Pet tom02 run.getBean(tom, Pet.class);System.out.println(组件(tom01 tom02));//4、com.atguigu.boot.config.MyConfig$$EnhancerBySpringCGLIB$$51f1e1ca1654a892MyConfig bean run.getBean(MyConfig.class);System.out.println(bean);//如果Configuration(proxyBeanMethods true)代理对象调用方法。SpringBoot总会检查这个组件是否在容器中有。//保持组件单实例User user bean.user01();User user1 bean.user01();System.out.println(user user1);User user01 run.getBean(user01, User.class);Pet tom run.getBean(tom, Pet.class);System.out.println(用户的宠物(user01.getPet() tom));} }Bean、Component、Controller、Service、RepositoryComponentScan、Import * Import({User.class, DBHelper.class})* 给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名*/Import({User.class, DBHelper.class}) Configuration(proxyBeanMethods false) //告诉SpringBoot这是一个配置类 配置文件 public class MyConfig { }ConditionalConditional派生注解Spring注解版原生的Conditional作用作用必须是Conditional指定的条件成立才给容器中添加组件配置配里面的所有内容才生效Conditional扩展注解作用判断是否满足当前指定条件ConditionalOnJava系统的java版本是否符合要求ConditionalOnBean容器中存在指定BeanConditionalOnMissingBean容器中不存在指定BeanConditionalOnExpression满足SpEL表达式指定ConditionalOnClass系统中有指定的类ConditionalOnBean容器中存在指定BeanConditionalOnMissingBean容器中不存在指定BeanConditionalOnExpression满足SpEL表达式指定ConditionalOnClass系统中有指定的类ConditionalOnBean容器中存在指定BeanConditionalOnMissingBean容器中不存在指定Bean测试条件装配 Configuration(proxyBeanMethods false) //告诉SpringBoot这是一个配置类 配置文件 //ConditionalOnBean(name tom) ConditionalOnMissingBean(name tom) public class MyConfig {Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值就是组件在容器中的实例public User user01(){User zhangsan new User(zhangsan, 18);//user组件依赖了Pet组件zhangsan.setPet(tomcatPet());return zhangsan;}Bean(tom22)public Pet tomcatPet(){return new Pet(tomcat);} }public static void main(String[] args) {//1、返回我们IOC容器ConfigurableApplicationContext run SpringApplication.run(MainApplication.class, args);//2、查看容器里面的组件String[] names run.getBeanDefinitionNames();for (String name : names) {System.out.println(name);}boolean tom run.containsBean(tom);System.out.println(容器中Tom组件tom);boolean user01 run.containsBean(user01);System.out.println(容器中user01组件user01);boolean tom22 run.containsBean(tom22);System.out.println(容器中tom22组件tom22);}自动配置类必须在一定的条件下才能生效我们怎么知道哪些自动配置类生效我们可以通过启用 debugtrue属性来让控制台打印自动配置报告这样我们就可以很方便的知道哪些自动配置类生效 AUTO-CONFIGURATION REPORT Positive matches:自动配置类启用的 -----------------DispatcherServletAutoConfiguration matched:- ConditionalOnClass found required class org.springframework.web.servlet.DispatcherServlet; ConditionalOnMissingClass did not find unwanted class (OnClassCondition)- ConditionalOnWebApplication (required) found StandardServletEnvironment (OnWebApplicationCondition)Negative matches:没有启动没有匹配成功的自动配置类 -----------------ActiveMQAutoConfiguration:Did not match:- ConditionalOnClass did not find required classes javax.jms.ConnectionFactory, org.apache.activemq.ActiveMQConnectionFactory (OnClassCondition)AopAutoConfiguration:Did not match:- ConditionalOnClass did not find required classes org.aspectj.lang.annotation.Aspect, org.aspectj.lang.reflect.Advice (OnClassCondition)3.2 原生配置文件引入ImportResourcebeans.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsdbean idhaha classcom.atguigu.boot.bean.Userproperty namename valuezhangsan/propertyproperty nameage value18/property/beanbean idhehe classcom.atguigu.boot.bean.Petproperty namename valuetomcat/property/bean /beansImportResource(classpath:beans.xml) Configuration public class MyConfig {}测试boolean haha run.containsBean(haha);boolean hehe run.containsBean(hehe);System.out.println(hahahaha);//trueSystem.out.println(hehehehe);//true3.3 配置绑定 如何使用Java读取到properties文件中的内容并且把它封装到JavaBean中以供随时使用public class getProperties {public static void main(String[] args) throws FileNotFoundException, IOException {Properties pps new Properties();pps.load(new FileInputStream(a.properties));Enumeration enum1 pps.propertyNames();//得到配置文件的名字while(enum1.hasMoreElements()) {String strKey (String) enum1.nextElement();String strValue pps.getProperty(strKey);System.out.println(strKey strValue);//封装到JavaBean。}}}Component ConfigurationProperties/*** 只有在容器中的组件才会拥有SpringBoot提供的强大功能*/ Component ConfigurationProperties(prefix mycar) public class Car {private String brand;private Integer price;public String getBrand() {return brand;}public void setBrand(String brand) {this.brand brand;}public Integer getPrice() {return price;}public void setPrice(Integer price) {this.price price;}Overridepublic String toString() {return Car{ brand brand \ , price price };} }EnableConfigurationProperties ConfigurationProperties配置类 EnableConfigurationProperties(Car.class) // 开启 Car 的配置绑定功能开启后 Car 类中就可以不用写 Component Configuration public class MyConfiguration { }Car类 ConfigurationProperties(prefix mycar) public class Car { }4、自动配置原理入门4.1 引导加载自动配置类SpringBootApplication标注在某个类上说明这个类是SpringBoot的主配置类SpringBoot就应该运行这个类的main方法来启动SpringBoot应用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 { }1SpringBootConfigurationSpring Boot的配置类标注在某个类上表示这是一个Spring Boot的配置类​ Configuration:配置类上来标注这个注解​ 配置类 ----- 配置文件配置类也是容器中的一个组件Component2EnableAutoConfiguration开启自动配置功能​ 以前我们需要配置的东西Spring Boot帮我们自动配置EnableAutoConfiguration告诉SpringBoot开启自动配置功能这样自动配置才能生效AutoConfigurationPackage Import(EnableAutoConfigurationImportSelector.class) public interface EnableAutoConfiguration { }​AutoConfigurationPackage自动配置包将主配置类SpringBootApplication标注的类的所在包及下面所有子包里面的所有组件扫描到Spring容器Import(AutoConfigurationPackages.Registrar.class) //给容器中导入一个组件 public interface AutoConfigurationPackage { } // 利用Registrar给容器中导入一系列组件将指定的一个包下的所有组件导入进来Import(EnableAutoConfigurationImportSelector.class)可以查看selectImports()方法的内容利用getAutoConfigurationEntry(annotationMetadata);给容器中批量导入一些组件调用ListString configurations getCandidateConfigurations(annotationMetadata, attributes)获取到所有需要导入到容器中的配置类SpringFactoriesLoader.loadFactoryNames()扫描所有jar包类路径下 META-INF/spring.factories把扫描到的这些文件的内容包装成properties对象从properties中获取到EnableAutoConfiguration.class类类名对应的值然后把他们添加在容器中将类路径下 META-INF/spring.factories 里面配置的所有EnableAutoConfiguration的值加入到了容器中文件里面写死了spring-boot一启动就要给容器中加载的所有配置类 spring-boot-autoconfigure-2.3.4.RELEASE.jar/META-INF/spring.factories # Auto Configure org.springframework.boot.autoconfigure.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.context.ConfigurationPropertiesAutoConfiguration,\ org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration,\ 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.ElasticsearchDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration,\ org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\ 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.r2dbc.R2dbcDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.r2dbc.R2dbcTransactionManagerAutoConfiguration,\ 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.ElasticsearchRestClientAutoConfiguration,\ org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\ org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\ org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\ 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.jersey.JerseyAutoConfiguration,\ org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\ org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\ org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\ org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration,\ 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.r2dbc.R2dbcAutoConfiguration,\ org.springframework.boot.autoconfigure.rsocket.RSocketMessagingAutoConfiguration,\ org.springframework.boot.autoconfigure.rsocket.RSocketRequesterAutoConfiguration,\ org.springframework.boot.autoconfigure.rsocket.RSocketServerAutoConfiguration,\ org.springframework.boot.autoconfigure.rsocket.RSocketStrategiesAutoConfiguration,\ 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.security.rsocket.RSocketSecurityAutoConfiguration,\ org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyAutoConfiguration,\ org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\ org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\ org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration,\ org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration,\ org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration,\ org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration,\ org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\ org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,\ org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration,\ 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.ClientHttpConnectorAutoConfiguration,\ 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,\ org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration 每一个这样的 xxxAutoConfiguration类都是容器中的一个组件都加入到容器中用他们来做自动配置每一个自动配置类进行自动配置功能4.2 按需开启自动配置项虽然我们127个场景的所有自动配置启动的时候默认全部加载。xxxxAutoConfiguration按照条件装配规则Conditional最终会按需配置。以HttpEncodingAutoConfigurationHttp编码自动配置为例解释自动配置原理//表示这是一个配置类以前编写的配置文件一样也可以给容器中添加组件 Configuration(proxyBeanMethods false) //将配置文件中对应的值和HttpProperties绑定起来,并把HttpEncodingProperties加入到ioc容器中 EnableConfigurationProperties(HttpProperties.class) //Spring底层Conditional注解根据不同的条件如果满足指定的条件整个配置类里面的配置就会生效 //判断当前应用是否是web应用如果是当前配置类生效 ConditionalOnWebApplication(type ConditionalOnWebApplication.Type.SERVLET) //判断当前项目有没有这个类CharacterEncodingFilter,SpringMVC中进行乱码解决的过滤器 //如果有当前配置类生效 ConditionalOnClass(CharacterEncodingFilter.class) //判断配置文件中是否存在某个配置 spring.http.encoding.enabled如果不存在判断也是成立的 //即使我们配置文件中不配置spring.http.encoding.enabledtrue也是默认生效的 ConditionalOnProperty(prefix spring.http.encoding, value enabled, matchIfMissing true) public class HttpEncodingAutoConfiguration {//他已经和SpringBoot的配置文件映射了private final HttpProperties.Encoding properties;//只有一个有参构造器的情况下参数的值就会从容器中拿public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) {this.properties properties;}Bean //容器中没有这个组件SpringBoot添加默认的组件ConditionalOnMissingBean(CharacterEncodingFilter.class) public CharacterEncodingFilter characterEncodingFilter() {CharacterEncodingFilter filter new OrderedCharacterEncodingFilter();filter.setEncoding(this.properties.getCharset().name());filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));return filter;} ConfigurationProperties(prefix spring.http) public class HttpProperties {public static class Encoding {public static final Charset DEFAULT_CHARSET StandardCharsets.UTF_8;private Charset charset DEFAULT_CHARSET;private Boolean force;private Boolean forceRequest;private Boolean forceResponse;private MapLocale, Charset mapping;} }所有在配置文件中能配置的属性都是在xxxxProperties类中封装着配置文件能配置什么就可以参照某个功能对应的这个属性类根据当前不同的条件判断决定这个配置类是否生效一但这个配置类生效这个配置类就会给容器中添加各种组件这些组件的属性是从对应的properties类中获取的这些类里面的每一个属性又是和配置文件绑定的4.3 修改默认配置 Bean //Bean标注的方法传入了对象参数这个参数的值就会从容器中找。//容器中有这个MultipartResolver组件但是名字不为 multipartResolve//则给容器中添加该组件ConditionalOnBean(MultipartResolver.class)ConditionalOnMissingBean(name DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)public MultipartResolver multipartResolver(MultipartResolver resolver) {//SpringMVC multipartResolver。防止有些用户配置的文件上传解析器名字不符合规范// Detect if the user has created a MultipartResolver but named it incorrectlyreturn resolver;} 给容器中加入了文件上传解析器SpringBoot默认会在底层配好所有的组件但是如果用户自己配置了以用户的优先。Bean ConditionalOnMissingBean public CharacterEncodingFilter characterEncodingFilter() { }总结SpringBoot先加载所有的自动配置类 xxxxxAutoConfiguration每个自动配置类按照条件进行生效给容器中自动配置类添加组件的时候会从xxxProperties类中获取某些属性,我们可以在配置文件中指定这些属性的值xxxProperties和配置文件进行了绑定生效的配置类就会给容器中装配很多组件只要容器中有这些组件相当于这些功能就有了定制化配置用户直接自己Bean替换底层的组件用户去看这个组件是获取的配置文件什么值就去修改。xxxxxAutoConfiguration --- 组件 --- xxxxProperties里面拿值 ---- application.properties四、开发小技巧1、Lombok简化JavaBean开发 dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependencyidea中搜索安装lombok插件简化JavaBean开发 NoArgsConstructor //AllArgsConstructor Data ToString EqualsAndHashCode public class User {private String name;private Integer age;private Pet pet;public User(String name,Integer age){this.name name;this.age age;}}简化日志开发 Slf4j RestController public class HelloController {RequestMapping(/hello)public String handle01(RequestParam(name) String name){log.info(请求进来了....);return Hello, Spring Boot 2!你好name;} }2、dev-tools dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdoptionaltrue/optional/dependency项目或者页面修改以后CtrlF93、Spring Initailizr项目初始化向导选择我们需要的开发场景自动依赖引入自动创建项目结构自动编写好主配置类参考文章springboot学习笔记含源代码视频链接
文章转载自:
http://www.morning.nxkyr.cn.gov.cn.nxkyr.cn
http://www.morning.jjxxm.cn.gov.cn.jjxxm.cn
http://www.morning.bkjhx.cn.gov.cn.bkjhx.cn
http://www.morning.nzklw.cn.gov.cn.nzklw.cn
http://www.morning.ngjpt.cn.gov.cn.ngjpt.cn
http://www.morning.addai.cn.gov.cn.addai.cn
http://www.morning.msfqt.cn.gov.cn.msfqt.cn
http://www.morning.hhpkb.cn.gov.cn.hhpkb.cn
http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn
http://www.morning.qzbwmf.cn.gov.cn.qzbwmf.cn
http://www.morning.zmyhn.cn.gov.cn.zmyhn.cn
http://www.morning.qmnjn.cn.gov.cn.qmnjn.cn
http://www.morning.xbxks.cn.gov.cn.xbxks.cn
http://www.morning.rcjwl.cn.gov.cn.rcjwl.cn
http://www.morning.nmnhs.cn.gov.cn.nmnhs.cn
http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn
http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn
http://www.morning.jfxdy.cn.gov.cn.jfxdy.cn
http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn
http://www.morning.ngkgy.cn.gov.cn.ngkgy.cn
http://www.morning.xfncq.cn.gov.cn.xfncq.cn
http://www.morning.yggdq.cn.gov.cn.yggdq.cn
http://www.morning.lnrhk.cn.gov.cn.lnrhk.cn
http://www.morning.ypzsk.cn.gov.cn.ypzsk.cn
http://www.morning.jopebe.cn.gov.cn.jopebe.cn
http://www.morning.qshxh.cn.gov.cn.qshxh.cn
http://www.morning.fwblh.cn.gov.cn.fwblh.cn
http://www.morning.fcpjq.cn.gov.cn.fcpjq.cn
http://www.morning.rtkz.cn.gov.cn.rtkz.cn
http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn
http://www.morning.qbjrf.cn.gov.cn.qbjrf.cn
http://www.morning.gglhj.cn.gov.cn.gglhj.cn
http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn
http://www.morning.rtbhz.cn.gov.cn.rtbhz.cn
http://www.morning.dtmjn.cn.gov.cn.dtmjn.cn
http://www.morning.wslr.cn.gov.cn.wslr.cn
http://www.morning.ycwym.cn.gov.cn.ycwym.cn
http://www.morning.pznhn.cn.gov.cn.pznhn.cn
http://www.morning.jikuxy.com.gov.cn.jikuxy.com
http://www.morning.mpscg.cn.gov.cn.mpscg.cn
http://www.morning.kdhrf.cn.gov.cn.kdhrf.cn
http://www.morning.wprxm.cn.gov.cn.wprxm.cn
http://www.morning.fqnql.cn.gov.cn.fqnql.cn
http://www.morning.bmjfp.cn.gov.cn.bmjfp.cn
http://www.morning.jmmzt.cn.gov.cn.jmmzt.cn
http://www.morning.phjny.cn.gov.cn.phjny.cn
http://www.morning.gmgyt.cn.gov.cn.gmgyt.cn
http://www.morning.lpppg.cn.gov.cn.lpppg.cn
http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn
http://www.morning.ljqd.cn.gov.cn.ljqd.cn
http://www.morning.txysr.cn.gov.cn.txysr.cn
http://www.morning.qwbht.cn.gov.cn.qwbht.cn
http://www.morning.bwgrd.cn.gov.cn.bwgrd.cn
http://www.morning.lhygbh.com.gov.cn.lhygbh.com
http://www.morning.hpggl.cn.gov.cn.hpggl.cn
http://www.morning.jwbfj.cn.gov.cn.jwbfj.cn
http://www.morning.bwttj.cn.gov.cn.bwttj.cn
http://www.morning.pghfy.cn.gov.cn.pghfy.cn
http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn
http://www.morning.jzkqg.cn.gov.cn.jzkqg.cn
http://www.morning.dnmwl.cn.gov.cn.dnmwl.cn
http://www.morning.xsszn.cn.gov.cn.xsszn.cn
http://www.morning.mftdq.cn.gov.cn.mftdq.cn
http://www.morning.wlxfj.cn.gov.cn.wlxfj.cn
http://www.morning.wnhsw.cn.gov.cn.wnhsw.cn
http://www.morning.bssjp.cn.gov.cn.bssjp.cn
http://www.morning.rnhh.cn.gov.cn.rnhh.cn
http://www.morning.xptkl.cn.gov.cn.xptkl.cn
http://www.morning.wftrs.cn.gov.cn.wftrs.cn
http://www.morning.sxmbk.cn.gov.cn.sxmbk.cn
http://www.morning.zknxh.cn.gov.cn.zknxh.cn
http://www.morning.lwcqh.cn.gov.cn.lwcqh.cn
http://www.morning.rrbhy.cn.gov.cn.rrbhy.cn
http://www.morning.rlnm.cn.gov.cn.rlnm.cn
http://www.morning.pqhgn.cn.gov.cn.pqhgn.cn
http://www.morning.zwtp.cn.gov.cn.zwtp.cn
http://www.morning.gkdhf.cn.gov.cn.gkdhf.cn
http://www.morning.pqnpd.cn.gov.cn.pqnpd.cn
http://www.morning.gbfzy.cn.gov.cn.gbfzy.cn
http://www.morning.nbqwt.cn.gov.cn.nbqwt.cn
http://www.tj-hxxt.cn/news/265882.html

相关文章:

  • 秒收的网站网页链接制作生成二维码
  • 广东网站建设微信网站定制免费创造网站
  • 建设部网站公示钦州公租房摇号查询wordpress编辑器格式
  • 宣威做网站推广的公司电商主页设计
  • 手机网站html5模版建了个网站百度上会有么
  • 内蒙古网站seo优化空投注册送币网站怎么做
  • 全国网站制作公司康体设备网站建设
  • 资料网站怎么做婚庆公司取名大全集
  • 网站域名怎么看西安做网站费用
  • 网站建设的可行性要求自己的公众号
  • 重庆高端品牌网站建设芜湖网站建设芜湖狼道
  • 如何建立电子商务网站手机网站建设论文
  • 做网站比较好的个人简历自我评价
  • 做网站考什么赚钱杭州网站设计推荐柚米
  • 网站验收珠海网站建设小程序
  • 网站点击软件排名常州微网站
  • 网站源码下载网站百度海外广告运营
  • 别人是怎么建设网站的昆明二级站seo整站优化排名
  • 网站改版新闻稿小米路由器3 wordpress
  • 网站制作答辩ppt怎么做张家界seo优化
  • 开网站做备案需要什么资料百度贴吧怎么做推广
  • 海口网站开发制作Wordpress主题上传PHP值
  • 英文网站模板源代码免费发布信息平台网
  • 中国民营企业500强seo诊断专家
  • 个人网站的网页寮步网站建设
  • 做网站要注册公司么惠州网站关键词排名
  • 网站建设提供排名鄂州网站制作人才招聘
  • 好的网站建设哪家好吉首企业自助建站
  • 备案网站首页地址做新得网站可以换到原来得域名嘛
  • 怎么使用网站模板网站副标题怎么写