Spring AOP - 在POJO getter上设置建议 - 不调用建议

时间:2016-01-03 16:18:02

标签: spring autowired spring-aop

我对Spring很陌生,我想在调用POJO getter时使用Spring AOP来发出建议。

我创建了一个简单的<table xmlns:xs="http://www.w3.org/2001/XMLSchema" border="1"> <tr> <th class="border-top border-bottom border-left border-right">Type</th> <th class="border-top border-bottom border-left border-right">Vehicle</th> <th class="border-top border-bottom border-left border-right">Label</th> <th class="border-top border-bottom border-left border-right">Quote-1</th> <th class="border-top border-bottom border-left border-right">Quote-2</th> </tr> <tr> <td class="border-top border-bottom border-left border-right">Motor</td> <td class="border-top border-bottom border-left border-right">Vehicle1</td> <td class="border-top border-bottom border-left border-right">Element1</td> <td class="border-top border-bottom border-left border-right">122</td> <td class="border-top border-bottom border-left border-right">233</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element2</td> <td class="border-top border-bottom border-left border-right">233</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element3</td> <td class="border-top border-bottom border-left border-right">344</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element4</td> <td class="border-top border-bottom border-left border-right">455</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">QuoteID</td> <td class="border-top border-bottom border-left border-right">1</td> <td class="border-top border-bottom border-left border-right">1</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element7</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element8</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element10</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element11</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element12</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">123</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element13</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element14</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element15</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element16</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right" colspan="count()">Motor</td> <td class="border-top border-bottom border-left border-right">Vehicle2</td> <td class="border-top border-bottom border-left border-right">Element1</td> <td class="border-top border-bottom border-left border-right">466</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element2</td> <td class="border-top border-bottom border-left border-right">577</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element3</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element4</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">QuoteID</td> <td class="border-top border-bottom border-left border-right">2</td> <td class="border-top border-bottom border-left border-right">2</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element7</td> <td class="border-top border-bottom border-left border-right">688</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element8</td> <td class="border-top border-bottom border-left border-right">799</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element10</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element11</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element12</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element13</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">133</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element14</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">234</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element15</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">456</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element16</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">654</td> </tr> <tr> <td class="border-top border-bottom border-left border-right" colspan="count()">Motor</td> <td class="border-top border-bottom border-left border-right">Vehicle3</td> <td class="border-top border-bottom border-left border-right">Element1</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element2</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element3</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element4</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">QuoteID</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element7</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element8</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element10</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element11</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element12</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">Not Found</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element13</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">234</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element14</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">443</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element15</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">654</td> </tr> <tr> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right"></td> <td class="border-top border-bottom border-left border-right">Element16</td> <td class="border-top border-bottom border-left border-right">Not Found</td> <td class="border-top border-bottom border-left border-right">544</td> </tr> </table>

POJO

我为getter这个名字创建了一个Aspect:

package com.atlas.datastore.datadomain;

import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.stereotype.Component;

@Component
public class Person
{
   private String name;

   public String getName() {
       System.out.println(name);
       return name;
   }

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

我创建了一个控制器(spring boot simple application)来使用getter:

package com.atlas.datastore.aspects;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class personAspect {
    @Pointcut("execution(* com.atlas.datastore.datadomain.Person.getName())")
    private void getName() {}

    @Before("getName()")
    public void doBeforeTask(){
       System.out.println("My name is: " );
    }
}

当我运行应用程序时,一切正常,我可以看到建议被解雇。

我遇到的问题是我不想{Person}对象package com.example.Controller; import com.atlas.datastore.datadomain.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/person") public class PersonController { @Autowired private Person person; @RequestMapping(value = "/{personId}", method = RequestMethod.GET) @ResponseBody() public Person personAction(@PathVariable String personId) { person.setName("John"); person.getName(); return person; } } 。当我使用默认构造函数创建Person(使用new关键字)时,我发现auto-wire没有被触发:

advice

在我的配置中,我使用以下注释:

package com.example.Controller;

import com.atlas.datastore.datadomain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/person")
public class PersonController {

   @RequestMapping(value = "/{personId}", method = RequestMethod.GET)
   @ResponseBody()
   public Person personAction(@PathVariable String personId) {

       Person person = new Person();

       person.setName("John");

       person.getName();

       return person;
   }
}

我可以在日志中看到以下输出:

@EnableAspectJAutoProxy(proxyTargetClass = true)

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

使用new运算符直接创建像Person person = new Person()这样的实例绕过Spring,因此Spring没有机会在此对象中注入依赖项或代理此对象。

要使Spring为使用new运算符的上述场景注入依赖项或代理,我们需要将Person对象注释为@Configurable,配置AnnotationBeanConfigurerAspect,使用Spring LoadTimeWeaving并使用-javaagent .....

运行应用程序

您可以在https://dzone.com/articles/domain-object-dependency-injection-with-spring

找到此示例用法

答案 1 :(得分:0)

而不是将@Pointcut添加到域对象: com.atlas.datastore.datadomain.Person.getName()

考虑创建一个服务,而不是将Person对象作为参数。将@Pointcut添加到服务方法。 将服务注入Web控制器,然后调用传递“新”Person的服务。