最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring aop兩種配置方式

 更新時(shí)間:2015年09月21日 16:32:38   投稿:lijiao  
這篇文章主要為大家詳細(xì)介紹了spring aop兩種配置方式,主要是注解配置AOP和xml配置aop,需要的朋友可以參考下

第一種:注解配置AOP
注解配置AOP(使用 AspectJ 類(lèi)庫(kù)實(shí)現(xiàn)的),大致分為三步:
1. 使用注解@Aspect來(lái)定義一個(gè)切面,在切面中定義切入點(diǎn)(@Pointcut),通知類(lèi)型(@Before, @AfterReturning,@After,@AfterThrowing,@Around).
2. 開(kāi)發(fā)需要被攔截的類(lèi)。
3. 將切面配置到xml中,當(dāng)然,我們也可以使用自動(dòng)掃描Bean的方式。這樣的話,那就交由Spring AoP容器管理。

另外需要引用 aspectJ 的 jar 包: aspectjweaver.jar aspectjrt.jar

實(shí)例:

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; 
 } 
} 
/** 
*接口類(lèi) 
*/ 
package com.bjsxt.dao; 
import com.bjsxt.model.User; 
 
 
public interface UserDAO { 
 public void save(User user); 
} 

實(shí)現(xiàn)接口:

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");*/ //拋異常 
 } 
 
}

操作類(lèi):

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"/> <!-- 自動(dòng)掃描 --> 
 <aop:aspectj-autoproxy/> <!-- 要添加本行 --> 
</beans> 

測(cè)試類(lèi):

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(); 
 
 } 
 
} 

結(jié)果:

class com.bjsxt.service.UserService$$EnhancerByCGLIB$$7b201784
method staet
user save11d!
method AfterReturning
method after
###

注意:

@Aspect:意思是這個(gè)類(lèi)為切面類(lèi)
@Componet:因?yàn)樽鳛榍忻骖?lèi)需要 Spring 管理起來(lái),所以在初始化時(shí)就需要將這個(gè)類(lèi)初始化加入 Spring 的管理;
@Befoe:切入點(diǎn)的邏輯(Advice)
execution…:切入點(diǎn)語(yǔ)法

第二種:xml配置aop

實(shí)例同上:只是配置文件不同

<?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的配置標(biāo)簽,beans里面幾個(gè)重要的屬性:

xmlns:

是默認(rèn)的xml文檔解析格式,即spring的beans。地址是http://www.springframework.org/schema/beans。

通過(guò)設(shè)置這個(gè)屬性,所有在beans里面聲明的屬性,可以直接通過(guò)<>來(lái)使用,比如<bean>等等。

xmlns:xsi:

是xml需要遵守的規(guī)范,通過(guò)URL可以看到,是w3的統(tǒng)一規(guī)范,后面通過(guò)xsi:schemaLocation來(lái)定位所有的解析文件。

xmlns:aop:

這個(gè)是重點(diǎn),是我們這里需要使用到的一些語(yǔ)義規(guī)范,與面向切面AOP相關(guān)。

xmlns:tx:

Spring中與事務(wù)相關(guān)的配置內(nèi)容。

一個(gè)XML文件,只能聲明一個(gè)默認(rèn)的語(yǔ)義解析的規(guī)范。

例如上面的xml中就只有beans一個(gè)是默認(rèn)的,其他的都需要通過(guò)特定的標(biāo)簽來(lái)使用,比如aop,它自己有很多的屬性,如果要使用,前面就必須加上aop:xxx才可以。比如上面的aop:config。

類(lèi)似的,如果默認(rèn)的xmlns配置的是aop相關(guān)的語(yǔ)義解析規(guī)范,那么在xml中就可以直接寫(xiě)config這種標(biāo)簽了。

以上就是spring aop兩種配置方式,大家了解了嗎?之后還有更多關(guān)于spring aop兩種配置方式的文章和大家分享,一定要繼續(xù)關(guān)注哦

