为了使用Spring AOP实现Logging,我遵循了这些简单的步骤.但它似乎无法正常工作.任何帮助都会有用
1)创建了MyLoggingAspect类
import org.aspectj.lang.ProceedingJoinPoint;
public class MyLoggingAspect
{
public MyLoggingAspect() {
super();
System.out.println("Instantiated MyLoggingAspect");
}
public Object log(ProceedingJoinPoint call) throws Throwable
{
System.out.println("from logging aspect: entering method [" + call.toShortString()
+"] with param:"+call.getArgs()[0] );
Object point = call.proceed();
System.out.println("from logging aspect: exiting method [" + call.toShortString()
+ "with return as:" +point);
return point;
}
Run Code Online (Sandbox Code Playgroud)
}
2)创建了一个我想要记录的类(TixServiceImpl)
public class TixServiceImpl implements TixService{
@Override
public void calculateSomething() {
String s = "did some calculation..";
System.out.println(s);
}
@Override …Run Code Online (Sandbox Code Playgroud) 我在一个嵌入式平台(mipsel体系结构,Linux 2.6内核)上,我需要在两个闭源进程(路由器固件)之间监视IPC,以便对某个事件做出反应(由于DSL重新连接,动态IP更改).到目前为止,我通过strace发现的是,无论何时IP发生变化,DSL守护进程都会将特殊消息写入绑定到特定文件名的UNIX域套接字.该消息由另一个守护程序使用.
现在这是我的要求:我想监视通过该特定UNIX域套接字的数据流,并在检测到某个消息时触发事件(调用shell脚本).我尝试使用inotify监视文件名,但它不适用于套接字文件.我知道我可以一直运行strace,过滤其输出并对过滤后的日志文件中的更改做出反应,但这样做太重了,因为strace确实会降低系统速度.我也知道我可以通过cron轮询IP地址,但是我想要一个看门狗,而不是一个轮询解决方案.我有兴趣了解是否有一个工具可以专门监视UNIX域套接字并对预定义方向流动的特定消息做出反应.我想象类似于inotifywait的东西,即工具应该等待某个事件,然后退出,这样我就可以对事件作出反应并循环回到再次启动工具,等待同一类型的下一个事件.
是否有任何现有的Linux工具能够做到这一点?或者是否有一些简单的C代码用于独立二进制文件,我可以在我的平台上编译(uClibc,而不是glibc)?我不是C专家,但能够运行makefile.使用shell中的二进制文件没问题,我对shell编程了解得足够多.
我的aspectj类没有被编译,尽管它们用@Aspect注释并且驻留在.aj扩展文件中.
该项目是Maven JBoss AS 7 EAR Archetype.
[INFO] --- aspectj-maven-plugin:1.4:compile (default-cli) @ hms ---
[WARNING] Not executing aspectJ compiler as the project is not a Java classpath-capable package
[INFO] --- aspectj-maven-plugin:1.4:compile (default-cli) @ hms-ejb ---
[WARNING] bad version number found in C:\Users\Oh Chin Boon\.m2\repository\org\aspectj\aspectjrt\1.5.4\aspectjrt-1.5.4.jar expected 1.6.11 found 1.5.4
[WARNING] advice defined in sg.java.hms.aspect.AbstractLoggingAspect has not been applied [Xlint:adviceDidNotMatch]
[WARNING] advice defined in sg.java.hms.aspect.DefaultLoggingAspect has not been applied [Xlint:adviceDidNotMatch]
[WARNING] advice defined in sg.java.hms.aspect.AbstractLoggingAspect has not been applied [Xlint:adviceDidNotMatch]
Run Code Online (Sandbox Code Playgroud)
编辑:pom.xml片段
<plugin> …Run Code Online (Sandbox Code Playgroud) 我在 IntelliJ 中使用了 ajc 编译器,以及 aspectj-maven-plugin,我在其中声明了以下排除项:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>**/EmployeeAccessAspect.java</exclude>
</excludes>
<complianceLevel>1.8</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
当我使用 编译时mvn compile,一切都很好,并且EmployeeAccessAspect方面被正确忽略。
但是,如果我Make是 IntelliJ 中的项目,它只会编织它找到的所有方面(并完全忽略 maven 排除项)。
我总是可以破解 ajc 编译器选项或方面路径来实现我想要的,但是如果我出于某种原因必须排除一个方面,它会让我改变两个地方。
有什么办法可以让maven插件和ajc编译同步吗?我正在使用 IntelliJ 15。
我始终认为,@Scheduled作品代理所述全豆,同样的方式@Async,@Transactional等做。所以我很惊讶以下内容就像一个魅力:
@Component
public static class Bean {
@Scheduled(fixedRate = 1000)
private void scheduled() {
System.out.println("Yo");
}
}
Run Code Online (Sandbox Code Playgroud)
他们有没有改变什么,或者从一开始就是这样?谢谢。
我正在使用以下示例代码来理解 AspectJ:
public class Account {
int balance = 20;
public boolean withdraw(int amount) {
if (balance < amount) {
return false;
}
balance = balance - amount;
return true;
}
public static void main(String[] args) {
Account acc = new Account();
acc.withdraw(5);
Account acc2 = new Account();
acc2.withdraw(25);
}
}
Run Code Online (Sandbox Code Playgroud)
以及以下方面:
public aspect AccountAspect {
pointcut callWithDraw(int amount, Account acc) :
call(boolean Account.withdraw(int)) && args(amount) && target(acc);
before(int amount, Account acc) : callWithDraw(amount, acc) {
System.out.printf("[before] withDraw, current balance …Run Code Online (Sandbox Code Playgroud) 我之前曾使用 AOP 风格的代码将逻辑与日志记录分开,并且对结果非常满意。我认识到对 AOP 的看法各不相同,但我想在 Elixir 中找到一个解决方案,即使我最终没有在产品中使用它。
我见过的最接近的例子是 ExUnit 内部的设置回调,它允许在每个测试运行之前执行代码;我想做类似的事情,但无法通过 ExUnit 源代码来掌握那里的直觉。
以代码形式:
defmodule Project.Logic do
LoggingInjection.inject Project.Logging
def work_do_stuff(arg) do
#...
#returns some_result
end
end
Run Code Online (Sandbox Code Playgroud)
在单独的代码文件中:
defmodule Project.Logging do
#called just before Project.Logic.work_do_stuff with the same args
def before_work_do_stuff(arg) do
Log.write("about to work_do_stuff with #{inspect arg}")
end
# def after_work_do_stuff(some_result) implicitly defined as no-op,
# but could be overridden.
end
Run Code Online (Sandbox Code Playgroud)
最后,真正的问题是:启用这个魔法的代码是什么?
defmodule LoggingInjection do
defmacro inject(logging_module) do
#What goes here?
end
end
Run Code Online (Sandbox Code Playgroud) 我有一个使用编译方面并在编译时编织它们的项目。我想添加 Lombok,但不幸的是 Lombok 不支持 AJC。由于该项目本身没有任何方面的来源,因此在使用 Javac+Lombok 编译后,我将 AspectJ Maven 插件配置为进行编译后编织。
这是 AspectJ Maven 插件的配置:
<forceAjcCompile>true</forceAjcCompile>
<sources/>
<weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
Run Code Online (Sandbox Code Playgroud)
它在 Maven Compiler 插件编译后立即附加到编译阶段。这样 Lombok + Javac 将首先被调用,然后 AJC 将在 Javac 生成的类文件上执行编织。
在 javac 生成的类上执行字节码编织时是否有任何限制/缺点?
也许有更好的方法可以让 Maven+Lombok+Aspects+Idea 一起工作而不会出现问题。
这是一个最小的示例项目:https : //github.com/Psimage/aspectj-and-lombok
我们不能在一个gardle 项目中一起运行使用Junit 5和Spock 框架编写的测试用例?
我们尝试将https://www.baeldung.com/junit-5-gradle 中给出的依赖项添加到我们的 gradle 文件中。Gradle 版本是 4.10.3 和 Junit 5。下面是我的 build.gradle 文件
apply plugin: 'groovy'
apply plugin: 'java'
repositories {
mavenCentral()
maven {
url "http://repo.fusesource.com/nexus/content/groups/public/"
}
maven {
url "https://repository.jboss.org/nexus/content/groups/public"
}
jcenter()
}
dependencies {
compile group: 'com.google.inject', name: 'guice', version: '4.2.2'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
testCompile(
'org.codehaus.groovy:groovy-all:2.4.8',
'org.spockframework:spock-core:1.0-groovy-2.4',
'org.jmockit:jmockit:1.8',
'junit:junit:4.12'
)
testRuntime(
'cglib:cglib:2.2.2',
'com.athaydes:spock-reports:1.2.7'
)
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testCompileOnly 'junit:junit:4.12'
}
test …Run Code Online (Sandbox Code Playgroud) Feb 02, 2022 12:58:03 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@71318ec4: startup date [Wed Feb 02 12:58:03 IST 2022]; root of context hierarchy
Feb 02, 2022 12:58:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Feb 02, 2022 12:58:04 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'triangle' defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Unexpected AOP exception; …Run Code Online (Sandbox Code Playgroud)