spring自定义注解
2016-04-01 09:38
531 查看
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <aop:aspectj-autoproxy /> <context:component-scan base-package="wjf.maven"> <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect" /> </context:component-scan> </beans>
package wjf.maven; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD, ElementType.PARAMETER }) public @interface DemoAnnotation { //定义注解里的参数,通过default指定默认值 String desc() default "##########无描述信息##########"; }
package wjf.maven; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; //定义一个切面 @Aspect public class DemoProcess { @Around(value = "execution(* wjf.maven.*.*(..)) && @annotation(log)") public Object aroundMethod(ProceedingJoinPoint pjd, DemoAnnotation log ) { Object result = null; System.out.println(log.desc()); try { System.out.println("前置通知"); result = pjd.proceed(); System.out.println("后置通知"); } catch (Throwable e) { System.out.println("异常通知"); } System.out.println("返回通知"); return result; } }
package wjf.maven; import org.springframework.stereotype.Service; @Service public class AopTest { @DemoAnnotation(desc = "我是测试") public void test() { // System.out.println("hello world!!"); } @DemoAnnotation public void test2() { System.out.println("hello world!!"); } }
相关文章推荐
- java5 ReadWriteLock用法--读写锁实现
- struts2中的constant配置详解
- Java - 多线程
- Eclipse SWT开发教程以及一个连连看游戏的代码实现下载
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
- Spring加载完成后调用方法
- java后台以json字符串的形式,将数据返回给$.ajax的问题总结
- 关于华为MAS+JAVA开发时遇到的问题
- Spring中Bean的配置:基于注解的方式
- 自学java的故事
- java.util.concurrent.CyclicBarrier
- Java中的ThreadLocal深入理解
- [置顶] Android开发之Eclipse与Android Studio的java类 作者版权模板
- JAVA读取配置文件
- java反射知识点总结
- jdk环境变量含义极其配置
- Hello, World!
- javax.naming.NoInitialContextException: Need to specify class name in environment or system property
- java泛型
- JDK与JRE及其在Eclipse中的使用