ipv6可以做网站吗电商网站更适合

张小明 2026/1/19 20:50:48
ipv6可以做网站吗,电商网站更适合,微商城分销系统多少钱,江西省建设招标网站Spring Boot starter我们知道Spring Boot大大简化了项目初始搭建以及开发过程#xff0c;而这些都是通过Spring Boot提供的starter来完成的。品达通用权限系统就是基于Spring Boot进行开发#xff0c;而且一些基础模块其本质就是starter#xff0c;所以我们需要对Spring Boo…Spring Boot starter我们知道Spring Boot大大简化了项目初始搭建以及开发过程而这些都是通过Spring Boot提供的starter来完成的。品达通用权限系统就是基于Spring Boot进行开发而且一些基础模块其本质就是starter所以我们需要对Spring Boot的starter有一个全面深入的了解这是我们开发品达通用权限系统的必备知识。1 starter介绍spring boot 在配置上相比spring要简单许多, 其核心在于spring-boot-starter, 在使用spring boot来搭建一个项目时, 只需要引入官方提供的starter, 就可以直接使用, 免去了各种配置。starter简单来讲就是引入了一些相关依赖和一些初始化的配置。Spring官方提供了很多starter第三方也可以定义starter。为了加以区分starter从名称上进行了如下规范[ ] Spring官方提供的starter名称为spring-boot-starter-xxx例如Spring官方提供的spring-boot-starter-web[ ] 第三方提供的starter名称为xxx-spring-boot-starter例如由mybatis提供的mybatis-spring-boot-starter2 starter原理Spring Boot之所以能够帮我们简化项目的搭建和开发过程主要是基于它提供的起步依赖和自动配置。2.1 起步依赖起步依赖其实就是将具备某种功能的坐标打包到一起可以简化依赖导入的过程。例如我们导入spring-boot-starter-web这个starter则和web开发相关的jar包都一起导入到项目中了。如下图所示2.2 自动配置自动配置就是无须手动配置xml自动配置并管理bean可以简化开发过程。那么Spring Boot是如何完成自动配置的呢自动配置涉及到如下几个关键步骤基于Java代码的Bean配置自动配置条件依赖Bean参数获取Bean的发现Bean的加载我们可以通过一个实际的例子mybatis-spring-boot-starter来说明自动配置的实现过程。2.2.1 基于Java代码的Bean配置当我们在项目中导入了mybatis-spring-boot-starter这个jar后可以看到它包括了很多相关的jar包如下图其中在mybatis-spring-boot-autoconfigure这个jar包中有如下一个MybatisAutoConfiguration自动配置类打开这个类截取的关键代码如下Configuration和Bean这两个注解一起使用就可以创建一个基于java代码的配置类可以用来替代传统的xml配置文件。Configuration注解的类可以看作是能生产让Spring IoC容器管理的Bean实例的工厂。Bean注解的方法返回的对象可以被注册到spring容器中。所以上面的MybatisAutoConfiguration这个类自动帮我们生成了SqlSessionFactory和SqlSessionTemplate这些Mybatis的重要实例并交给spring容器管理从而完成bean的自动注册。2.2.2 自动配置条件依赖从MybatisAutoConfiguration这个类中使用的注解可以看出要完成自动配置是有依赖条件的。所以要完成Mybatis的自动配置需要在类路径中存在SqlSessionFactory.class、SqlSessionFactoryBean.class这两个类同时需要存在DataSource这个bean且这个bean完成自动注册。这些注解是spring boot特有的常见的条件依赖注解有注解功能说明ConditionalOnBean仅在当前上下文中存在某个bean时才会实例化这个BeanConditionalOnClass某个class位于类路径上才会实例化这个BeanConditionalOnExpression当表达式为true的时候才会实例化这个BeanConditionalOnMissingBean仅在当前上下文中不存在某个bean时才会实例化这个BeanConditionalOnMissingClass某个class在类路径上不存在的时候才会实例化这个BeanConditionalOnNotWebApplication不是web应用时才会实例化这个BeanAutoConfigureAfter在某个bean完成自动配置后实例化这个beanAutoConfigureBefore在某个bean完成自动配置前实例化这个bean2.2.3 Bean参数获取要完成mybatis的自动配置需要我们在配置文件中提供数据源相关的配置参数例如数据库驱动、连接url、数据库用户名、密码等。那么spring boot是如何读取yml或者properites配置文件的的属性来创建数据源对象的在我们导入mybatis-spring-boot-starter这个jar包后会传递过来一个spring-boot-autoconfigure包在这个包中有一个自动配置类DataSourceAutoConfiguration如下所示!我们可以看到这个类上加入了EnableConfigurationProperties这个注解继续跟踪源码到DataSourceProperties这个类如下可以看到这个类上加入了ConfigurationProperties注解这个注解的作用就是把yml或者properties配置文件中的配置参数信息封装到ConfigurationProperties注解标注的bean(即DataSourceProperties)的相应属性上。EnableConfigurationProperties注解的作用是使ConfigurationProperties注解生效。2.2.4 Bean的发现spring boot默认扫描启动类所在的包下的主类与子类的所有组件但并没有包括依赖包中的类那么依赖包中的bean是如何被发现和加载的我们需要从Spring Boot项目的启动类开始跟踪在启动类上我们一般会加入SpringBootApplication注解此注解的源码如下重点介绍如下三个注解SpringBootConfiguration作用就相当于Configuration注解被注解的类将成为一个bean配置类ComponentScan作用就是自动扫描并加载符合条件的组件最终将这些bean加载到spring容器中EnableAutoConfiguration这个注解很重要借助Import的支持收集和注册依赖包中相关的bean定义继续跟踪EnableAutoConfiguration注解源码EnableAutoConfiguration注解引入了Import这个注解。Import导入需要自动配置的组件此处为EnableAutoConfigurationImportSelector这个类EnableAutoConfigurationImportSelector类源码如下EnableAutoConfigurationImportSelector继承了AutoConfigurationImportSelector类继续跟踪AutoConfigurationImportSelector类源码AutoConfigurationImportSelector类的getCandidateConfigurations方法中的调用了SpringFactoriesLoader类的loadFactoryNames方法继续跟踪源码SpringFactoriesLoader的loadFactoryNames静态方法可以从所有的jar包中读取META-INF/spring.factories文件而自动配置的类就在这个文件中进行配置spring.factories文件内容如下这样Spring Boot就可以加载到MybatisAutoConfiguration这个配置类了。2.2.5 Bean的加载在Spring Boot应用中要让一个普通类交给Spring容器管理通常有以下方法1、使用 Configuration与Bean 注解2、使用Controller Service Repository Component 注解标注该类并且启用ComponentScan自动扫描3、使用Import 方法其中Spring Boot实现自动配置使用的是Import注解这种方式AutoConfigurationImportSelector类的selectImports方法返回一组从META-INF/spring.factories文件中读取的bean的全类名这样Spring Boot就可以加载到这些Bean并完成实例的创建工作。2.3 自动配置总结我们可以将自动配置的关键几步以及相应的注解总结如下1、Configuration与Bean基于Java代码的bean配置2、Conditional设置自动配置条件依赖3、EnableConfigurationProperties与ConfigurationProperties读取配置文件转换为bean4、EnableAutoConfiguration与Import实现bean发现与加载Top3 自定义starter本小节我们通过自定义两个starter来加强starter的理解和应用。3.1 案例一3.1.1 开发starter第一步创建starter工程hello-spring-boot-starter并配置pom.xml文件?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version2.2.2.RELEASE/version relativePath/ /parent groupIdcn.pf/groupId artifactIdhello-spring-boot-starter/artifactId version1.0-SNAPSHOT/version dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-autoconfigure/artifactId /dependency /dependencies /project第二步创建配置属性类HelloPropertiespackage cn.pf.config; import org.springframework.boot.context.properties.ConfigurationProperties; /* *读取配置文件转换为bean * */ ConfigurationProperties(prefix hello) public class HelloProperties { private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name name; } public String getAddress() { return address; } public void setAddress(String address) { this.address address; } Override public String toString() { return HelloProperties{ name name \ , address address \ }; } }第三步创建服务类HelloServicepackage cn.pf.service; public class HelloService { private String name; private String address; public HelloService(String name, String address) { this.name name; this.address address; } public String sayHello(){ return 你好我的名字叫 name 我来自 address; } }第四步创建自动配置类HelloServiceAutoConfigurationpackage cn.pf.config; import cn.pf.service.HelloService; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /* * 配置类基于Java代码的bean配置 * */ Configuration EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration { private HelloProperties helloProperties; //通过构造方法注入配置属性对象HelloProperties public HelloServiceAutoConfiguration(HelloProperties helloProperties) { this.helloProperties helloProperties; } //实例化HelloService并载入Spring IoC容器 Bean ConditionalOnMissingBean public HelloService helloService(){ return new HelloService(helloProperties.getName(),helloProperties.getAddress()); } }第五步在resources目录下创建META-INF/spring.factoriesorg.springframework.boot.autoconfigure.EnableAutoConfiguration\ cn.pf.config.HelloServiceAutoConfiguration至此starter已经开发完成了可以将当前starter安装到本地maven仓库供其他应用来使用。3.1.2 使用starter第一步创建maven工程myapp并配置pom.xml文件?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version2.2.2.RELEASE/version relativePath/ /parent groupIdcn.pf/groupId artifactIdmyapp/artifactId version1.0-SNAPSHOT/version dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency !--导入自定义starter-- dependency groupIdcn.pf/groupId artifactIdhello-spring-boot-starter/artifactId version1.0-SNAPSHOT/version /dependency /dependencies /project第二步创建application.yml文件server: port: 8080 hello: name: xiaoming address: beijing第三步创建HelloControllerpackage cn.pf.controller; import cn.pf.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; RestController RequestMapping(/hello) public class HelloController { //HelloService在我们自定义的starter中已经完成了自动配置所以此处可以直接注入 Autowired private HelloService helloService; GetMapping(/say) public String sayHello(){ return helloService.sayHello(); } }第四步创建启动类HelloApplicationpackage cn.pf; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; SpringBootApplication public class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class,args); } }执行启动类main方法访问地址http://localhost:8080/hello/say3.2 案例二在前面的案例一中我们通过定义starter自动配置了一个HelloService实例。本案例我们需要通过自动配置来创建一个拦截器对象通过此拦截器对象来实现记录日志功能。我们可以在案例一的基础上继续开发案例二。3.2.1 开发starter第一步在hello-spring-boot-starter的pom.xml文件中追加如下maven坐标dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId optionaltrue/optional /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-configuration-processor/artifactId /dependency第二步自定义MyLog注解package cn.pf.log; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) public interface MyLog { /** * 方法描述 */ String desc() default ; }第三步自定义日志拦截器MyLogInterceptorpackage cn.pf.log; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.lang.reflect.Method; /** * 日志拦截器 */ public class MyLogInterceptor extends HandlerInterceptorAdapter { private static final ThreadLocalLong startTimeThreadLocal new ThreadLocal(); public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HandlerMethod handlerMethod (HandlerMethod)handler; Method method handlerMethod.getMethod();//获得被拦截的方法对象 MyLog myLog method.getAnnotation(MyLog.class);//获得方法上的注解 if(myLog ! null){ //方法上加了MyLog注解需要进行日志记录 long startTime System.currentTimeMillis(); startTimeThreadLocal.set(startTime); } return true; } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { HandlerMethod handlerMethod (HandlerMethod)handler; Method method handlerMethod.getMethod();//获得被拦截的方法对象 MyLog myLog method.getAnnotation(MyLog.class);//获得方法上的注解 if(myLog ! null){ //方法上加了MyLog注解需要进行日志记录 long endTime System.currentTimeMillis(); Long startTime startTimeThreadLocal.get(); long optTime endTime - startTime; String requestUri request.getRequestURI(); String methodName method.getDeclaringClass().getName() . method.getName(); String methodDesc myLog.desc(); System.out.println(请求uri requestUri); System.out.println(请求方法名 methodName); System.out.println(方法描述 methodDesc); System.out.println(方法执行时间 optTime ms); } } }第四步创建自动配置类MyLogAutoConfiguration用于自动配置拦截器、参数解析器等web组件package cn.pf.config; import cn.pf.log.MyLogInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * 配置类用于自动配置拦截器、参数解析器等web组件 */ Configuration public class MyLogAutoConfiguration implements WebMvcConfigurer{ //注册自定义日志拦截器 public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyLogInterceptor()); } }第五步在spring.factories中追加MyLogAutoConfiguration配置org.springframework.boot.autoconfigure.EnableAutoConfiguration\ cn.pf.config.HelloServiceAutoConfiguration,\ cn.pf.config.MyLogAutoConfiguration注意我们在hello-spring-boot-starter中追加了新的内容需要重新打包安装到maven仓库。3.2.2 使用starter在myapp工程的Controller方法上加入MyLog注解package cn.pf.controller; import cn.pf.log.MyLog; import cn.pf.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; RestController RequestMapping(/hello) public class HelloController { //HelloService在我们自定义的starter中已经完成了自动配置所以此处可以直接注入 Autowired private HelloService helloService; MyLog(desc sayHello方法) //日志记录注解 GetMapping(/say) public String sayHello(){ return helloService.sayHello(); } }访问地址http://localhost:8080/hello/say查看控制台输出请求uri/hello/say 请求方法名cn.pf.controller.HelloController.sayHello 方法描述sayHello方法 方法执行时间36ms
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

