English 中文(简体)
页: 1
原标题:Spring AOP: advice is not triggered

试图设计一个简单的方面,即在任何公共方法实施时,将“logg”字印成ole。

http://www.hchr.org。

@Aspect
public class LoggingAspect {

    @Pointcut("execution(public * *(..))")
    public void publicServices() {
    };

    @Before("publicServices()")
    public void logg() {
        System.out.println("logg");
    }

}

http://www.ohchr.org。

    <context:component-scan base-package="aspectlogging" /> 
    <aop:aspectj-autoproxy/>
    <bean id="loggingAspectHolder" class="aspectlogging.LoggingAspect"/>

<<>strong>simple bean:

package aspectlogging;
@Component
    public class TestableBean {

        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
        }

<<>strongtest>:

public class TestLogging {
    public static void main(String[] args) {
        TestableBean tb = new TestableBean();
        tb.setName("yes");
        tb.getName();
    }
}

I expect,that result of running of TestLogging will be "logg" word in console,and no output returned. Do I understand AOP correctly in this case?

最佳回答

With @Around advice, you need to have a ProceedingJoinPoint pjp argument to the advising method and to call pjp.proceed() at the point in the advisor when you want the wrapped method to be called. It s easier to use @Before advice really, when what you ve done will otherwise work just fine.


[EDIT]: Also, you must let Spring construct your beans for you instead of directly calling new. This is because the bean object is actually a proxy for your real object (which sits inside it). Because your target object doesn t implement an interface, you will need to have the cglib library on your classpath in addition to the Spring libraries. (Alternatively, you can go with using AspectJ fully, but that requires using a different compiler configuration.)

为了创造你们的美人,你首先需要创造春天context,然后问,就好客而言。 这意味着:

TestableBean tb = new TestableBean();

(假设你重新使用春天3,而你的XML会议在你的班次上“会场”。)

ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
TestableBean tb = context.getBean(TestableBean.class);

您的法典其余部分保持不变(在对<编码> 进口<>/编码>的说明和可能的额外依赖性进行调整之后)。

问题回答

这一点并不令人信服,但也许你需要利用一个春天管理的可测验的加勒比,来启动自动交换标准。

编辑:当然,您可使用@Around。 你提供的方式——但这个问题已由另一个答复处理,因此在此省略。

edit2:如果你需要帮助如何获得一个春天有管理的灯塔,请大家自由要求。 但是,既然你已经走到你身上,我认为你可以处理:

edit3: Hehe。 Ok. maybe not :

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

will load your application context. Load beans from there by calling:

TestableBean testableBean = (TestableBean )ctx.getBean("testableBean ");

定义<代码> Bean就好像你一样。

edit4:现在,我确信,死者是非后代人管理的床。

<编码> 使用最简单的工作。 Spring AOP比充分利用AspectJ更为简单,因为没有必要将AspectJ汇编者/编织者引入你的发展和建设进程。 如果你只需要就春天的行动提出建议,那么春天行动就是正确的选择。 如果你需要向域标或春季集装箱未管理的其他任何物体提供咨询意见,那么你将需要使用AspectJ.。

摘自: rel=“nofollow”http://static.merssource.org/mers/docs/2.0.x/fer/aop.html





相关问题
array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

How can I determine Objects in application context?

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. I am using some configuration XMLs (applicationContext.xml, etc.) and ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...