零基础使用coze-loop:AI帮你一键提升代码可读性

核心内容摘要

Java毕设选题推荐:基于SpringBoot+Vue的会议室预基于springboot的面向企业用户的复合型活动基地,活动场地,会议室预订系统【附源码、mysql、文档、调试+代码讲解+全bao等】
RMBG-2.0在运维自动化中的应用:服务器监控截图智能处理

计算机毕业设计之基于Android的健身信息管理系统

自定义starter相当于自己根据配置信息生成了一个默认的bean导入依赖?xml version

0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/

4.

0xmlns:xsihttp://www.w

org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/

4.

0 https://maven.apache.org/xsd/maven-

4.

0.

xsdmodelVersion

4.

0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion

3.

10/versionrelativePath/!-- lookup parent from repository --/parentgroupIdcom.cj/groupIdartifactIdhello-spring-boot-starter/artifactIdversion

0.

1-SNAPSHOT/versionnamehello-spring-boot-starter/namedescriptionhello-spring-boot-starter/descriptionurl/licenseslicense//licensesdevelopersdeveloper//developersscmconnection/developerConnection/tag/url//scmpropertiesjava.version17/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependency/dependenciesbuildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdconfigurationannotationProcessorPathspathgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactId/pathpathgroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/path/annotationProcessorPaths/configuration/plugin/plugins/build/project将springboot启动类测试类删掉将maven打包成jar的配置删掉

自定义一个配置类来定义配置packagecom.cj.hellospringbootstarter;importlombok.Data;importorg.springframework.boot.context.properties.ConfigurationProperties;ConfigurationProperties(prefixmytest)DatapublicclassHelloProperties{privateStringid;privateStringname;privateStringaddress;}通过这个配置加载一个默认的 BeanDataNoArgsConstructorpublicclassHelloService{privateStringname;privateStringaddress;publicHelloService(Stringname,Stringaddress){this.namename;this.addressaddress;}}ConfigurationComponentScanEnableConfigurationProperties(HelloProperties.class)publicclassHelloServiceAutoConfiguration{AutowiredprivateHelloPropertieshelloProperties;BeanpublicHelloServicehelloService(){returnnewHelloService(helloProperties.getName(),helloProperties.getAddress());}}容器中就有这个默认的 HelloService的bean设置自动装配springboot3是在META-INF/spring文件夹下创建org.springframework.boot.autoconfigure.AutoConfiguration.imports这个文件填入自动配置类的全限定类名springboot2是在META-INF下面创建spring.factories打包使用dependencygroupIdcom.cj/groupIdartifactIdhello-spring-boot-starter/artifactIdversion

0.