网站输入卡密提取怎么做软件工程师是做什么的

目录已开发项目效果实现截图开发技术介绍系统开发工具:核心代码参考示例1.建立用户稀疏矩阵,用于用户相似度计算【相似度矩阵】2.计算目标用户与其他用户的相似度系统测试总结源码文档获取/同行可拿货,招校园代理 :文章底部获取博主联系方式&…

张小明 2026/1/17 17:35:46 网站建设

青岛 两学一做 网站永兴集团网站

RTL8852BE无线网卡驱动:让Linux连接更稳定的终极方案 【免费下载链接】rtl8852be Realtek Linux WLAN Driver for RTL8852BE 项目地址: https://gitcode.com/gh_mirrors/rt/rtl8852be 还在为Linux系统下Realtek RTL8852BE无线网卡频繁断线而烦恼吗&#xff1…

张小明 2026/1/17 17:35:47 网站建设

福州网络营销网站宇说建筑网站

AD9361是一款高度集成的射频(RF)收发器,能够针对各种应用进行配置。这些设备集成了在单个设备中提供所有收发器功能所需的所有RF,混合信号和数字模块。可编程性使该宽带收发器适用于多种通信标准,包括频分双工(FDD)和时分双工(TDD)系统。这种可编程性还允许使用单个12位并行数据…

