什么是分析Scala方法调用的标准方法?
我需要的是一个方法的钩子,我可以使用它来启动和停止计时器.
在Java中,我使用方面编程aspectJ来定义要分析的方法并注入字节码以实现相同的目的.
在Scala中是否有更自然的方式,我可以在函数前后定义一组函数,而不会在进程中丢失任何静态类型?
从纯计算机科学(或计算语言学)的角度来看,我想知道这些词之间的区别:
各种语言以不同的方式使用这些单词和功能.在Python中,例如,Decorators [根据Python Wiki](强调我的):
装饰器动态地改变函数,方法或类的功能,而不必直接使用子类或更改正在装饰的函数的源代码.
这让我感觉非常类似于面向方面的编程工具,如PostSharp或DynamicProxy.即:
[Profile]
private static void SleepSync()
{
Thread.Sleep(200);
}
Run Code Online (Sandbox Code Playgroud)
来源:PostSharp示例
在C#和Java(以及无数其他语言)中,属性可以表示Decorator-ish模式(C#)或字段(Java).
在C++中通过boost或PhP通过内置特征词,我们可以使用特征来扩展类,如下所示:https://en.wikipedia.org/wiki/Trait_ (computer_programming)
因此,从"纯粹"的角度来看,所有这些实际上是什么的规范定义是什么?有没有更好的方法来定义它们?
当访问特定包的类中的方法时,我在记录日志信息时遇到问题.换句话说,发生"不"记录.我甚至绝望并添加了System.out.println语句,没有运气.
我的所有类都位于org.my.package包下,即org.my.package.controller,org.my.package.model等.
这是我的Application类:
package org.my.package;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan(basePackages = {"org.my.package.config"})
@EnableAutoConfiguration
@EnableAspectJAutoProxy
public class FirstWebAppApplication {
public static void main(String[] args) {
SpringApplication.run(FirstWebAppApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的配置类:
package org.my.package.config;
import org.deloitte.javatraining.daythree.utilities.MyLogger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = {"org.my.package.utilities"})
public class AssetConfig {
//-----------------------------------------------------------------------------------------------------------------------
@Bean
public MyLogger myLogger(){
return new MyLogger();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Aspect类:
package org.my.package.utilities;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; …
Run Code Online (Sandbox Code Playgroud) 我在基于spring mvc的应用程序中使用Aspect进行日志记录活动.我正在使用@controller注释来定义我的应用程序中的任何控制器.我在两个不同的包中有两个不同的控制器说
我可以通过使用将方面应用于一个特定的控制器包
<aop:config>
<aop:pointcut id="pointcut1"
expression="execution(* package1.*.*(..))"
id="policy1" />
<aop:aspect ref="aspect1" order="1">
<aop:before pointcut-ref="pointcut1" method="before" arg-names="joinPoint" />
<aop:after-returning returning="returnValue" arg-names="joinPoint, returnValue" pointcut-ref="pointcut1" method="after" />
</aop:aspect>
</aop:config>
<bean id="aspect1" class="com......aspectclass" />
Run Code Online (Sandbox Code Playgroud)
我的问题是如何指定在一个以上的不同包装的表达(*包1. ..(..))**.
现在我宣布为每个包在一个方面的一个单独的一个单独的切入点aop:before
,并aop:after
为每个切入点条目.但我认为这应该是定义多个包切入点的理想方式.
我有一个庞大的Java应用程序。我想拦截所有Java异常并通过电子邮件发送它们。我不能在任何地方添加代码以通过发送代码,try-catch
因此是否可以使用例如Aspect将异常拦截为低级类并获取异常内容?
还是有某种方法可以覆盖某些内部Java类并获取异常有效负载?
有什么可能?
我需要帮助解决Spring和代理问题.
org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为'fooAPIService'的bean必须是[com.foo.clientapi.service.FooAPIService]类型,但实际上是[com.sun.proxy.$ Proxy110]类型
org.springframework.beans.factory.BeanCreationException:创建名为'activityController'的bean时出错:资源依赖注入失败; 嵌套异常是org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为'fooAPIService'的bean必须是[com.foo.clientapi.service.FooAPIService]类型,但实际上是[com.sun.proxy.$ Proxy110]类型
Webapp项目 - >
春天上下文
<context:annotation-config/>
<context:component-scan base-package="com.foo.controller"/>
<aop:aspectj-autoproxy />
<aop:config proxy-target-class="true"/>
<mvc:annotation-driven/>
Run Code Online (Sandbox Code Playgroud)
ActivityController.class
import com.foo.clientapi.service.FooAPIService;
...
@Controller
@RequestMapping(value = "/toto")
public class ActivityController {
@Resource
private FooAPIService fooAPIService;
...
}
Run Code Online (Sandbox Code Playgroud)
另一个项目(微服务) - >
FooAPIService.class
@Path("/foos")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface FooAPIService {
...
}
Run Code Online (Sandbox Code Playgroud)
Jaxrs配置:
<jaxrs:client id="fooAPIService"
address="${toto}"
threadSafe="true"
serviceClass="com.foo.clientapi.service.FooAPIService"
inheritHeaders="true">
...
</jaxrs:client>
Run Code Online (Sandbox Code Playgroud)
版本:aspectjweaver:1.6.10 aspectjrt:1.6.11 cglib:2.2 Spring 3.2.2
为此做一个简洁的标题很难.
无论如何,想象一下我有一个父类:
public class Shape {
public Dimensions getDimensions() {
// Does some generic stuff.
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个派生类来覆盖getDimensions方法:
public class Circle extends Shape {
public Dimensions getDimensions() {
// Does some stuff.
super.getDimensions();
}
}
Run Code Online (Sandbox Code Playgroud)
当我创建一个带有切入点的方面时Shape.getDimensions
,切入点Circle.getDimensions
被调用两次:一次用于Circle.getDimensions
,然后一次调用super.getDimensions
.
切入点看起来像这样: @Pointcut("execution(* Shape.getDimensions(..))")
我在建议中添加了一个黑客来检查声明类型的名称(使用JoinPoint.getSignature().getDeclaringType().getName()
),但我发现它相当粗糙,感觉有点像黑客.我认为必须有一个更好的方法来做到这一点.
在那儿?
如果格式不是很好,请道歉.第一次在这里问一个问题(我通常可以找到答案).
我想要一个具有拦截所有方法调用的方面的JAR,例如
@Aspect
public class CoolAspect {
@Pointcut("execution(public * *(..))")
public void anyPublicMethod() { }
@Before("anyPublicMethod()")
public void advice(JoinPoint point) {
System.out.println("sign:\t" + point.getSignature());
}
}
Run Code Online (Sandbox Code Playgroud)
假设以上是我拥有并希望客户使用的方面。我将其编译为JAR并提供给Maven。
现在,客户端将使用此jar作为依赖项,例如
<dependency>
<groupId>cool-group</groupId>
<artifactId>cool-artifact</artifactId>
<version>1.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
该工件(JAR)具有上述方面。
现在可以通过仅声明Maven依赖项来进行方面工作?
几件重要的事情:
web.xml
。ServletContextListener
。有任何想法吗?
public class CachingInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
// code comes here....
}
}
Run Code Online (Sandbox Code Playgroud)
public class Business : IBusiness
{
public void Add(string a)
{
var t= GetAll();
// code comes here....
}
[CacheAttribute]
public string GetAll()
{
// code comes here....
}
}
Run Code Online (Sandbox Code Playgroud)
public class JustForTest
{
public JustForTest(IBusiness business)
{
//if GetAll is invoked directly caching works fine.
business.GetAll();
//if GetAll is invoked over Add method caching doesn't work.
business.Add();
}
} …
Run Code Online (Sandbox Code Playgroud) C#7引入了本地函数(非常棒!).假设我有以下代码:
using System;
using PostSharp.Aspects;
namespace AspectCS7
{
class Program
{
private static void Main()
{
[MyAspect]
void LocalFunction()
{
Console.WriteLine("Hello Aspect!");
}
LocalFunction();
}
}
[Serializable]
public class MyAspect : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
Console.WriteLine("Entering Aspect");
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码显示编译时错误.是否可以将属性应用于本地函数?