标签: aop

是否可以在C#中为静态类或静态方法生成动态代理?

我试图想出一种方法(动态或实例)方法调用可以被动态代理拦截.我想将它作为c#扩展方法实现,但坚持如何为静态方法生成动态代理.

一些用法:

Repository.GetAll<T>().CacheForMinutes(10);
Repository.GetAll<T>().LogWhenErrorOccurs();

//or     
var repo = new Repository();
repo.GetAll<T>().CacheForMinutes(10);
repo.GetAll<T>().LogWhenErrorOccurs();
Run Code Online (Sandbox Code Playgroud)

我对任何图书馆(linfu,castle.dynamic proxy 2等)开放.

谢谢!

c# aop castle-dynamicproxy

1
推荐指数
1
解决办法
1953
查看次数

spring声明roolback-for的例子?

想要在spring aop中的声明式事务管理示例........

其实这里

<aop:config>
    <aop:advisor advice-ref="addAdvice" pointcut="execution(* com.DAO.*.*(..))"/>
</aop:config>
<tx:advice id="addAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="add*" propagation="REQUIRED" rollback-for="" />
    </tx:attributes>
</tx:advice>
Run Code Online (Sandbox Code Playgroud)

所以这里我想写的实际上是rollback-for ="",有什么方法或其他吗?如果方法那么该方法将放在哪里?

java aop spring transactions

1
推荐指数
1
解决办法
1890
查看次数

为Spring MVC Controller - AOP或Spring Security的方法传递密码?

