gpt4 book ai didi

java - 创建简单的 Spring AOP 时出现异常抛出建议演示

转载 作者:行者123 更新时间:2023-12-02 13:12:04 33 4
gpt4 key购买 nike

嗨,我收到异常****线程“main”org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException****同时尝试做一个简单的Spring AOP抛出建议Demo。请有人帮助我。

有异常的详细代码如下

MyInterface.java

package com.Spring.Aop.SpringAOPThrowsAdvice;

public class MyImplClass implements MyInterface {
public void m1(){

int java4s = 10/0;
System.out.println("Am in business method..");

}
}

MyInterface.java

package com.Spring.Aop.SpringAOPThrowsAdvice;

public interface MyInterface {
void m1();
}

MyThrowsAdvice.java

package com.Spring.Aop.SpringAOPThrowsAdvice;

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;

public class MyThrowsAdvice implements ThrowsAdvice {

//First preference
public void afterThrowing(ArithmeticException e)
{
System.out.println("This is from ArithmeticException method");
}

//Second preference
public void afterThrowing(Method m, Object args[], Object target,Exception e)
{
System.out.println("Am from 4 parameters method called from "+m.getName());
}

//Third preference
public void afterThrowing(Exception e)
{
System.out.println("Fom single parameter method");
}

}

OurLogic.java

package com.Spring.Aop.SpringAOPThrowsAdvice;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class OurLogic {
public static void main(String args[])
{
Resource res = new ClassPathResource("spconfig.xml");
BeanFactory factory = new XmlBeanFactory(res);

MyInterface inter =(MyInterface)factory.getBean("id3");
inter.m1();
}
}

spconfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="id1" class="com.Spring.Aop.SpringAOPThrowsAdvice.MyImplClass" />
<bean id="id2" class="com.Spring.Aop.SpringAOPThrowsAdvice.MyThrowsAdvice" />
<bean id="id3" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="proxyInterfaces" value="com.Spring.Aop.SpringAOPThrowsAdvice.MyInterface" />
<property name="interceptorNames" >
<list>
<value>id2</value>
</list>
</property>
<property name="target">
<ref bean="id1"/>
</property>

</bean>

</beans>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.Spring.Aop</groupId>
<artifactId>SpringAOPThrowsAdvice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>SpringAOPThrowsAdvice</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>2.5.6</version>
</dependency>

</dependencies>
</project>

异常(exception)是:

May 11, 2017 12:47:38 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spconfig.xml]
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 2 in XML document from class path resource [spconfig.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 8; cvc-elt.1: Cannot find the declaration of element 'beans'.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:404)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
at com.Spring.Aop.SpringAOPThrowsAdvice.OurLogic.main(OurLogic.java:12)
Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 8; cvc-elt.1: Cannot find the declaration of element 'beans'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:396)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:284)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1900)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:740)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3132)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:852)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
... 5 more

最佳答案

您只是没有将 namespace bean 放入配置文件头中:http://docs.spring.io/spring-integration/reference/html/xml.html

关于java - 创建简单的 Spring AOP 时出现异常抛出建议演示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43909476/

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