相關(guān)文章

  • 解析ConcurrentHashMap: put方法源碼分析

    解析ConcurrentHashMap: put方法源碼分析

    ConcurrentHashMap是由Segment數(shù)組結(jié)構(gòu)和HashEntry數(shù)組結(jié)構(gòu)組成。Segment的結(jié)構(gòu)和HashMap類(lèi)似,是一種數(shù)組和鏈表結(jié)構(gòu),今天給大家普及java面試常見(jiàn)問(wèn)題---ConcurrentHashMap知識(shí),一起看看吧
    2021-06-06
  • .idea文件夾里面iml文件的作用及說(shuō)明

    .idea文件夾里面iml文件的作用及說(shuō)明

    這篇文章主要介紹了.idea文件夾里面iml文件的作用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Spring security 自定義過(guò)濾器實(shí)現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實(shí)例代碼)

    Spring security 自定義過(guò)濾器實(shí)現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實(shí)例代碼)

    這篇文章主要介紹了Spring security 自定義過(guò)濾器實(shí)現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • SpringBoot 中 AutoConfiguration的使用方法

    SpringBoot 中 AutoConfiguration的使用方法

    這篇文章主要介紹了SpringBoot 中 AutoConfiguration的使用方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-04-04
  • Mybatis基礎(chǔ)概念與高級(jí)應(yīng)用小結(jié)

    Mybatis基礎(chǔ)概念與高級(jí)應(yīng)用小結(jié)

    這篇文章主要介紹了Mybatis基礎(chǔ)回顧與高級(jí)應(yīng)用,本文內(nèi)容有點(diǎn)小長(zhǎng),希望大家耐心閱讀,此文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • Java多線程并發(fā)編程和鎖原理解析

    Java多線程并發(fā)編程和鎖原理解析

    這篇文章主要介紹了Java多線程并發(fā)編程和鎖原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • IDEA2020.2創(chuàng)建springboot項(xiàng)目卡死在reading maven project的問(wèn)題

    IDEA2020.2創(chuàng)建springboot項(xiàng)目卡死在reading maven project的問(wèn)題

    這篇文章主要介紹了關(guān)于2020.2IDEA用spring Initializr創(chuàng)建maven的springboot項(xiàng)目卡死在reading maven project的問(wèn)題描述及解決方法,感興趣的朋友跟隨小編一起看看吧
    2020-09-09
  • SpringBoot自定義轉(zhuǎn)換器應(yīng)用實(shí)例講解

    SpringBoot自定義轉(zhuǎn)換器應(yīng)用實(shí)例講解

    SpringBoot在響應(yīng)客戶端請(qǐng)求時(shí),將提交的數(shù)據(jù)封裝成對(duì)象時(shí),使用了內(nèi)置的轉(zhuǎn)換器,SpringBoot 也支持自定義轉(zhuǎn)換器,這個(gè)內(nèi)置轉(zhuǎn)換器在 debug的時(shí)候,可以看到,提供了124個(gè)內(nèi)置轉(zhuǎn)換器
    2022-08-08
  • Java?Validated?分組校驗(yàn)的使用

    Java?Validated?分組校驗(yàn)的使用

    這篇文章主要介紹了Java?Validated?分組校驗(yàn)的使用,文章記錄所以在這里記錄下分組校驗(yàn)注解@Validated的使用,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-02-02
  • JAVA實(shí)現(xiàn)鏈表面試題

    JAVA實(shí)現(xiàn)鏈表面試題

    這篇文章主要為大家詳細(xì)介紹了JAVA相關(guān)實(shí)現(xiàn)鏈表的面試題,代碼實(shí)現(xiàn)非常詳細(xì),每一個(gè)方法講解也很到位,特別適合參加Java面試的朋友閱讀
    2015-09-09

最新評(píng)論

南川市| 刚察县| 丰台区| 无锡市| 平果县| 苍溪县| 珲春市| 辽阳市| 澄城县| 漾濞| 报价| 岫岩| 永昌县| 万山特区| 陆河县| 阿拉善盟| 常熟市| 上饶市| 合山市| 正宁县| 卓资县| 稷山县| 静乐县| 嘉鱼县| 鹤峰县| 蕲春县| 虹口区| 彭州市| SHOW| 博乐市| 陈巴尔虎旗| 凌云县| 连江县| 中西区| 辽宁省| 新巴尔虎左旗| 泰来县| 太白县| 苏尼特左旗| 吐鲁番市| 钟祥市|