我已经使用Spring MVC一段时间了,现在为JSP页面添加了带注释的控制器.我有一个类似这样的类:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class AdminController {

   @RequestMapping(value = "/doStuff1.htm", method = RequestMethod.POST)
   public void doStuff1(@RequestParam("password")String password) {

        // do some stuff

   }

   @RequestMapping(value = "/doStuff2.htm", method = RequestMethod.POST)    
   public void doStuff2(
       @RequestParam("password")String password,
       @RequestParam("foo")String foo{

        // do some stuff

   }    
}   
Run Code Online (Sandbox Code Playgroud)

如您所见,每个调用都会传入密码参数.密码从对话框中读取并传递到每个提交的调用中.

我想从方法调用中删除密码参数以获得"更干净"的代码.

我为此目的快速浏览了Spring安全性,但它似乎有点重量级.也许可以使用AOP?

有一个明显的解决方案我错过了吗?

非常感谢, - 斯科特

java aop spring spring-mvc spring-security

1
推荐指数
1
解决办法
2035
查看次数

在Spring 3中为控制器实现AOP

如何使用带注释的控制器实现AOP?

我已经搜索并找到了之前关于这个问题的两篇帖子,但似乎无法让解决方案起作用.

发布解决方案1

发布解决方案2

这就是我所拥有的:

派遣Servlet:

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.foo.controller"/>

    <bean id="fooAspect" class="com.foo.aop.FooAspect" />

    <aop:aspectj-autoproxy>
        <aop:include name="fooAspect" />
    </aop:aspectj-autoproxy>
</beans>
Run Code Online (Sandbox Code Playgroud)

控制器:

@Controller
public class FooController {

    @RequestMapping(value="/index.htm", method=RequestMethod.GET)
    public String showIndex(Model model){
        return "index";
    }
}
Run Code Online (Sandbox Code Playgroud)

方面:

@Aspect
public class FooAspect {

    @Pointcut("@target(org.springframework.stereotype.Controller)")
    public void controllerPointcutter() {}

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

    @Before("controllerPointcutter()")
    public void beforeMethodInController(JoinPoint jp){
        System.out.println("### before controller call...");
    }

    @AfterReturning("controllerPointcutter() && …
Run Code Online (Sandbox Code Playgroud)

aop spring controller

1
推荐指数
1
解决办法
7159
查看次数

如何访问建议方法的返回Object实例

我正在使用spring AOP来建议我的服务方法,尤其是返回一个对象的方法,我想在建议处理过程中访问该对象.

我的配置工作正常,没有问题.

这是adviced方法的签名,方法根据方法参数中的数据返回一个新实例,因此参数不可用

@Traceable(ETraceableMessages.SAUVER_APPORTEUR)
public ElementNiveauUn save(ElementNiveauUn apporteur) throws ATPBusinessException {
    String identifiant = instanceService.sauverInstance(null, apporteur);
    List<String> extensions = new ArrayList<String>();
    extensions.add(ELEMENTSCONTENUS);
    extensions.add(TYPEELEMENT);
    extensions.add(VERSIONING);
    extensions.add(PARAMETRAGES);
    extensions.add(PARAMETRAGES + "." + PARAMETRES);
    return (ElementNiveauUn ) instanceService.lireInstanceParId(identifiant, extensions.toArray(new String[]{}));
}
Run Code Online (Sandbox Code Playgroud)

这就是我想要做的事情

@Around(value = "execution(elementNiveauUn fr.generali.nova.atp.service.metier.impl.*.*(..)) && @annotation(traceable) && args(element)", argNames = "element,traceable")
public void serviceLayerTraceAdviceBasedElementInstanceAfter2(final ProceedingJoinPoint pjp,
                final ElementNiveauUn element, final Traceable traceable) throws SecurityException,
                NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    // current user
    String currentUserId = findCurrentUserId();

    // wether user is found …
Run Code Online (Sandbox Code Playgroud)

java aop spring aspectj

1
推荐指数
1
解决办法
7691
查看次数

使用Unity 2.0拦截不能拦截WCF服务

在我的WCF Web应用程序中,我已经为拦截配置了Unity容器.以下是我的统一配置.

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> 
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>

    <assembly name="Infrastructure" />
    <assembly name="WCFServiceLib1"/>

    <namespace name="Infrastructure"/>
    <namespace name="WCFServiceLib1" />

    <container>
      <extension type="Interception" />
      <register type="IService1" mapTo="Service1">
        <interceptor type="InterfaceInterceptor"/>
        <interceptionBehavior type="LogPerformanceDataBehavior"/>
      </register>
    </container>
</unity>
Run Code Online (Sandbox Code Playgroud)

当我尝试使用wcftestclient工具调用服务上的方法时,抛出以下异常.

ArgumentException - 类型WCFServiceLib1.Service1不可拦截.
参数名称:interceptedType

我使用svctraceviewer工具获取上述异常详细信息.

以下是LogPerformanceDataBehavior类的实现

public class LogPerformanceDataBehavior : IInterceptionBehavior
{
    public IEnumerable<Type> GetRequiredInterfaces()
    {
        return Type.EmptyTypes;
    }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    {
        var watch = new Stopwatch();

        watch.Start();
        IMethodReturn methodReturn = getNext()(input, getNext);
        watch.Stop();

        string sb = string.Format("Method {0}.{1} executed in: ({2} …
Run Code Online (Sandbox Code Playgroud)

wcf aop unity-container

1
推荐指数
1
解决办法
2583
查看次数

NServiceBus中的AoP支持?

在这里回到我以前的问题:

但我真的想拦截消息并拥有处理程序的实例,并能够影响消息是否应该转到处理程序.基本上是"围绕建议".

现在,实现这类事物的最传统方式是通过目标对象的动态继承和覆盖虚拟方法.由于缺少文档,我无法分辨的是,NServiceBus是否创建或构建其消息处理程序实例.如果它构建起来,那么它就不能动态继承,所以大多数AoP框架可能都是不可能的,否则最流行的DI容器应该可以做到.

然而,使用Saga handers进行测试似乎NServiceBus建立而不是创建新的,因为需要一个默认构造函数,它指向NServiceBus手动激活类.

是的,我意识到我可以使用好的'OOP来解决同样的问题,但我通常更喜欢AoP以获得更好(更少)的耦合.


.net c# soa aop nservicebus

1
推荐指数
1
解决办法
279
查看次数

使用AspectJ监视数据库访问方法

我想使用AspectJ来监视数据库语句.

当我将切入点定义为

@Pointcut("execution(* java.sql.*Statement.execute*(..))")
Run Code Online (Sandbox Code Playgroud)

这是行不通的.但是当我将切入点定义为

@Pointcut("execution(* *.*(..))")
Run Code Online (Sandbox Code Playgroud)

它可以监控所有方法.

如何定义切入点以仅监视数据库访问方法?

java sql aop monitoring aspectj

1
推荐指数
1
解决办法
697
查看次数

如何在Feign调用中使用AOP

我对如何在AOP中使用Feign客户感兴趣.例如:

API:

public interface LoanClient {
    @RequestLine("GET /loans/{loanId}")
    @MeteredRemoteCall("loans")
    Loan getLoan(@Param("loanId") Long loanId);
}
Run Code Online (Sandbox Code Playgroud)

配置:

@Aspect
@Component // Spring Component annotation
public class MetricAspect {

    @Around(value = "@annotation(annotation)", argNames = "joinPoint, annotation")
    public Object meterRemoteCall(ProceedingJoinPoint joinPoint, 
                        MeteredRemoteCall annotation) throws Throwable {
    // do something
  }
}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何"拦截"api方法调用.我哪里做错了?

更新:

我的Spring类注释:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MeteredRemoteCall {

    String serviceName();
}
Run Code Online (Sandbox Code Playgroud)

java aop aspectj spring-aop feign

1
推荐指数
1
解决办法
1695
查看次数

Prevent Spring AOP from closing my Stream?

I'm using the following construction (simplified):

@Component
class RuleProvider {
    public Stream<String> getRules() {
        return Stream.of("r1", "r2", "r3");
    }
}

@Service
class RuleService {
    @Autowired RuleProvider ruleProvider;
    public void evaluateRules() {
        ruleProvider.getRules().foreach(System.out::println);
    }
}
Run Code Online (Sandbox Code Playgroud)

and I use Spring AOP to perform logging. Now I'd like to log all the rules that are handed to the service. In general, this should do it:

@Aspect
class LoggingAspect {
    @AfterReturning(value="execution(* *..RuleProvider.getRules(*))",
                    returning="rules")
    void logRules(JoinPoint jp, Stream<String> rules) {
        Logger logger = LoggerFactory.getLogger(jp.getTarget().getClass());
        rules.peek(rule …
Run Code Online (Sandbox Code Playgroud)

java aop spring spring-aop

1
推荐指数
1
解决办法
38
查看次数