Spring入门Blog[九、Spring Aop 用xml的方式实现]

2014-11-24 08:29:26 · 作者: · 浏览: 0

学习完了Spring AOPAnnotation之后在再学xml方式的我觉得就很简明易懂了。这个案例不再将一些细节性的问题再次叙述,请看案例代码,如下所示:

1、 编写被代理对象:

[java]
import org.springframework.stereotype.Component;
import com.spring.dao.UserDao;

@Component("userService")
public class UserServiceImpl implements UserService{
private UserDao userDao;

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

//在下面方法前面加逻辑
public void HelloWorld(){
System.out.println("helloworld");
}
}
2、编写配置文件:

[html]
< xml version="1.0" encoding="UTF-8" >
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-2.5.xsd" >










aspect id="aspectLog" ref="logInterceptor">






3、编写测试文件:

[java]
package com.spring.test;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.service.UserService;

public class SpringTest {
@Test
public void test01() {
BeanFactory applicationContext = new ClassPathXmlApplicationContext(
"beans.xml");
UserService user = (UserService) applicationContext.getBean("userService");
user.HelloWorld();
}
}