使用spring aop 2.0无aop输出

发布于 2024-09-12 02:37:11 字数 4993 浏览 2 评论 0原文

我正在阅读 Spring in Action,并试图设置一个 aop 示例。

package com.springinaction.chapter01.knight;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class KnightApp {
    public static void main(String[] args) throws Exception {
        BeanFactory factory = new XmlBeanFactory(new FileSystemResource("knight.xml"));
        Knight knight = (Knight) factory.getBean("knight");
        knight.embarkOnQuest();
    }
}

文件 KnightOfTheRoundTable.java:

package com.springinaction.chapter01.knight;

public class KnightOfTheRoundTable implements Knight {
    private String name;
    private Quest quest;

    public KnightOfTheRoundTable(String name) {
        this.name = name;
    }

    public Object embarkOnQuest() throws QuestFailedException {
        //minstrel.singBefore(this);
        HolyGrail grail = (HolyGrail) quest.embark();
        //minstrel.singAfter(this);
        return grail;
    }

    public void setQuest(Quest quest) {
        this.quest = quest;
    }

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return name;
    }
}

文件 Minstrel.java:

package com.springinaction.chapter01.knight;

import org.apache.log4j.Logger;

public class Minstrel {
    private static final Logger SONG = Logger.getLogger(Minstrel.class);

    public void singBefore(Knight knight) {
        SONG.info("Fa la la; Sir " + knight.getName() + " is so brave!");
        System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
    }

    public void singAfter(Knight knight) {
        SONG.info("Tee-hee-he; Sir " + knight.getName()
              + " did embark on a quest!");
    }
}

输出如下:

DEBUG ClassUtils - Class [org.apache.commons.collections.map.LinkedMap] or one of its dependencies is not present: java.lang.ClassNotFoundException: org.apache.commons.collections.map.LinkedMap
DEBUG ClassUtils - Class [edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap] or one of its dependencies is not present: java.lang.ClassNotFoundException: edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap
INFO  XmlBeanDefinitionReader - Loading XML bean definitions from file [C:\Users\Chris\workspace\chapter01\knight.xml]
DEBUG DefaultDocumentLoader - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
DEBUG PluggableSchemaResolver - Loading schema mappings from [META-INF/spring.schemas]
DEBUG PluggableSchemaResolver - Found XML schema [http://www.springframework.org/schema/aop/spring-aop-2.0.xsd] in classpath: org/springframework/aop/config/spring-aop-2.0.xsd
DEBUG DefaultBeanDefinitionDocumentReader - Loading bean definitions
DEBUG XmlBeanFactory - Creating shared instance of singleton bean 'knight'
DEBUG XmlBeanFactory - Creating instance of bean 'knight' with merged definition [Root bean: class [com.springinaction.chapter01.knight.KnightOfTheRoundTable]; scope=singleton; abstract=false; lazyInit=false; autowireCandidate=true; autowireMode=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\Users\Chris\workspace\chapter01\knight.xml]]
DEBUG XmlBeanFactory - Eagerly caching bean 'knight' to allow for resolving potential circular references
DEBUG XmlBeanFactory - Creating shared instance of singleton bean 'quest'
DEBUG XmlBeanFactory - Creating instance of bean 'quest' with merged definition [Root bean: class [com.springinaction.chapter01.knight.HolyGrailQuest]; scope=singleton; abstract=false; lazyInit=false; autowireCandidate=true; autowireMode=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\Users\Chris\workspace\chapter01\knight.xml]]
DEBUG XmlBeanFactory - Eagerly caching bean 'quest' to allow for resolving potential circular references

文件 Knight.xml:

<bean id="minstrel" class="com.springinaction.chapter01.knight.Minstrel" />
<aop:config>
    <aop:aspect ref="minstrel">
        <aop:pointcut id="questPointcut"
            expression="execution(* *.embarkOnQuest(..)) and target(bean)" />
        <aop:before method="singBefore" pointcut-ref="questPointcut"
            arg-names="bean" />
        <aop:after-returning method="singAfter"
            pointcut-ref="questPointcut" arg-names="bean" />
    </aop:aspect>
