Spring AOP与IOC以及自定义注解
Spring AOP實現日志服務
pom.xml需要的jar
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.4</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.2.4.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.4.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.2.4.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.4.RELEASE</version>
</dependency>
<dependency><groupId>cglib</groupId><artifactId>cglib-nodep</artifactId><version>3.2.0</version>
</dependency>
<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.8</version>
</dependency>
<dependency><groupId>aopalliance</groupId><artifactId>aopalliance</artifactId><version>1.0</version>
</dependency>
<dependency><groupId>commons-collections</groupId><artifactId>commons-collections</artifactId><version>3.2.2</version>
</dependency>
<dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version>
</dependency>
一.AOP用法一配置XML
1.Before Advice
Before Advice會在目標方法執行之前被調用,可以通過實現org.springframework.aop.MethodBeforeAdvice接口實現。
LogBeforeAdvice.java
package com.blog.csdn.net.unix21;import java.lang.reflect.Method;
import java.util.logging.Level;
import org.springframework.aop.MethodBeforeAdvice;
import java.util.logging.Logger;/**** @author http://blog.csdn.net/unix21/*/
public class LogBeforeAdvice implements MethodBeforeAdvice {private Logger logger=Logger.getLogger(this.getClass().getName());public void before(Method method,Object[] args,Object target) throws Throwable{logger.log(Level.INFO,"日志信息>>>method starts..."+method);}
}
beans-config.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<bean id="LogBeforeAdvice" class="blog.csdn.net.unix21.LogBeforeAdvice"/>
<bean id="HelloSpeaker" class="blog.csdn.net.unix21.HelloSpeake"/>
<bean id="HelloProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="proxyInterfaces" value="blog.csdn.net.unix21.IHello"/><property name="target" ref="HelloSpeaker"/><property name="interceptorNames"><list><value>LogBeforeAdvice</value></list></property>
</bean>
</beans>
接口
IHello.java
package blog.csdn.net.unix21;public interface IHello {
public void hello(String name);
}
HelloSpeaker.java
package blog.csdn.net.unix21;public class HelloSpeaker implements IHello {public void hello(String name){System.out.println("Hello, "+name);}
}
測試Before Advice
SpringAOP 前置增強 后置增強 編程式 需XML配置
ApplicationContext context=new ClassPathXmlApplicationContext("beans-config.xml");IHello h=(IHello)context.getBean("helloProxy");h.hello("blog.csdn.net.unix21")
運行成功使用AOP的方式打印日志信息:
參考:《Spring 2.0技術手冊》
Spring 3.1編寫AOP時需要導入的倚賴jar包匯總
菜鳥學SSH(七)——Spring jar包詳解
2.After Advice
After Advice會在目標方法執行之后被調用,可以通過實現org.springframework.aop.AfterReturningAdvice接口來實現
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.aop.AfterReturningAdvice;public class LogAfterAdvice implements AfterReturningAdvice {
private Logger logger=Logger.getLogger(this.getClass().getName());public void afterReturning(Object object, Method method, Object[] args, Object target) throws Throwable {logger.log(Level.INFO,"日志信息<<<method ends..."+method);}
}
修改beans-config.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<bean id="LogBeforeAdvice" class="blog.csdn.net.unix21.LogBeforeAdvice"/>
<bean id="HelloSpeaker" class="blog.csdn.net.unix21.HelloSpeake"/>
<bean id="HelloProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="proxyInterfaces" value="blog.csdn.net.unix21.IHello"/><property name="target" ref="HelloSpeaker"/><property name="interceptorNames"><list><value>LogBeforeAdvice</value><value>LogAfterAdvice</value></list></property>
</bean>
</beans>
二.AOP用法二編程式不配置XML
SpringAOP 前置增強 后置增強 編程式 無需XML配置
ProxyFactory pf=new ProxyFactory();
pf.setTarget(new TestModel());
pf.addAdvice(new LogBeforeAdvice());
pf.addAdvice(new LogAfterAdvice());
TestModel gt=(TestModel)pf.getProxy();
return gt.add(...);效果是一樣的。
三.Java自定義注解和運行時靠反射獲取注解
還是日志信息,我們希望運行的時候可以獲取自定義注解
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;@Retention(RetentionPolicy.RUNTIME) // 注解會在class字節碼文件中存在,在運行時可以通過反射獲取到
@Target({ElementType.FIELD,ElementType.METHOD})//定義注解的作用目標**作用范圍字段、枚舉的常量/方法
@Documented//說明該注解將被包含在javadoc中
public @interface FieldMeta {/** * 是否為序列號 * @return */ boolean id() default false; /** * 字段名稱 * @return */ String name() default ""; /** * 是否可編輯 * @return */ boolean editable() default true; /** * 是否在列表中顯示 * @return */ boolean summary() default true; /** * 字段描述 * @return */ String description() default ""; /** * 排序字段 * @return */ int order() default 0;
}
聲明注解
需要注意如果一個類是接口的實現,那么這種注解需要寫接口方法上,例如AOP的調用方式一,對于AOP的調用方式二可以把注解寫類的方法上即可。
調用
if (method.isAnnotationPresent(FieldMeta.class)) {FieldMeta myAnnotation = method.getAnnotation(FieldMeta.class);System.out.println("注解:" + myAnnotation.description() + " 類:"+target.getClass().getName()+" 方法:" + method.getName());
}
參考:Java自定義注解和運行時靠反射獲取注解
總結
以上是生活随笔為你收集整理的Spring AOP与IOC以及自定义注解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Tsar安装使用
- 下一篇: shell语法以及监控进程不存在重启