我是春天的新手。尝试时我可以使用
从xml配置中设置自动装配<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 Triangle
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;
}
}
如何在配置中使用上下文标记来解决此异常?
答案 0 :(得分:1)
发现了这个问题。问题不在于代码,而在于Spring Framework版本。 JDK 1.8与Spring框架不兼容:3.2.0.RELEASE。升级spring框架解决了这个问题。