论坛首页 Java企业应用论坛

我对AOP的理解

浏览 34774 次
该帖已经被评为精华帖
作者 正文
   发表时间:2013-01-14  
jinnianshilongnian 写道
raykcn 写道
jinnianshilongnian 写道

4、配置文件
    <bean id="actpt" class="com.tes.Actpt"/>        
    <bean class="com.tes.Ti"/>
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

不理解,我没有用spring,只是想用aspectj,所以没有用配置文件。
楼主看一下这个博文:
http://www.cnblogs.com/louzj82/archive/2010/04/28/1722967.html
并没有用到配置文件。

继续求解!!!


1切入点错了,应该为    @Around("execution(@com.tes.Asit * *(..))") 
2 环绕通知也不是那么写
都是语法错误


貌似我们聊的不在一个频道...怎么谈到环绕通知了,可能是我没表达清晰
1. 我不想用spring,只想单独使用aspect
2. 我想实现博文中的效果,博文中用3个类实现了切入效果。
楼主如果能帮忙,非常感激,其他的细节或(甚解)容后再议,不知道我这样能否表达清晰???
0 请登录后投票
   发表时间:2013-01-14  
这样三个类:
package com.tes;

public class HelloWorld {
	public void say() {
		System.out.println("HelloWorld!");
	}
}

package com.tes;

public class MainTest {
	public static void main(String[] args) {
		HelloWorld helloWorld = new HelloWorld();
		helloWorld.say();
	}
}

package com.tes;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AspectjAn {
	@Around("execution(@com.tes.HelloWorld * *(..))")
	public Object around(ProceedingJoinPoint pjp) throws Throwable {  
        System.out.println("run Aspect...");  
        return pjp.proceed();
    }  
}


这样的三个类不能切入,我想知道错在哪里? 真心没有spring的事。
0 请登录后投票
   发表时间:2013-01-14  
在eclipse中开发aspectj

1、安装 AspectJ Development Tools
2、新建项目时,选择AspectJ Project
3、还是我上边回的测试用例,正常工作,
4、测试,直接new一个测试即可
package com.tes;
public class Actpt {

	@Asit
	public void test() {
		System.out.println("!!!!!!!111");
	}
	
	public static void main(String[] args) {
		new Actpt().test();
	}
}
0 请登录后投票
   发表时间:2013-01-15   最后修改:2013-01-15
现在没环境,今天测试,报告结果
0 请登录后投票
   发表时间:2013-01-15   最后修改:2013-01-15
鞠躬感谢楼主,经测试 是我没有装ajdt的原因。。比较丢人。
不过貌似部署到tomcat下 不行。

想请教一下 web项目 该如何调试 处理呢 ?
0 请登录后投票
   发表时间:2013-01-16  
我在web项目下用junit单元测试或直接 system.out.println()时,是测试不出来aspectj的运行效果的。

请教一下楼主:
不知道楼主在web项目中怎么处理这个情况?
0 请登录后投票
   发表时间:2013-01-16  
raykcn 写道
我在web项目下用junit单元测试或直接 system.out.println()时,是测试不出来aspectj的运行效果的。

请教一下楼主:
不知道楼主在web项目中怎么处理这个情况?

右击项目-->Configure--->Convert to Aspectj Project
0 请登录后投票
   发表时间:2013-01-16  
呵呵 开涛有些烦我了吧? 不过我还想问 :)
我现在的环境是 eclipse+m2eclipse 这个环境该怎样在maven下调试 aspectj 呢?

我google了几圈,发现在pom.xml中:
<plugin>
					<groupId>org.codehaus.mojo</groupId>
					<artifactId>aspectj-maven-plugin</artifactId>
					<version>1.4</version>
					<configuration>
						<source>1.6</source>
						<target>1.6</target>
						<XnoInline>true</XnoInline>
						<showWeaveInfo>true</showWeaveInfo>
					</configuration>
				</plugin>


我技术有限,测试了,怎么也不出现ajdt那样的方法标记,就是不能织入,鞠躬求教!
0 请登录后投票
   发表时间:2013-01-17  
raykcn 写道
呵呵 开涛有些烦我了吧? 不过我还想问 :)
我现在的环境是 eclipse+m2eclipse 这个环境该怎样在maven下调试 aspectj 呢?

我google了几圈,发现在pom.xml中:
<plugin>
					<groupId>org.codehaus.mojo</groupId>
					<artifactId>aspectj-maven-plugin</artifactId>
					<version>1.4</version>
					<configuration>
						<source>1.6</source>
						<target>1.6</target>
						<XnoInline>true</XnoInline>
						<showWeaveInfo>true</showWeaveInfo>
					</configuration>
				</plugin>


我技术有限,测试了,怎么也不出现ajdt那样的方法标记,就是不能织入,鞠躬求教!

如果用maven的话,需要指定插件在编译时执行:
<executions>
          <execution>
              <goals>
                  <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
              </goals>
          </execution>
     </executions>

http://blog.csdn.net/zhjinfeng/article/details/7532939
1 请登录后投票
   发表时间:2013-01-17   最后修改:2013-01-17
jinnianshilongnian 写道

如果用maven的话,需要指定插件在编译时执行:
<executions>
          <execution>
              <goals>
                  <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
              </goals>
          </execution>
     </executions>

http://blog.csdn.net/zhjinfeng/article/details/7532939


两种情况:
第一种:
我copy的您给我的连接中的代码,加入了编译执行的pom代码,还是不行。
第二种:
不需要插件,我把maven项目按照您之前提到的 Configure-->Convert to Aspectj 这样是可以运行了,不过eclipse的项目图标上放变成了m和aj重合的标志,就是说2个标签.不知道最后打包时,或其他方面是否会有影响。

还是想用第一种方法实现.......
其他代码和上面说的一样,maven后,我的pom是这样的,开涛老师帮我看看 嘿嘿:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>AspectTest</groupId>
	<artifactId>aspectTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.7.1</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.7.1</version>
		</dependency>
	</dependencies>
	<build>
		<pluginManagement>
			<plugins>
				<!-- compiler插件, 设定JDK版本 -->
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<version>3.0</version>
					<configuration>
						<source>1.6</source>
						<target>1.6</target>
						<showWarnings>true</showWarnings>
					</configuration>
				</plugin>
				<plugin>
					<groupId>org.codehaus.mojo</groupId>
					<artifactId>aspectj-maven-plugin</artifactId>
					<version>1.4</version>
					<executions>
						<execution>
							<goals>
								<goal>compile</goal>
								<goal>test-compile</goal>
							</goals>
						</execution>
					</executions>
					<configuration>
						<outxml>true</outxml>
						<source>1.6</source>
						<target>1.6</target>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics