Java新建Maven项目学习Spring注解开发显示:Unsupported class file major version 60

发生缘由

  • 学习Spring中注解开发

环境

  • jdk版本:jdk-16.0.2

  • Idea版本:2021.2

  • 电脑系统:win10

问题描述

  1. 新疆一个Maven项目,pom.xml文件中添加Spring依赖

        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.2.10.RELEASE</version>
            </dependency>
        </dependencies>
  2. 创建BookDao接口和BookDaoImpl类

    package com.linxuan.dao;
    ​
    public interface BookDao {
        public void save();
        public void update();
    }
    package com.linxuan.dao.impl;
    ​
    import com.linxuan.dao.BookDao;
    import org.springframework.stereotype.Repository;
    ​
    // 相当于Component注解,不过是dao层的Component注解。
    // 创建一个Been对象,让Spring管理,默认名字是当前类名首字母小写
    @Repository
    public class BookDaoImpl implements BookDao {
    ​
        @Override
        public void save() {
            System.out.println(System.currentTimeMillis());
            System.out.println("book dao save ...");
        }
    ​
        @Override
        public void update() {
            System.out.println("book dao update ...");
        }
    }
  3. 创建Spring的配置类

    package com.linxuan.config;
    ​
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    ​
    @Configuration
    @ComponentScan("com.linxuan")
    public class SpringConfig {
    }
  4. 编写运行类以及主方法

    package com.linxuan;
    ​
    import com.linxuan.config.SpringConfig;
    import com.linxuan.dao.BookDao;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    ​
    public class App {
        public static void main(String[] args) {
            ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
    ​
            BookDao bookDao = ctx.getBean(BookDao.class);
            bookDao.update();
        }
    }
  5. 运行主方法。结果发现报错,报错信息如下:

    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [D:\Java\IdeaProjects\ssm\spring02\spring01_aop\target\classes\com\linxuan\App.class]; nested exception is org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file [D:\Java\IdeaProjects\ssm\spring02\spring01_aop\target\classes\com\linxuan\App.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 60
        at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:452)
        at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:315)
        at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:276)
        at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132)
        at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:296)
        at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250)
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:207)
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:175)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:319)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:236)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:280)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:96)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:707)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533)
        at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:89)
        at com.linxuan.App.main(App.java:10)
    Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file [D:\Java\IdeaProjects\ssm\spring02\spring01_aop\target\classes\com\linxuan\App.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 60
        at org.springframework.core.type.classreading.SimpleMetadataReader.getClassReader(SimpleMetadataReader.java:60)
        at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:49)
        at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103)
        at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:123)
        at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:429)
        ... 15 more
    Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 60
        at org.springframework.asm.ClassReader.<init>(ClassReader.java:196)
        at org.springframework.asm.ClassReader.<init>(ClassReader.java:177)
        at org.springframework.asm.ClassReader.<init>(ClassReader.java:163)
        at org.springframework.asm.ClassReader.<init>(ClassReader.java:284)
        at org.springframework.core.type.classreading.SimpleMetadataReader.getClassReader(SimpleMetadataReader.java:57)
        ... 19 more

解决方案

  1. 点击IDEA左上角的File,然后再点击Settings

  2. 依次点击Build, Execution, Deployment > Compiler > Java Compiler

  3. 将右方出现的Moudle模块里面的项目和模块的版本切换值8即可 

  4. 点击Apply,然后再点击OK。
  5. 点击主方法运行,发现没得问题!

posted @ 2022-05-13 11:30  炸天帮帮主  阅读(677)  评论(0编辑  收藏  举报