张小明 2026/1/17 17:35:47 网站建设

网站图片属性是什么网站开发方法 优帮云

第一章:Open-AutoGLM 微信集成仅需3小时?真实项目落地经验全公开在某初创企业智能客服系统升级项目中,团队成功将 Open-AutoGLM 模型接入微信公众号后台,端到端开发与部署耗时仅 3 小时。这一效率得益于模块化设计、清晰的 API 文…

张小明 2026/1/17 14:20:14 网站建设

网站建设支付接口crm在线演示

如何用AI智能筛选技术革新文献管理工作流 【免费下载链接】zotero-gpt GPT Meet Zotero. 项目地址: https://gitcode.com/gh_mirrors/zo/zotero-gpt 你是否曾面对数百篇待读论文感到无从下手?当研究领域不断拓展,传统的手动文献筛选方法已难以应对…

张小明 2026/1/17 17:35:49 网站建设

win2003 iis配置网站门户网站建设哪家便宜

在当今数字时代,字体选择已经成为网页设计和用户体验的关键因素。你是否曾经为寻找既美观又实用的字体而苦恼?Barlow字体正是为满足这一需求而生,它提供了一个包含54种样式的完整字体家族,从超细到超粗,满足各种设计场…

张小明 2026/1/17 17:35:51 网站建设