You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring注解式简单项目中getBean(Class)方法调用报错的问题咨询

Spring注解式简单项目中getBean(Class)方法调用报错的问题咨询

看起来你遇到的这个报错挺反常的——毕竟Spring的ApplicationContext从很早版本开始就支持getBean(Class<T>)泛型方法了。结合你的场景(想做最小依赖的Spring项目,不用Spring Boot),我来帮你一步步排查解决:

一、先排查最可能的根源:依赖与JDK版本问题

这个报错提示getBean(String)不适用Class参数,大概率是因为你的项目依赖不完整、版本不兼容,或者JDK版本不符合Spring 6.x的要求。

1. 确保Maven依赖纯净且正确

你换成spring-context是对的,但要注意Spring 6.x强制要求JDK 17及以上,而且要保证依赖没有冲突。给你一个极简的pom.xml配置,包含所有必要的内容:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.nirmalya</groupId>
    <artifactId>AutoWireExample</artifactId>
    <version>1.0</version>
    <name>AutoWireExample</name>
    <packaging>jar</packaging>

    <dependencies>
        <!-- 核心Spring Context依赖,自动引入spring-core、spring-beans等基础模块 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.1.11</version>
        </dependency>
    </dependencies>

    <!-- 必须指定JDK版本为17+,否则Spring 6.x无法正常工作 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.12.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

执行mvn clean install清理并重新构建项目,确保依赖都正确下载。

2. 检查本地JDK版本

打开终端执行java -version,确认版本是17或更高。如果不是,需要升级JDK,并且在IDE中把项目的JDK配置切换到17+(比如IntelliJ里的Project Structure、Eclipse里的Build Path设置)。

二、修正代码中的冗余配置问题

你的代码里有一个潜在的冲突:MyService上标注了@Component,同时AppConfig里又用@Bean手动创建了MyService实例。这会导致Spring容器中存在两个同类型的Bean,虽然不是当前报错的直接原因,但后续可能引发其他问题,建议二选一:

方案A:保留@Bean,去掉@Component

把MyService上的@Component注解删掉,因为你已经通过@Bean手动注册了Bean:

package com.nirmalya;

// 移除@Component注解
public class MyService {
    public void doSomething() {
        System.out.println("MyService is doing something.");
    }
}

方案B:保留@Component,用组件扫描替代@Bean

在AppConfig上添加@ComponentScan,让Spring自动扫描包下的组件,同时删掉@Bean方法:

package com.nirmalya;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.nirmalya") // 扫描当前包及子包的组件
public class AppConfig {
    // 移除@Bean方法
}

三、清理IDE缓存

有时候IDE的缓存会导致编译器识别不到最新的依赖或类,执行以下操作:

  • IntelliJ IDEA:点击File -> Invalidate Caches... -> 选择Invalidate and Restart
  • Eclipse:点击Project -> Clean...,选择你的项目,然后重新构建

四、验证代码

完成以上步骤后,运行MainApp的main方法,应该就能正常获取Bean并执行doSomething()了。

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 07:38:13