spring aop兩種設置裝備擺設方法。本站提示廣大學習愛好者:(spring aop兩種設置裝備擺設方法)文章只能為提供參考,不一定能成為您想要的結果。以下是spring aop兩種設置裝備擺設方法正文
第一種:注解設置裝備擺設AOP
注解設置裝備擺設AOP(應用 AspectJ 類庫完成的),年夜致分為三步:
1. 應用注解@Aspect來界說一個切面,在切面中界說切入點(@Pointcut),告訴類型(@Before, @AfterReturning,@After,@AfterThrowing,@Around).
2. 開辟須要被攔阻的類。
3. 將切面設置裝備擺設到xml中,固然,我們也能夠應用主動掃描Bean的方法。如許的話,那就交由Spring AoP容器治理。
別的須要援用 aspectJ 的 jar 包: aspectjweaver.jar aspectjrt.jar
實例:
User.java
package com.bjsxt.model;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
/**
*接口類
*/
package com.bjsxt.dao;
import com.bjsxt.model.User;
public interface UserDAO {
public void save(User user);
}
完成接口:
package com.bjsxt.dao.impl;
import org.springframework.stereotype.Component;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
@Component("u")
public class UserDAOImpl implements UserDAO {
public void save(User user) {
System.out.println("user save11d!");
/*throw new RuntimeException("exception");*/ //拋異常
}
}
操作類:
package com.bjsxt.service;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
@Component("userService")
public class UserService {
private UserDAO userDAO;
public void init() {
System.out.println("init");
}
public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
@Resource(name="u")
public void setUserDAO( UserDAO userDAO) {
this.userDAO = userDAO;
}
public void destroy() {
System.out.println("destroy");
}
}
參加aop
package com.bjsxt.aop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.bjsxt.service..*.add(..))")
public void myMethod(){};
/*@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.model.User))")*/
@Before("myMethod()")
public void before() {
System.out.println("method staet");
}
@After("myMethod()")
public void after() {
System.out.println("method after");
}
@AfterReturning("execution(public * com.bjsxt.dao..*.*(..))")
public void AfterReturning() {
System.out.println("method AfterReturning");
}
@AfterThrowing("execution(public * com.bjsxt.dao..*.*(..))")
public void AfterThrowing() {
System.out.println("method AfterThrowing");
}
}
設置裝備擺設文件
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd "><!-- 要添加最初2行 --> <context:annotation-config /> <context:component-scan base-package="com.bjsxt"/> <!-- 主動掃描 --> <aop:aspectj-autoproxy/> <!-- 要添加本行 --> </beans>
測試類:
package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bjsxt.model.User;
//Dependency Injection
//Inverse of Control
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = (UserService)ctx.getBean("userService");
System.out.println(service.getClass());
service.add(new User());
System.out.println("###");
ctx.destroy();
}
}
成果:
class com.bjsxt.service.UserService$$EnhancerByCGLIB$$7b201784 method staet user save11d! method AfterReturning method after ###
留意:
@Aspect:意思是這個類為切面類
@Componet:由於作為切面類須要 Spring 治理起來,所以在初始化時就須要將這個類初始化參加 Spring 的治理;
@Befoe:切入點的邏輯(Advice)
execution…:切入點語法
第二種:xml設置裝備擺設aop
實例同上:只是設置裝備擺設文件分歧
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd "><!-- 要添加最初2行 --> <context:annotation-config /> <context:component-scan base-package="com.bjsxt"/> <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean> <aop:config> <aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))" id="servicePointcut"/> <aop:aspect id="logAspect" ref="logInterceptor"> <aop:before method="before" pointcut-ref="servicePointcut" /> </aop:aspect> </aop:config> </beans>
上面的<beans>是Spring的設置裝備擺設標簽,beans外面幾個主要的屬性:
xmlns:
是默許的xml文檔解析格局,即spring的beans。地址是http://www.springframework.org/schema/beans。
經由過程設置這個屬性,一切在beans外面聲明的屬性,可以直接經由過程<>來應用,好比<bean>等等。
xmlns:xsi:
是xml須要遵照的標准,經由過程URL可以看到,是w3的同一標准,前面經由過程xsi:schemaLocation來定位一切的解析文件。
xmlns:aop:
這個是重點,是我們這裡須要應用到的一些語義標准,與面向切面AOP相干。
xmlns:tx:
Spring中與事務相干的設置裝備擺設內容。
一個XML文件,只能聲明一個默許的語義解析的標准。
例如下面的xml中就只要beans一個是默許的,其他的都須要經由過程特定的標簽來應用,好比aop,它本身有許多的屬性,假如要應用,後面就必需加上aop:xxx才可以。好比下面的aop:config。
相似的,假如默許的xmlns設置裝備擺設的是aop相干的語義解析標准,那末在xml中便可以直接寫config這類標簽了。
以上就是spring aop兩種設置裝備擺設方法,年夜家懂得了嗎?以後還有更多關於spring aop兩種設置裝備擺設方法的文章和年夜家分享,必定要持續存眷哦