国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - spring aop兩種配置方式

spring aop兩種配置方式

2020-01-05 14:49lijiao JAVA教程

這篇文章主要為大家詳細介紹了spring aop兩種配置方式,主要是注解配置AOP和xml配置aop,需要的朋友可以參考下

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

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

實例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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);
}

實現接口:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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");*/ //拋異常
 }
 
}

操作類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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");
 }
}

配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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>

測試類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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();
 
 }
 
}

結果:

?
1
2
3
4
5
6
class com.bjsxt.service.UserService$$EnhancerByCGLIB$$7b201784
method staet
user save11d!
method AfterReturning
method after
###

注意:

@Aspect:意思是這個類為切面類
@Componet:因為作為切面類需要 Spring 管理起來,所以在初始化時就需要將這個類初始化加入 Spring 的管理;
@Befoe:切入點的邏輯(Advice)
execution…:切入點語法

第二種:xml配置aop

實例同上:只是配置文件不同

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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兩種配置方式的文章和大家分享,一定要繼續關注哦

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美一级二级视频 | 黄p在线看 | 日韩精品一区二区在线观看 | 91精品国产综合久久久久久丝袜 | 亚洲福利| 婷婷综合色 | 亚洲毛片在线观看 | 一级黄色国产片 | 日韩一区二区不卡 | 噜噜噜视频在线观看 | 国产男人天堂 | 色欧美亚洲 | 日韩成人一区 | 最近2018年手机中文字幕版 | 欧美精品亚洲精品日韩精品 | 国产精品亚洲视频 | 成人黄色在线观看 | 一级全黄性色生活片 | 亚洲网站在线 | 91最新| 国产性猛交xxxx免费看久久 | 国产精品美女久久久久久久久久久 | 久久国产电影 | 国产精品一区二区视频 | 台湾黄网| 亚洲精品久 | 黄色a级网站 | 欧美色欧美亚洲另类七区 | 日韩精品中文字幕在线 | 久久久女女女女999久久 | 亚洲成人免费 | 成年网站视频 | 国产精品美女久久久久av麻豆 | 亚洲国产精品久久久 | av网站免费看 | 午夜寂寞少妇aaa片毛片 | 自拍偷拍第一页 | 久久综合一区二区 | 男人的天堂在线视频 | 国产精品成人观看视频国产奇米 | 台湾一级特黄aa大片免费看 |