1-SNAPSHOT/version/dependencyRestControllerLogpublicclassHelloController{AutowiredHelloServicehelloService;GetMapping(/hello)publicHelloServicegetHelloService(){returnhelloService;}}自定义一个注解写在starter里面动态的增强被标注这个注解的类可以用spring中BeanPostProcessor: spring完成 实例化配置 在初始化bean的前后可以实现一些自定义的配置有两个方法postProcessBeforeInitialization bean初始化之前调用postProcessAfterInitialization: bean初始化之后调用如果想增强指定的注解标注的类就可以写在BeanPostProcessor中拦截指定注解增强Target(ElementType.TYPE)Retention(RetentionPolicy.RUNTIME)publicinterfaceLog{/** * 是否打印参数 * return */booleanprintArgs()defaulttrue;booleanprintResult()defaulttrue;/** * 是否打印方法耗时 * return */booleanprintCostTime()defaulttrue;}packagecom.cj.hellospringbootstarter.annotation;importorg.springframework.beans.BeansException;importorg.springframework.beans.factory.config.BeanPostProcessor;importorg.springframework.cglib.proxy.Callback;importorg.springframework.cglib.proxy.Enhancer;importorg.springframework.cglib.proxy.MethodInterceptor;importorg.springframework.cglib.proxy.MethodProxy;importorg.springframework.stereotype.Component;importjava.lang.reflect.Method;importjava.util.Arrays;/** * 对Log 标注的 bean进行统一的 增强 * * * BeanPostProcessor spring完成实例化配置初始化一个bean之后可以实现一些自定义的逻辑 * - postProcessBeforeInitialization bean初始化之前调用 * - postProcessAfterInitialization bean初始化之后调用 */ComponentpublicclassLogBeanPostProcessorimplementsBeanPostProcessor{OverridepublicObjectpostProcessAfterInitialization(Objectbean,StringbeanName)throwsBeansException{Class?beanClassgetTargetClass(bean);// 是否标记了 Log注解if(beanClass.isAnnotationPresent(Log.class)){LoglogAnnotationbeanClass.getAnnotation(Log.class);ObjectenhanceBeancreateLogEnhanceProxy(bean,beanClass,logAnnotation);returnenhanceBean;}returnbean;}privateObjectcreateLogEnhanceProxy(ObjecttargetBean,Class?targetClass,LoglogAnnotation){EnhancerenhancernewEnhancer();enhancer.setSuperclass(targetClass);// MethodInterceptor : cglib的方法拦截器当你调任何方法的时候都会执行这个enhancer.setCallback(newMethodInterceptor(){OverridepublicObjectintercept(Objectobj,Methodmethod,Object[]args,MethodProxyproxy)throwsThrowable{// 跳过Object的基础方法if(isObjectBaseMethod(method)){returnproxy.invokeSuper(obj,args);}try{if(logAnnotation.printArgs()){System.out.printf([日志增强]%s.%s()入参%s%n,targetClass.getSimpleName(),method.getName(),Arrays.toString(args));}longstartTimeSystem.currentTimeMillis();Objectresultproxy.invokeSuper(obj,args);if(logAnnotation.printCostTime()){longendTimeSystem.currentTimeMillis();System.out.printf([日志耗时]%s.%s() 耗时%s,targetClass.getSimpleName(),method.getName(),startTime-endTime);}returnresult;}catch(Throwablee){System.out.printf(日志增强失败 %s.%s() 异常为:%s,targetClass.getSimpleName(),method.getName(),e.getMessage());throwe;}};});ObjectenhancedBeanenhancer.create();System.out.println(增强完成targetClass.getName());returnenhancedBean;}/** * 获取bean的真实类型 * param bean * return */privateClass?getTargetClass(Objectbean){Class?clazzbean.getClass();// 如果是CGLIB代理类获取父类if(clazz.getName().contains($$EnhancerByCGLIB$$)){returnclazz.getSuperclass();}returnclazz;}privatebooleanisObjectBaseMethod(Methodmethod){// 获取声明该方法的类Class?declaringClassmethod.getDeclaringClass();// method.getDeclaringClass()returndeclaringClassObject.class(method.getName().equals(toString)||method.getName().equals(hashCode)||method.getName().equals(equals));}}

skixixkino免费-skixixkino免费应用

相关标签
企业级农商对接系统管理系统源码|SpringBoot+Vue+MyBatis架构+MySQL数据库【完整版】 计算机毕业设计java基于MVC的社区党建信息系统的设计与实现 基于SpringBoot的社区党建数字化管理平台设计与实现基层党务工作信息化管理系统研发 计算机毕业设计springboot基于+vue的水果商城系统的设计与实现 基于SpringBoot与Vue的生鲜水果电商平台的设计与开发 基于SpringBoot框架的鲜果在线销售与服务系统的设计 Pi0具身智能v1创新应用:Agent技术在自动化测试中的实践 EmbeddingGemma-300m在医疗领域的创新应用:病历语义检索系统 电子工程师必看:三极管NPN与PNP的5个实战应用场景对比 Qwen3-ASR-0.6B智能硬件开发:RaspberryPi语音控制套件 为什么好心的金钱奖励,反而杀死了孩子的自律与热爱:人有两种动力:内在动机、外在动机。当强外在物质奖励,介入本身有内在动机的行为时,内在动机会被逐渐驱逐,行为最终依赖外部奖励,一旦奖励消失/不足,行为立 别再乱写正则了!一行 regex 可能让你的网站瘫痪 10 分钟 解决 ModuleNotFoundError: No module named ‘vllm._c‘ 的高效方案与避坑指南 JavaScript性能优化实战性扔 3个突破点:生物信号处理从信号采集到深度分析的实践指南 微信防撤回功能终极解决方案:3步修复DLL文件适配问题 Mac应用管理效率提升300%:Applite让Homebrew操作可视化

百度百家号客服电话人工服务

123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123