`
fanrey
  • 浏览: 256619 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

AspectJ HelloWorld程序

    博客分类:
  • AOP
 
阅读更多
下载Aspectj: http://www.eclipse.org/aspectj/downloads.php
配置好PATH和CLASSPATH后,可以试试ajc编译器是否work。

D:\java\aspectjDemo>ajc
{0}

        Usage: <options> <source file | @argfile>..

AspectJ-specific options:
        -inpath <list>      use classes in dirs and jars/zips in <list> as sourc
e
                            (<list> uses platform-specific path delimiter)
        -injars <jarList>   use classes in <jarList> zip files as source
                            (<jarList> uses classpath delimiter)
                            deprecated - use inpath instead.
        -aspectpath <list>  weave aspects in .class files from <list> dirs and j
ars/zip into sources
                            (<list> uses classpath delimiter)
        -outjar <file>      put output classes in zip file <file>
        -outxml             generate META-INF/aop.xml
        -outxmlfile <file>  specify alternate destination output of -outxml
        -argfile <file>     specify line-delimited list of source files
        -showWeaveInfo      display information about weaving
        -incremental        continuously-running compiler, needs -sourceroots
                            (reads stdin: enter to recompile and 'q' to quit)
        -sourceroots <dirs> compile all .aj and .java files in <dirs>
                            (<dirs> uses classpath delimiter)
        -crossrefs          generate .ajsym file into the output directory
        -emacssym           generate .ajesym symbol files for emacs support
        -Xlint              same as '-Xlint:warning'
        -Xlint:<level>      set default level for crosscutting messages
                            (<level> may be ignore, warning, or error)
        -Xlintfile <file>   specify properties file to set per-message levels
                            (cf org/aspectj/weaver/XlintDefault.properties)
        -X                  print help on non-standard options

Standard Eclipse compiler options:
Options enabled by default are prefixed with '+'

Classpath options:
    -cp -classpath <directories and zip/jar files separated by ;>
                       specify location for application classes and sources
    -bootclasspath <directories and zip/jar files separated by ;>
                       specify location for system classes
    -d <dir>           destination directory (if omitted, no directory is create
d)
    -d none            generate no .class files
    -encoding <enc>    specify custom encoding for all sources. Each file/direct
ory can override it
                       when suffixed with '['<enc>']' (e.g. X.java[utf8])

Compliance options:
    -1.3               use 1.3 compliance level (implicit -source 1.3 -target 1.
1)
    -1.4             + use 1.4 compliance level
    -1.5               use 1.5 compliance level
    -1.6               use 1.6 compliance level
    -source <version>  set source level (1.3, 1.4, 1.5 or 1.6)
    -target <version>  set classfile target (1.1 to 1.4)

Warning options:
    -deprecation     + deprecation outside deprecated code
    -nowarn            disable all warnings except xlint or declare warning
    -warn:none         disable all warnings except xlint or declare warning
    -warn:<warnings separated by ,>    enable exactly the listed warnings
    -warn:+<warnings separated by ,>   enable additional warnings
    -warn:-<warnings separated by ,>   disable specific warnings
      allDeprecation       deprecation including inside deprecated code
      allJavadoc           invalid or missing javadoc
      assertIdentifier   + 'assert' used as identifier
      charConcat         + char[] in String concat
      conditionAssign      possible accidental boolean assignment
      constructorName    + method with constructor name
      deprecation        + deprecation outside deprecated code
      emptyBlock           undocumented empty block
      fieldHiding          field hiding another variable
      finally            + finally block not completing normally
      indirectStatic       indirect reference to static member
      intfNonInherited   + interface non-inherited method compatibility
      javadoc              invalid javadoc
      localHiding          local variable hiding another variable
      maskedCatchBlock   + hidden catch block
      nls                  string literal lacking non-nls tag //$NON-NLS-<n>$
      noEffectAssign     + assignment without effect
      pkgDefaultMethod   + attempt to override package-default method
      semicolon            superfluous semicolon
      unqualifiedField     unqualified reference to field
      unusedImport       + unused import declaration
      unusedLocal          unread local variable
      unusedPrivate        unused private member declaration
      unusedThrown         unused declared thrown exception
      uselessTypeCheck     unnecessary cast/instanceof operation
      specialParamHiding   constructor or setter parameter hiding another field
      staticReceiver     + non-static reference to static member
      syntheticAccess      synthetic access for innerclass
      tasks(<tags separated by |>) tasks identified by tags inside comments

Debug options:
    -g[:lines,vars,source] custom debug info
    -g:lines,source  + both lines table and source debug info
    -g                 all debug info
    -g:none            no debug info
    -preserveAllLocals preserve unused local vars for debug purpose

Advanced options:
    -log <file>        log to a file
    -proceedOnError    do not stop at first error, dumping class files with prob
lem methods
    -verbose           enable verbose output
    -referenceInfo     compute reference info
    -progress          show progress (only in -log mode)
    -time              display speed information
    -noExit            do not call System.exit(n) at end of compilation (n==0 if
no error)
    -repeat <n>        repeat compilation process <n> times for perf analysis
    @<file>            read command line arguments from file

    -? -help           print this help message
    -v -version        print compiler version
    -showversion       print compiler version and continue

{0}

        Usage: <options> <source file | @argfile>..

AspectJ-specific options:
        -inpath <list>      use classes in dirs and jars/zips in <list> as sourc
e
                            (<list> uses platform-specific path delimiter)
        -injars <jarList>   use classes in <jarList> zip files as source
                            (<jarList> uses classpath delimiter)
                            deprecated - use inpath instead.
        -aspectpath <list>  weave aspects in .class files from <list> dirs and j
ars/zip into sources
                            (<list> uses classpath delimiter)
        -outjar <file>      put output classes in zip file <file>
        -outxml             generate META-INF/aop.xml
        -outxmlfile <file>  specify alternate destination output of -outxml
        -argfile <file>     specify line-delimited list of source files
        -showWeaveInfo      display information about weaving
        -incremental        continuously-running compiler, needs -sourceroots
                            (reads stdin: enter to recompile and 'q' to quit)
        -sourceroots <dirs> compile all .aj and .java files in <dirs>
                            (<dirs> uses classpath delimiter)
        -crossrefs          generate .ajsym file into the output directory
        -emacssym           generate .ajesym symbol files for emacs support
        -Xlint              same as '-Xlint:warning'
        -Xlint:<level>      set default level for crosscutting messages
                            (<level> may be ignore, warning, or error)
        -Xlintfile <file>   specify properties file to set per-message levels
                            (cf org/aspectj/weaver/XlintDefault.properties)
        -X                  print help on non-standard options

Standard Eclipse compiler options:
Options enabled by default are prefixed with '+'

Classpath options:
    -cp -classpath <directories and zip/jar files separated by ;>
                       specify location for application classes and sources
    -bootclasspath <directories and zip/jar files separated by ;>
                       specify location for system classes
    -d <dir>           destination directory (if omitted, no directory is create
d)
    -d none            generate no .class files
    -encoding <enc>    specify custom encoding for all sources. Each file/direct
ory can override it
                       when suffixed with '['<enc>']' (e.g. X.java[utf8])

Compliance options:
    -1.3               use 1.3 compliance level (implicit -source 1.3 -target 1.
1)
    -1.4             + use 1.4 compliance level
    -1.5               use 1.5 compliance level
    -1.6               use 1.6 compliance level
    -source <version>  set source level (1.3, 1.4, 1.5 or 1.6)
    -target <version>  set classfile target (1.1 to 1.4)

Warning options:
    -deprecation     + deprecation outside deprecated code
    -nowarn            disable all warnings except xlint or declare warning
    -warn:none         disable all warnings except xlint or declare warning
    -warn:<warnings separated by ,>    enable exactly the listed warnings
    -warn:+<warnings separated by ,>   enable additional warnings
    -warn:-<warnings separated by ,>   disable specific warnings
      allDeprecation       deprecation including inside deprecated code
      allJavadoc           invalid or missing javadoc
      assertIdentifier   + 'assert' used as identifier
      charConcat         + char[] in String concat
      conditionAssign      possible accidental boolean assignment
      constructorName    + method with constructor name
      deprecation        + deprecation outside deprecated code
      emptyBlock           undocumented empty block
      fieldHiding          field hiding another variable
      finally            + finally block not completing normally
      indirectStatic       indirect reference to static member
      intfNonInherited   + interface non-inherited method compatibility
      javadoc              invalid javadoc
      localHiding          local variable hiding another variable
      maskedCatchBlock   + hidden catch block
      nls                  string literal lacking non-nls tag //$NON-NLS-<n>$
      noEffectAssign     + assignment without effect
      pkgDefaultMethod   + attempt to override package-default method
      semicolon            superfluous semicolon
      unqualifiedField     unqualified reference to field
      unusedImport       + unused import declaration
      unusedLocal          unread local variable
      unusedPrivate        unused private member declaration
      unusedThrown         unused declared thrown exception
      uselessTypeCheck     unnecessary cast/instanceof operation
      specialParamHiding   constructor or setter parameter hiding another field
      staticReceiver     + non-static reference to static member
      syntheticAccess      synthetic access for innerclass
      tasks(<tags separated by |>) tasks identified by tags inside comments

Debug options:
    -g[:lines,vars,source] custom debug info
    -g:lines,source  + both lines table and source debug info
    -g                 all debug info
    -g:none            no debug info
    -preserveAllLocals preserve unused local vars for debug purpose

Advanced options:
    -log <file>        log to a file
    -proceedOnError    do not stop at first error, dumping class files with prob
lem methods
    -verbose           enable verbose output
    -referenceInfo     compute reference info
    -progress          show progress (only in -log mode)
    -time              display speed information
    -noExit            do not call System.exit(n) at end of compilation (n==0 if
no error)
    -repeat <n>        repeat compilation process <n> times for perf analysis
    @<file>            read command line arguments from file

    -? -help           print this help message
    -v -version        print compiler version
    -showversion       print compiler version and continue

下面是最简单一个helloworld程序。

1. 最简单的hello程序。
public class Hello {

     public void sayHello(){
       System.out.println("Hello AspectJ.");
    }

    public static void main(String[] args) {
       Hello hello = new Hello();
       hello.sayHello();
    }
}

编译运行结果:
D:\java\aspectjDemo>javac Hello.java

D:\java\aspectjDemo>java Hello
Hello AspectJ.


2. 不改变Hello.java文件,在调用sayHello前后打log。

public aspect TestAspect{

    void around():call(void Hello.sayHello()){
       System.out.println("begin transaction....");
       proceed();//代表调用原来的sayHello()方法
        System.out.println("end transaction....");
    }
}

编译运行结果:
D:\java\aspectjDemo>dir *
驱动器 D 中的卷是 SOFTWARE
卷的序列号是 8236-DBD6

D:\java\aspectjDemo 的目录

2011/10/11  22:55    <DIR>          .
2011/10/11  22:55    <DIR>          ..
2011/10/11  22:56               249 Hello.java
2011/10/11  22:57               252 TestAspect.java
               2 个文件            501 字节
               2 个目录 80,415,948,800 可用字节

D:\java\aspectjDemo>ajc *.java

D:\java\aspectjDemo>java Hello
begin transaction....
Hello AspectJ.
end transaction....

D:\java\aspectjDemo>
分享到:
评论

相关推荐

    webAspectTest

    【描述】提到的"aspectJ的hello world程序,web方面的",意味着它将提供一个基础的入门示例,展示AspectJ如何与Web应用程序(如基于Servlet的)协同工作。通常,这个Hello World程序会包含一个简单的切面,可能在...

    Aspectj框架实战案例详解

    通过这些定义,AspectJ可以在编译期、加载期或运行期为Java程序动态地添加额外的行为。该框架实战案例详解主要阐述了AspectJ框架的实战应用,并通过具体案例讲述了框架配置、使用、编译等操作技巧。 首先,环境配置...

    一个hellword例子

    标题中的"一个HelloWorld例子"指的是使用AspectJ编写的简单示例程序,通常用于初学者了解和学习AspectJ的基本用法。这个例子将帮助我们理解如何定义切面、通知(advice)以及它们如何影响应用程序的行为。 描述中...

    JAVA选择题1.docx

    17. 执行JAVA语句“if (1 || false) System.out.print(“Hello world!”);”显示器上将显示(B):什么都没显示。 18. 执行下列JAVA语句:1.int x = 0;2.while (x )3. System.out.print("*" ); x++;显示器将显示(A...

    spring2-aop.pdf

    - **AspectJ 版的 HelloWorld 示例**:通过一个简单的示例来演示如何使用 AspectJ 编写和织入切面。 - **AspectJ 中相关语法**:详细介绍 AspectJ 语言的各种特性和语法,如声明切面、切点表达式等。 - **一个简单的...

    spring2-aop入门实例教程

    - **HelloWorld示例**:通过编写一个简单的AspectJ切面来展示基本的AOP功能。 - **AspectJ语法**:介绍AspectJ中切面、切入点、通知等元素的基本语法。 #### 四、Spring AOP示例 - **定义业务组件**:创建具体的...

    简化企业级开发

    - **代码示例**:在上面的部分内容中,提到了如何使用Spring来创建一个简单的Hello World程序。具体步骤包括: - 创建Spring的配置文件(如`beans.xml`)。 - 在配置文件中定义Bean及其属性。 - 使用`...

    springMVC搭建实例

    例如,你可以创建一个简单的Hello World Controller: ```java @Controller public class HelloWorldController { @RequestMapping("/hello") public String sayHello(Model model) { model.addAttribute(...

    javaagent+javassist

    在提供的压缩包`helloWorld`中,可能包含了演示如何使用javaagent+javassist的示例代码。导入IDEA后,可以进一步学习和实践如何结合这两个工具进行字节码操作和动态增强。 总的来说,javaagent和javassist的结合...

    跟我学spring3(1-7)

    15. **AOP的HelloWorld** 通过创建切面、定义切入点表达式和通知,实现简单的AOP示例。 16. **基于Schema的AOP** 使用Spring XML配置文件定义切面,包括切入点、通知类型和执行顺序。 17. **基于@AspectJ的AOP**...

    Spring AOP精讲

    System.out.println("Hello World! (by " + this.getClass().getName() + ")"); } } ``` 在这个例子中,`TestBeforeAdvice`实现了`MethodBeforeAdvice`接口,并覆盖了`before`方法。该方法会在目标方法执行之前...

    springMVC学习

    AOP的HelloWorld - 实现一个简单的AOP示例,比如添加日志记录功能,可以帮助理解AOP的基本用法。 #### 3. 基于Schema的AOP - 通过XML配置文件来定义切面、切入点和通知(Advice)。 - 可以使用`&lt;aop:config&gt;`、`...

    Spring3开发实战

    - **Hello World示例**:通过创建一个简单的Java类并使用Spring进行管理来演示如何使用Spring框架。 - **配置文件**:XML配置文件是Spring早期的主要配置方式,介绍了如何在XML中定义bean以及它们之间的依赖关系。 ...

    spring 4.2.4.RELEASE核心jar包

    Spring框架是中国最流行的Java企业级应用开发框架,它极大地简化了Java应用程序的构建,特别是对于基于Web的应用。这里提到的"spring 4.2.4.RELEASE核心jar包"是Spring框架的一个特定版本,用于支持4.2.4.RELEASE中...

    Java的日子:与Java混在一起(基本到高级)

    在这个文件夹里,你可能会找到不同难度级别的练习,从简单的Hello World程序到复杂的多线程或网络编程示例。 在学习过程中,你将深入理解Java的基础语法,如变量、数据类型、控制结构(条件语句、循环)、函数和类...

    springmvc插件

    model.addAttribute("message", "Hello World!"); return "hello"; } } ``` 6. **编写视图文件**: - 在 WEB-INF/views 目录下创建 hello.jsp 文件: ```html ; charset=UTF-8" pageEncoding="UTF-8"%&gt; &lt;!...

    hibernate和spring MVC配置整合

    通过`springMVC_01_helloworld`示例,你可以创建一个简单的Hello World应用,验证配置是否正确。设置一个简单的Controller,返回一个视图,然后在浏览器中访问,确认Spring MVC能够正常工作。 通过以上步骤,你已经...

    跟我学spring3

    #### 6.2 AOP的HelloWorld - **示例**:使用AOP来记录日志或添加缓存等功能。 - **配置**:可以使用XML或注解的方式配置AOP切面。 #### 6.3 基于Schema的AOP - **配置**:使用XML Schema来定义AOP切面。 - **示例...

Global site tag (gtag.js) - Google Analytics