</aop:config>
<bean id="quest" class="com.springinaction.chapter01.knight.HolyGrailQuest" />
<bean id="knight"
    class="com.springinaction.chapter01.knight.KnightOfTheRoundTable">
    <constructor-arg value="Bedivere" />
    <property name="quest" ref="quest" />
</bean>

不调用 minstrel 代码。

谢谢。

I'm reading Spring in Action and I'm trying to set an aop example.

package com.springinaction.chapter01.knight;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class KnightApp {
    public static void main(String[] args) throws Exception {
        BeanFactory factory = new XmlBeanFactory(new FileSystemResource("knight.xml"));
        Knight knight = (Knight) factory.getBean("knight");
        knight.embarkOnQuest();
    }
}

The file KnightOfTheRoundTable.java:

package com.springinaction.chapter01.knight;

public class KnightOfTheRoundTable implements Knight {
    private String name;
    private Quest quest;

    public KnightOfTheRoundTable(String name) {
        this.name = name;
    }

    public Object embarkOnQuest() throws QuestFailedException {
        //minstrel.singBefore(this);
        HolyGrail grail = (HolyGrail) quest.embark();
        //minstrel.singAfter(this);
        return grail;
    }

    public void setQuest(Quest quest) {
        this.quest = quest;
    }

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return name;
    }
}

The file Minstrel.java:

package com.springinaction.chapter01.knight;

import org.apache.log4j.Logger;

public class Minstrel {
    private static final Logger SONG = Logger.getLogger(Minstrel.class);

    public void singBefore(Knight knight) {
        SONG.info("Fa la la; Sir " + knight.getName() + " is so brave!");
        System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
    }

    public void singAfter(Knight knight) {
        SONG.info("Tee-hee-he; Sir " + knight.getName()
              + " did embark on a quest!");
    }
}

The output is as follows :

DEBUG ClassUtils - Class [org.apache.commons.collections.map.LinkedMap] or one of its dependencies is not present: java.lang.ClassNotFoundException: org.apache.commons.collections.map.LinkedMap
DEBUG ClassUtils - Class [edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap] or one of its dependencies is not present: java.lang.ClassNotFoundException: edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap
INFO  XmlBeanDefinitionReader - Loading XML bean definitions from file [C:\Users\Chris\workspace\chapter01\knight.xml]
DEBUG DefaultDocumentLoader - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
DEBUG PluggableSchemaResolver - Loading schema mappings from [META-INF/spring.schemas]
DEBUG PluggableSchemaResolver - Found XML schema [http://www.springframework.org/schema/aop/spring-aop-2.0.xsd] in classpath: org/springframework/aop/config/spring-aop-2.0.xsd
DEBUG DefaultBeanDefinitionDocumentReader - Loading bean definitions
DEBUG XmlBeanFactory - Creating shared instance of singleton bean 'knight'
DEBUG XmlBeanFactory - Creating instance of bean 'knight' with merged definition [Root bean: class [com.springinaction.chapter01.knight.KnightOfTheRoundTable]; scope=singleton; abstract=false; lazyInit=false; autowireCandidate=true; autowireMode=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\Users\Chris\workspace\chapter01\knight.xml]]
DEBUG XmlBeanFactory - Eagerly caching bean 'knight' to allow for resolving potential circular references
DEBUG XmlBeanFactory - Creating shared instance of singleton bean 'quest'
DEBUG XmlBeanFactory - Creating instance of bean 'quest' with merged definition [Root bean: class [com.springinaction.chapter01.knight.HolyGrailQuest]; scope=singleton; abstract=false; lazyInit=false; autowireCandidate=true; autowireMode=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\Users\Chris\workspace\chapter01\knight.xml]]
DEBUG XmlBeanFactory - Eagerly caching bean 'quest' to allow for resolving potential circular references

File knight.xml :

<bean id="minstrel" class="com.springinaction.chapter01.knight.Minstrel" />
<aop:config>
    <aop:aspect ref="minstrel">
        <aop:pointcut id="questPointcut"
            expression="execution(* *.embarkOnQuest(..)) and target(bean)" />
        <aop:before method="singBefore" pointcut-ref="questPointcut"
            arg-names="bean" />
        <aop:after-returning method="singAfter"
            pointcut-ref="questPointcut" arg-names="bean" />
    </aop:aspect>
