gpt4 book ai didi

java - 提供上下文时出现异常 :annotation-config

转载 作者:行者123 更新时间:2023-11-30 06:24:34 25 4
gpt4 key购买 nike

我是 Spring 的新手。在尝试时,我能够使用

从 xml 配置设置 Autowiring
<bean id="triangle" class="com.spring.sample.Triangle" autowire="byName"/>

我还尝试使用 @autowire 注释,在定义 bean 时成功了

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>

但是当我尝试使用<context:annotation-config />时我遇到了以下异常。

INFO: Loading XML bean definitions from class path resource [Spring.xml]
Exception in thread "main" java.lang.IllegalArgumentException
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:52)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
at org.springframework.context.annotation.ConfigurationClassUtils.checkConfigurationClassCandidate(ConfigurationClassUtils.java:70)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:253)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.spring.sample.MainSpringClass.main(MainSpringClass.java:17)

我在 StackOverflow 的几个问题中发现了这个问题,但没有一个提供可靠的答案。

JDK version :1.8.0_144
Spring framework: 3.2.0.RELEASE

Spring XML 配置是

<?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"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<bean id="triangle" class="com.spring.sample.Triangle">
</bean>

<bean id="pointA" class="com.spring.sample.Point">
<property name="x" value="10" />
<property name="y" value="$10" />
</bean>
<bean id="pointB" class="com.spring.sample.Point">
<property name="x" value="20" />
<property name="y" value="20" />
</bean>
<bean id="pointC" class="com.spring.sample.Point">
<property name="x" value="30" />
<property name="y" value="30" />
</bean>
<!-- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean> -->
<context:annotation-config />
</beans>

最后是 bean 三角

public class Triangle {

@Autowired
private Point pointA;
@Autowired
private Point pointB;
@Autowired
private Point pointC;

public Point getPointA() {
return pointA;
}

public void setPointA(Point pointA) {
this.pointA = pointA;
}

public Point getPointB() {
return pointB;
}

public void setPointB(Point pointB) {
this.pointB = pointB;
}

public Point getPointC() {
return pointC;
}

public void setPointC(Point pointC) {
this.pointC = pointC;
}
}

在配置中使用上下文标记时如何解决此异常?

最佳答案

发现问题了。问题不在于代码,而在于 Spring 框架版本。 JDK 1.8 与 Spring 框架不兼容:3.2.0.RELEASE。升级spring框架解决了这个问题。

关于java - 提供上下文时出现异常 :annotation-config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47467494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com