@Component 扫描 spring 个 bean

@Component scan for spring bean

我有一个名为 Coach 的接口,由 TennisCoach 实现,我正在尝试扫描 xml

中的 bean

with <context:component-scan base-package="com.luv2code.springdemo" /> 但是当我运行我的主要方法时我有这个错误

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [C:\Users\pooya\eclipse-workspace\spring-demo-ann\bin\com\luv2code\springdemo\Coach.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 [C:\Users\pooya\eclipse-workspace\spring-demo-ann\bin\com\luv2code\springdemo\Coach.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 57
 at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:454)
 at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:316)
 at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:275)
 at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:90)
 at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74)
 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1366)
 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1352)
 at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:179)
 at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:149)
 at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:96)
 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:513)
 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:393)
 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224)
 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195)
 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:257)
 at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:128)
 at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:94)
 at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:133)
 at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:622)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:518)
 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
 at com.luv2code.springdemo.helloappann.main(helloappann.java:9)
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 [C:\Users\pooya\eclipse-workspace\spring-demo-ann\bin\com\luv2code\springdemo\Coach.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 57
 at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:57)
 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:430)
 ... 25 more
Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 57
 at org.springframework.asm.ClassReader.<init>(ClassReader.java:184)
 at org.springframework.asm.ClassReader.<init>(ClassReader.java:166)
 at org.springframework.asm.ClassReader.<init>(ClassReader.java:152)
 at org.springframework.asm.ClassReader.<init>(ClassReader.java:273)
 at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:54)
 ... 28 more

package com.luv2code.springdemo;


public interface Coach {

 public String getDailyWorkout();
 
}

package com.luv2code.springdemo;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;


@Component("thatSillyCoach")
public class TennisCoach implements Coach {

 
 @Override
 public String getDailyWorkout() {
  return "Practice your backhand volley";
 }

}

package com.luv2code.springdemo;

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class helloappann {

 public static void main(String[] args) {
  // read spring config file
  ClassPathXmlApplicationContext context = 
    new ClassPathXmlApplicationContext("app.xml");
  
  // get the bean from spring container
  Coach theCoach = context.getBean("thatSillyCoach", Coach.class);
  
  // call a method on the bean
  System.out.println(theCoach.getDailyWorkout());
    
  // close the context
  context.close();
  
 }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

 <!-- add entry to enable component scanning -->

 <context:component-scan base-package="com.luv2code.springdemo" />

</beans>

您的 java 版本不匹配:版本 57 是 java 13

您可能需要再次 mvn clean install 并检查您的版本。

你可以去那里看看:https://www.baeldung.com/java-lang-unsupportedclassversion