原生JDBC--使用-简述

使用JDBC链接数据库

简述开发链接步骤

  • 一 通过java反射 Class.forName()加载驱动
  • 二 通过DriverManager创建链接connection(提供数据库URL,user,password)
  • 三 通过链接connection创建实例
  • 四 执行sql,获取结果集ResultSet
  • 五 通过结果集获取数据
  • 六 资源回收,关闭结果集,实例,链接。

缺点:
jdbc底层没有连接池,操作数据库需要频繁的创建和关联链接,需要耗费大量的资源,在java中项目一旦需要修改sql,就需要整体编译不利于维护。
返回的结果集也需要硬编码。


注意事项:在操作中可能出现sql注入的问题

sql注入:在应用程序中通过插入恶意sql篡改数据或获取未经授权查询的数据,是非常大的安全漏洞。

解决方法:

注:代码示例见文章末尾
PreparedStatement statement = connection.prepareStatement(sql);

为什么能解决sql注入?

因为PreparedStatement对sql进行了预编译,对参数位置使用了占位符,只需要填充即可,而Statement只是对sql进行拼接,并且每次都需要编译,效率低下。

代码示例

代码编写之前需要引入jar

		<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

以下为实现代码

package com.chenmo.study;

import java.sql.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 父依赖:Spring Boot 3.3.1 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.1</version> <relativePath/> <!-- 从仓库获取父依赖 --> </parent> <!-- 项目基本信息 --> <groupId>com.example</groupId> <artifactId>main</artifactId> <version>0.0.1</version> <name>main</name> <description>为Spring Cloud Alibaba升级铺垫的主服务</description> <!-- 版本管理(统一控制依赖版本,避免冲突) --> <properties> <java.version>17</java.version> <encoding>UTF-8</encoding> <project.build.sourceEncoding>${encoding}</project.build.sourceEncoding> <project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding> <maven.compiler.encoding>${encoding}</maven.compiler.encoding> <!-- Spring Cloud Alibaba 版本(适配Spring Boot 3.3.x) --> <spring-cloud-alibaba.version>2023.0.1.0</spring-cloud-alibaba.version> <!-- Spring Cloud 版本(与Alibaba配套) --> <spring-cloud.version>2023.0.2</spring-cloud.version> <!-- 第三方工具版本 --> <mybatis-plus.version>3.5.7</mybatis-plus.version> <fastjson2.version>2.0.52</fastjson2.version> <modelmapper.version>3.2.1</modelmapper.version> <jjwt.version>0.11.5</jjwt.version> <redisson.version>3.50.0</redisson.version> <hutool.version>5.8.32</hutool.version> <!-- 统一hutool版本 --> <poi.version>5.4.0</poi.version> <druid.version>1.2.13</druid.version> <minio.version>8.5.12</minio.version> <taos-jdbcdriver.version>3.5.3</taos-jdbcdriver.version> <xxl-job.version>2.4.1</xxl-job.version> <activiti.version>7.1.0.M6</activiti.version> <springdoc.version>2.1.0</springdoc.version> <!-- <powerjob.version>5.1.1</powerjob.version>--> <californium.version>3.5.0</californium.version> <wechatpay-sdk.version>0.6.0</wechatpay-sdk.version> <alipay-sdk.version>4.34.0.ALL</alipay-sdk.version> <volc-sdk.version>LATEST</volc-sdk.version> </properties> <!-- 依赖管理(控制Spring Cloud Alibaba及相关组件版本) --> <dependencyManagement> <dependencies> <!-- Spring Cloud Alibaba 核心依赖管理 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Spring Cloud 核心依赖管理 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>jakarta.annotation</groupId> <artifactId>jakarta.annotation-api</artifactId> <version>2.1.1</version> </dependency> <!-- 一、Spring Boot 核心 Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- 排除 Tomcat 的旧版注解 API --> <exclusion> <groupId>org.apache.tomcat</groupId> <artifactId>annotations-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <!-- 服务监控,Alibaba生态必备 --> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 二、Spring Cloud Alibaba 核心组件(为升级铺垫) --> <!-- 服务注册与发现(Nacos) --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- 声明式服务调用(OpenFeign) --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- 熔断限流(Sentinel,已存在,保留) --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!-- 消息队列(RocketMQ,与Alibaba生态兼容) --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-stream-rocketmq</artifactId> <!-- 替换原生rocketmq-starter,适配Alibaba --> </dependency> <!-- 三、安全与认证 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-authorization-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <!-- JWT工具 --> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-api</artifactId> <version>${jjwt.version}</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-impl</artifactId> <version>${jjwt.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-jackson</artifactId> <version>${jjwt.version}</version> <scope>runtime</scope> </dependency> <!-- 四、数据访问 --> <!-- MySQL驱动 --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <!-- MongoDB --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <!-- Redis(Redisson作为客户端,已排除原生Redis starter避免冲突) --> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>${redisson.version}</version> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </exclusion> </exclusions> </dependency> <!-- MyBatis-Plus(适配Spring Boot 3) --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>${mybatis-plus.version}</version> </dependency> <!-- 数据库连接池(Druid) --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <!-- 五、消息与通信 --> <!-- WebSocket --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <!-- Kafka(如需保留,与RocketMQ并存需注意配置隔离) --> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka-test</artifactId> <scope>test</scope> </dependency> <!-- CoAP协议 --> <dependency> <groupId>org.eclipse.californium</groupId> <artifactId>californium-core</artifactId> <version>${californium.version}</version> </dependency> <!-- 六、工具类 --> <!-- JSON处理(FastJSON2) --> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>${fastjson2.version}</version> </dependency> <!-- 对象转换(ModelMapper) --> <dependency> <groupId>org.modelmapper</groupId> <artifactId>modelmapper</artifactId> <version>${modelmapper.version}</version> </dependency> <!-- Hutool工具集(已包含captcha,移除单独的hutool-captcha) --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>${hutool.version}</version> </dependency> <!-- Excel处理(POI) --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${poi.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${poi.version}</version> </dependency> <!-- Lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- 七、业务组件 --> <!-- 文件存储(MinIO) --> <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>${minio.version}</version> </dependency> <!-- 时序数据库(TAOS) --> <dependency> <groupId>com.taosdata.jdbc</groupId> <artifactId>taos-jdbcdriver</artifactId> <version>${taos-jdbcdriver.version}</version> </dependency> <!-- 任务调度(XXL-Job) --> <dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>${xxl-job.version}</version> </dependency> <!-- 工作流(Activiti) --> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring-boot-starter</artifactId> <version>${activiti.version}</version> </dependency> <dependency> <groupId>org.activiti.core.common</groupId> <artifactId>activiti-spring-security</artifactId> <version>${activiti.version}</version> </dependency> <!-- 分布式任务调度(PowerJob) --> <!-- <dependency>--> <!-- <groupId>com.github.kfcfans</groupId>--> <!-- <artifactId>powerjob-client</artifactId>--> <!-- <version>${powerjob.version}</version>--> <!-- </dependency>--> <!-- 八、支付相关 --> <dependency> <groupId>com.github.wechatpay-apiv3</groupId> <artifactId>wechatpay-apache-httpclient</artifactId> <version>${wechatpay-sdk.version}</version> </dependency> <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>${alipay-sdk.version}</version> </dependency> <!-- 九、API文档(Swagger) --> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>${springdoc.version}</version> </dependency> <!-- 十、火山引擎SDK --> <dependency> <groupId>com.volcengine</groupId> <artifactId>volcengine-java-sdk-ark-runtime</artifactId> <version>${volc-sdk.version}</version> </dependency> <dependency> <groupId>com.volcengine</groupId> <artifactId>volc-sdk-java</artifactId> <version>${volc-sdk.version}</version> </dependency> </dependencies> <!-- 仓库配置(优先使用阿里云仓库加速) --> <repositories> <repository> <id>aliyun</id> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> <snapshots> <enabled>false</enabled> <!-- 关闭快照版本下载 --> </snapshots> </repository> <!-- Spring Cloud Alibaba 专属仓库(确保依赖下载) --> <repository> <id>spring-cloud-alibaba</id> <name>Spring Cloud Alibaba Repository</name> <url>https://maven.aliyun.com/repository/spring</url> </repository> </repositories> <!-- 构建配置 --> <build> <plugins> <!-- Spring Boot 打包插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> <!-- 多环境配置 --> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <spring-boot.run.profiles>dev</spring-boot.run.profiles> </properties> </profile> <profile> <id>prod</id> <properties> <spring-boot.run.profiles>prod</spring-boot.run.profiles> </properties> </profile> </profiles> </configuration> </plugin> <!-- 编译插件(指定Java版本) --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${encoding}</encoding> </configuration> </plugin> </plugins> <!-- 资源文件配置 --> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.yml</include> <!-- 补充yml文件支持 --> </includes> <filtering>false</filtering> <!-- 不启用过滤 --> </resource> <!-- 多环境配置文件过滤 --> <resource> <directory>src/main/resources</directory> <includes> <include>application.properties</include> <include>application-${profiles.active}.properties</include> <include>application-${profiles.active}.yml</include> </includes> <filtering>true</filtering> <!-- 启用变量替换 --> </resource> </resources> </build> <!-- 多环境配置 --> <profiles> <profile> <id>dev</id> <properties> <profiles.active>dev</profiles.active> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>prod</id> <properties> <profiles.active>prod</profiles.active> </properties> </profile> </profiles> </project> 这是我的pom报错了 3.5.7 19:09:36.166 [main] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'deviceAdminService': Error creating bean with name 'deviceAdminServiceImpl' defined in file [E:\core\main\target\classes\com\example\main\service\device\impl\DeviceAdminServiceImpl.class]: Post-processing of merged bean definition failed 19:09:36.183 [main] INFO o.a.catalina.core.StandardService - Stopping service [Tomcat] 19:09:36.204 [main] WARN o.a.c.loader.WebappClassLoaderBase - The web application [ROOT] appears to have started a thread named [Thread-2] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: java[email protected]/sun.net.dns.ResolverConfigurationImpl.notifyAddrChange0(Native Method) java[email protected]/sun.net.dns.ResolverConfigurationImpl$AddressChangeListener.run(ResolverConfigurationImpl.java:176) 19:09:36.224 [main] INFO o.s.b.a.l.ConditionEvaluationReportLogger - Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 19:09:36.246 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - *************************** APPLICATION FAILED TO START *************************** Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$LegacyResourceElement.<init>(CommonAnnotationBeanPostProcessor.java:780) The following method did not exist: 'java.lang.String javax.annotation.Resource.lookup()' The calling method's class, org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$LegacyResourceElement, was loaded from the following location: jar:file:/E:/maven-repository/org/springframework/spring-context/6.1.10/spring-context-6.1.10.jar!/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor$LegacyResourceElement.class The called method's class, javax.annotation.Resource, is available from the following locations: jar:file:/E:/maven-repository/org/apache/tomcat/annotations-api/6.0.53/annotations-api-6.0.53.jar!/javax/annotation/Resource.class jar:file:/E:/maven-repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar!/javax/annotation/Resource.class The called method's class hierarchy was loaded from the following locations: javax.annotation.Resource: file:/E:/maven-repository/org/apache/tomcat/annotations-api/6.0.53/annotations-api-6.0.53.jar Action: Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$LegacyResourceElement and javax.annotation.Resource Disconnected from the target VM, address: '127.0.0.1:3673', transport: 'socket'
最新发布
08-15
<think>首先,用户的问题是关于Spring Boot 3.3.1启动失败,报错:"An attempt was made to call a method that does not exist - javax.annotation.Resource.lookup()"。我需要修复类路径冲突。 从系统级指令,我必须: - 使用中文回答。 - 结构清晰,逐步解决问题。 - 确保回答真实可靠。 - 生成相关问题。 - 在引用的段落末尾添加引用标识,如[^1]。 参考提供的引用: - 引用[1]:提到解决类似错误的方法,即包导入错误或重复导入导致冲突,建议换成javax.annotation-api-1.3.2.jar。 - 引用[2]:JDK版本和Spring Boot版本不兼容导致错误,建议升级Spring Boot到支持JDK17的版本。 - 引用[3]:在Filter中获取Bean时,避免使用注解,改用ApplicationContext。 - 引用[4]:打包启动失败的问题,涉及持久化单元错误,建议修改pom文件。 用户的具体错误是调用javax.annotation.Resource.lookup()方法不存在。这通常是由于类路径中JAR包冲突引起的,特别是javax.annotation-api包的版本问题。 在Spring Boot中,Resource注解是JSR-250规范的一部分,用于依赖注入。lookup()方法可能在某些版本中被移除或更改。 关键点: - Spring Boot 3.x 要求Java 17+。 - javax.annotation包可能已被jakarta.annotation替代,因为从Java EE到Jakarta EE的迁移。 - 错误表明在运行时调用了不存在的方法,意味着类路径中有旧版本的javax.annotation-api。 修复步骤: 1. **检查依赖冲突**:使用Maven或Gradle检查依赖树,找出冲突的JAR包。 2. **更新或排除依赖**:确保使用正确的javax.annotation-api版本,或排除旧版本。 3. **考虑Jakarta迁移**:Spring Boot 3.x 可能使用jakarta.annotation。 从引用[1],它建议换成javax.annotation-api-1.3.2.jar。引用[2]提到JDK版本兼容性,用户使用的是Spring Boot 3.3.1,应该支持JDK17,但可能依赖问题。 用户说“Spring Boot 3.3.1”,所以版本是新的。 可能的解决方案: - 添加或更新javax.annotation-api依赖。 - 排除冲突的依赖。 - 确保没有重复的JAR包。 在Maven项目中,可以: - 添加依赖:`<dependency><groupId>javax.annotation</groupId><artifactId>javax.annotation-api</artifactId><version>1.3.2</version></dependency>` - 或者,如果使用Jakarta,用`<dependency><groupId>jakarta.annotation</groupId><artifactId>jakarta.annotation-api</artifactId><version>2.1.1</version></dependency>` 在Spring Boot 3.x中,它可能默认使用Jakarta,所以需要检查项目中是否有旧版的javax.annotation。 步骤建议: 1. 确认JDK版本:确保是JDK 17+。 2. 检查依赖:运行`mvn dependency:tree`或Gradle的依赖树命令,查找javax.annotation-api。 3. 添加或更新依赖:在pom.xml中添加正确版本。 4. 排除冲突:如果有其他依赖引入旧版本,排除它。 生成相关问题:基于用户问题,生成相关的问题。 回答结构: - 先解释错误原因。 - 逐步提供修复步骤。 - 确保清晰可靠。 - 结尾生成相关问题。 添加引用:在回答中,如果引用提供的参考,添加引用标识。例如,引用[1]的建议。 我的回答: - 开头:简述错误原因。 - 步骤:详细修复步骤。 - 结尾:生成相关问题。</think>### 解决Spring Boot 3.3.1启动报错:javax.annotation.Resource.lookup()方法不存在 您遇到的错误`An attempt was made to call a method that does not exist - javax.annotation.Resource.lookup()`通常是由于类路径冲突引起的。具体来说,Spring Boot 3.x(如3.3.1)默认使用Jakarta EE规范(从Java EE迁移而来),而项目中可能残留了旧版的`javax.annotation-api`依赖。这导致JVM加载了不兼容的JAR包,其中`Resource.lookup()`方法在较新版本中已被移除或更改。错误表明运行时调用了不存在的方法,常见于依赖管理不当(如重复引入或版本冲突)。 下面我将逐步指导您修复此问题。整个过程基于Maven项目(如果是Gradle,逻辑类似),请确保您的环境满足: - JDK版本为17或更高(Spring Boot 3.x要求)。 - IDE中已配置好Maven/Gradle。 #### 修复步骤 1. **确认JDK版本** 首先检查您的JDK版本。Spring Boot 3.3.1必须运行在JDK 17+上。在终端运行: ```bash java -version ``` 如果版本低于17,请升级JDK。例如,从[Oracle JDK下载页](https://www.oracle.com/java/technologies/downloads/)安装JDK 17。 2. **检查依赖冲突** 使用Maven依赖树命令分析冲突。在项目根目录运行: ```bash mvn dependency:tree -Dincludes=javax.annotation:* ``` 或针对Jakarta(Spring Boot 3.x默认): ```bash mvn dependency:tree -Dincludes=jakarta.annotation:* ``` 查看输出,寻找`javax.annotation-api`或`jakarta.annotation-api`的多个版本。常见冲突源包括: - 旧版Spring Boot Starter(如`spring-boot-starter-web`)间接引入旧依赖。 - 第三方库(如Hibernate或JPA相关)拉入不兼容JAR。 如果发现多个版本(如1.2和1.3.2),说明存在冲突。 3. **添加或更新正确依赖** 在`pom.xml`中显式添加兼容依赖,并排除冲突版本: - **方案A:使用Jakarta EE(推荐)** Spring Boot 3.x默认使用Jakarta,添加以下依赖: ```xml <dependency> <groupId>jakarta.annotation</groupId> <artifactId>jakarta.annotation-api</artifactId> <version>2.1.1</version> <!-- 兼容Spring Boot 3.3.1 --> </dependency> ``` 同时,在可能引入旧版`javax.annotation`的依赖中排除冲突: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> </exclusion> </exclusions> </dependency> ``` - **方案B:强制使用javax.annotation(兼容旧项目)** 如果项目仍需`javax.annotation`,添加指定版本(引用[1]建议): ```xml <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> <!-- 解决lookup()缺失问题 --> </dependency> ``` 并在所有父依赖中排除旧版: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <exclusions> <exclusion> <groupId>com.sun.activation</groupId> <artifactId>javax.activation</artifactId> </exclusion> </exclusions> </dependency> ``` 4. **清理并重建项目** 修改`pom.xml`后,执行Maven清理和重新编译: ```bash mvn clean install ``` 确保IDE(如IntelliJ IDEA)也执行清理: - 点击菜单:Build > Rebuild Project。 - 删除`target`目录和IDE缓存(File > Invalidate Caches)。 5. **验证修复** 启动应用: ```bash mvn spring-boot:run ``` 如果错误消失,表示修复成功。若仍报错: - 检查是否还有其他冲突依赖(如`javax.persistence`),参考类似排除方法[^4]。 - 确保打包配置正确(如MANIFEST.MF位置),避免类加载问题[^4]。 #### 注意事项 - **版本兼容性**:Spring Boot 3.3.1应与JDK 17+和Jakarta EE 9+兼容。如果您的父工程Spring Boot版本过低(如2.x),升级到3.x以避免兼容性问题[^2]。 - **替代方案**:如果错误发生在Filter或Bean加载时,避免使用`@Resource`注解,改用`ApplicationContext`获取Bean(引用[3]方法): ```java // 在Filter中替换@Resource MyService service = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext()).getBean(MyService.class); ``` - **预防措施**:定期运行`mvn dependency:tree`检查依赖冲突,并使用`<dependencyManagement>`统一管理版本。 通过以上步骤,类路径冲突应被解决。如果问题持续,请提供完整的`pom.xml`和错误日志进一步分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值