gpt4 book ai didi

java - Spring AOP - 异常 : org. springframework.beans.factory.BeanDefinitionStoreException:

转载 作者:太空宇宙 更新时间:2023-11-04 14:02:08 25 4
gpt4 key购买 nike

我使用 Eclipse 和 Java8_40。我从带有 AOP 的 Spring 框架开始(来自 http://www.tutorialspoint.com/spring/aop_with_spring.htm ),并且 Beans.xml(?) 遇到一些问题。我认为我创建的示例是正确的,但它崩溃并显示异常。

我在 CLASSPATH 中有:Apache *.jars

  • commons-logging-1.2.jar
  • commons-logging-1.2-javadoc.jar

Spring *.jars

  • spring-aop-4.1.5.RELEASE.jar
  • spring-beans-4.1.5.RELEASE.jar
  • spring-context-4.1.5.RELEASE.jar
  • spring-aspects-4.1.5.RELEASE.jar
  • spring-context-support-4.1.5.RELEASE.jar
  • spring-core-4.1.5.RELEASE.jar
  • spring-web-4.1.5.RELEASE.jar
  • spring-webmvc-4.1.5.RELEASE.jar
  • spring-expression-4.1.5.RELEASE.jar

AOP *.jar

  • aspectjrt-1.8.5.jar
  • aspectjweaver-1.8.5.jar
  • aspectj-1.8.5.jar

程序客户端

package spring.program;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.aop.trida.Student;
public class ProgramAOP1 {
@SuppressWarnings("resource")
public static void main(String [] args){
System.out.println("Start Programu");
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Student student = (Student) context.getBean("student");
student.getName();
student.getAge();
System.out.println("Konec Programu"); }
}

Spring Bean AOP 类:

package spring.beanAOP;
public class BeanAOP {
public void beforeAdvice(){
System.out.println("Going to setup student profile."); }
public void afterAdvice(){
System.out.println("Student profile has been setup."); }
public void afterReturningAdvice(Object retVal){
System.out.println("Returning:" + retVal.toString() ); }
public void AfterThrowingAdvice(IllegalArgumentException ex){
System.out.println("There has been an exception: " + ex.toString()); }
}

POJO 类:

package spring.aop.trida;
public class Student {
private Integer age;
private String name;
public void setAge(Integer age) {
this.age = age; }
public Integer getAge() {
System.out.println("Age : " + age );
return age; }
public void setName(String name) {
this.name = name; }
public String getName() {
System.out.println("Name : " + name );
return name; }
public void printThrowException(){
System.out.println("Exception raised");
throw new IllegalArgumentException(); }
}

Bean.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd ">
<aop:config>
<aop:aspect id="log" ref="beanAOP">
<aop:pointcut id="selectAll" expression="execution(* com.tutorialspoint.*.*(..))"/>
<aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
<aop:after pointcut-ref="selectAll" method="afterAdvice"/>
<aop:after-returning pointcut-ref="selectAll" returning="retVal" method="afterReturningAdvice"/>
<aop:after-throwing pointcut-ref="selectAll" throwing="ex" method="AfterThrowingAdvice"/>
</aop:aspect>
</aop:config>

<!-- Definition for student bean -->
<bean id="student" class="spring.aop.trida.Student">
<property name="name" value="Zara" />
<property name="age" value="11"/>
</bean>
<!-- Definition for logging aspect -->
<bean id="beanAOP" class="spring.beanAOP.BeanAOP"/>
</beans>

异常:

    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [Beans.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:414) 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:181) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)  at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)   at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)        at spring.program.ProgramAOP1.main(ProgramAOP1.java:17)    Caused by: java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice        at java.lang.ClassLoader.defineClass1(Native Method)        at java.lang.ClassLoader.defineClass(Unknown Source)        at java.security.SecureClassLoader.defineClass(Unknown Source)        at java.net.URLClassLoader.defineClass(Unknown Source)        at java.net.URLClassLoader.access$100(Unknown Source)        at java.net.URLClassLoader$1.run(Unknown Source)        at java.net.URLClassLoader$1.run(Unknown Source)        at java.security.AccessController.doPrivileged(Native Method)        at java.net.URLClassLoader.findClass(Unknown Source)        at java.lang.ClassLoader.loadClass(Unknown Source)        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)        at java.lang.ClassLoader.loadClass(Unknown Source)        at java.lang.ClassLoader.defineClass1(Native Method)        at java.lang.ClassLoader.defineClass(Unknown Source)        at java.security.SecureClassLoader.defineClass(Unknown Source)        at java.net.URLClassLoader.defineClass(Unknown Source)        at java.net.URLClassLoader.access$100(Unknown Source)        at java.net.URLClassLoader$1.run(Unknown Source)        at java.net.URLClassLoader$1.run(Unknown Source)        at java.security.AccessController.doPrivileged(Native Method)        at java.net.URLClassLoader.findClass(Unknown Source)        at java.lang.ClassLoader.loadClass(Unknown Source)        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)        at java.lang.ClassLoader.loadClass(Unknown Source)        at java.lang.ClassLoader.defineClass1(Native Method)        at java.lang.ClassLoader.defineClass(Unknown Source)        at java.security.SecureClassLoader.defineClass(Unknown Source)        at java.net.URLClassLoader.defineClass(Unknown Source)        at java.net.URLClassLoader.access$100(Unknown Source)        at java.net.URLClassLoader$1.run(Unknown Source)        at java.net.URLClassLoader$1.run(Unknown Source)        at java.security.AccessController.doPrivileged(Native Method)        at java.net.URLClassLoader.findClass(Unknown Source)        at java.lang.ClassLoader.loadClass(Unknown Source)        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)        at java.lang.ClassLoader.loadClass(Unknown Source)        at org.springframework.aop.config.ConfigBeanDefinitionParser.getAdviceClass(ConfigBeanDefinitionParser.java:411)        at org.springframework.aop.config.ConfigBeanDefinitionParser.createAdviceDefinition(ConfigBeanDefinitionParser.java:367)        at org.springframework.aop.config.ConfigBeanDefinitionParser.parseAdvice(ConfigBeanDefinitionParser.java:333)        at org.springframework.aop.config.ConfigBeanDefinitionParser.parseAspect(ConfigBeanDefinitionParser.java:228)        at org.springframework.aop.config.ConfigBeanDefinitionParser.parse(ConfigBeanDefinitionParser.java:116)        at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74)        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1427)        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1417)        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:174)        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:144)        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:100)        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:510)        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392)        ... 14 more

最佳答案

您错过了包含以下类的 aopalliance jar:

org.aopalliance.aop.Advice.class
org.aopalliance.aop.AspectException.class
org.aopalliance.intercept.ConstructorInterceptor.class
org.aopalliance.intercept.ConstructorInvocation.class
org.aopalliance.intercept.Interceptor.class
org.aopalliance.intercept.Invocation.class
org.aopalliance.intercept.Joinpoint.class
org.aopalliance.intercept.MethodInterceptor.class
org.aopalliance.intercept.MethodInvocation.class

可以找到here .

关于java - Spring AOP - 异常 : org. springframework.beans.factory.BeanDefinitionStoreException:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29211099/

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