</aop:config>
<bean id="quest" class="com.springinaction.chapter01.knight.HolyGrailQuest" />
<bean id="knight"
    class="com.springinaction.chapter01.knight.KnightOfTheRoundTable">
    <constructor-arg value="Bedivere" />
    <property name="quest" ref="quest" />
</bean>

The minstrel code is not invoked.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风月客 2024-09-19 02:37:11

抱歉,

文件 Knight.xml :

<bean id="minstrel" class="com.springinaction.chapter01.knight.Minstrel" />
<aop:config>
    <aop:aspect ref="minstrel">
        <aop:pointcut id="questPointcut"
            expression="execution(* *.embarkOnQuest(..)) and target(bean)" />
        <aop:before method="singBefore" pointcut-ref="questPointcut"
            arg-names="bean" />
        <aop:after-returning method="singAfter"
            pointcut-ref="questPointcut" arg-names="bean" />
    </aop:aspect>
</aop:config>
<bean id="quest" class="com.springinaction.chapter01.knight.HolyGrailQuest" />
<bean id="knight"
    class="com.springinaction.chapter01.knight.KnightOfTheRoundTable">
    <constructor-arg value="Bedivere" />
    <property name="quest" ref="quest" />
</bean>

Sorry,

file knight.xml :

<bean id="minstrel" class="com.springinaction.chapter01.knight.Minstrel" />
<aop:config>
    <aop:aspect ref="minstrel">
        <aop:pointcut id="questPointcut"
            expression="execution(* *.embarkOnQuest(..)) and target(bean)" />
        <aop:before method="singBefore" pointcut-ref="questPointcut"
            arg-names="bean" />
        <aop:after-returning method="singAfter"
            pointcut-ref="questPointcut" arg-names="bean" />
    </aop:aspect>
</aop:config>
<bean id="quest" class="com.springinaction.chapter01.knight.HolyGrailQuest" />
<bean id="knight"
    class="com.springinaction.chapter01.knight.KnightOfTheRoundTable">
    <constructor-arg value="Bedivere" />
    <property name="quest" ref="quest" />
</bean>
記柔刀 2024-09-19 02:37:11

我在这里看到两个问题:
1. 您的日志包含类 org.apache.commons.collections.map.LinkedMapedu.emory.mathcs.backport.java.util.concurrent 的 ClassNotFoundException .ConcurrentHashMap 这很奇怪,因为你的代码没有提到这些类,而且据我所知,Spring 也没有使用它们。要修复此异常,您需要将 Apache common-collections.jarbackport-util-concurrent.jar 库添加到您的类路径中。

2.您使用BeanFactory实现而不是ApplicationContext实现。根据 Spring 文档,BeanFactory 实现默认不支持 AOP。这是那里的引用:
BeanFactory 几乎只是实例化和配置 beans。 ApplicationContext 也可以做到这一点,它提供支持基础架构来启用许多特定于企业的功能,例如事务和 AOP。

I see two problems here:
1. Your logs contain ClassNotFoundException for classes org.apache.commons.collections.map.LinkedMap and edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap wich is quite strange, because your code doesn't mention none of these classes, and Spring, as far as I know, also doesn't use them. To fix this exeptions, you need to add Apache common-collections.jar and backport-util-concurrent.jar libraries to your classpath.

2. You use BeanFactory implementation instead of ApplicationContext implementation. According to Spring documentation BeanFactory implementations by default doesn't support AOP. Here is quote from there:
A BeanFactory pretty much just instantiates and configures beans. An ApplicationContext also does that, and it provides the supporting infrastructure to enable lots of enterprise-specific features such as transactions and AOP.

绝對不後悔。 2024-09-19 02:37:11

我在阅读 Spring AOP 时也遇到了同样的问题。然后我在 pom.xml 中添加了依赖项(我将 spring 与 Maven 一起使用),然后它开始工作。

<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
<scope>compile</scope>
</dependency>

注意:我使用以下代码来加载 spring 配置文件。

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");

I also had the same problem while reading about Spring AOP. Then I added the dependency in my pom.xml (I used spring with maven) and then it started working.

<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
<scope>compile</scope>
</dependency>

Note: I used following code for loading spring configuration file.

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文