关闭

spring-AOP

标签: spring-AOP
55人阅读 评论(0) 收藏 举报

spring-AOP

1,实体类

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;
	}
}
2,数据库操作Dao类

public interface UserDao {
	
	public void save(User u);
	
}
3,数据库实现类

@Component
public class UserDaoImpl implements UserDao{

	@Override
	public void save(User u) {
		System.out.println("a user save !");
	}
	
}
4,业务service类

@Service
public class UserService {
	
	private UserDao userDao;
	
	public UserDao getUserDao() {
		return userDao;
	}
	
	@Resource   //默认是按名称userDao
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public void Add(User u){
		this.userDao.save(u);
	}
}
5,代理处理类

@Aspect
public class LogInterceptor {
	
	public void before(){
		System.out.println("method start!");
	}
	public void end(){
		System.out.println("method end!");
	}
}

6,xml配置

<?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.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" >
	
	<context:annotation-config /> 
	 <!--默认初始化4个处理annotation的bean,若扫描到写的有@Autowired 将会调用set方法注入  
	 		 是根据byType注入的   若有两个类型u的话,可以使用@Qualifier("u")  -->
	<context:component-scan base-package="com.cn.sxt" />
	<!-- 开启自动扫描,之后就不用bean注,会自动根据类名首字母小写作为bena的名字 -->
	
	<bean id="logInteceptor" class="com.cn.sxt.aop.LogInterceptor"></bean>
	<aop:config>
		<aop:pointcut expression="execution(public * com.cn.sxt.service..*.*(..))" id="servicePointcut"/>
		<aop:aspect id="logAspect" ref="logInteceptor">
			<aop:before method="before" pointcut-ref="servicePointcut"/>
			<aop:after method="end" pointcut-ref="servicePointcut"/>
		</aop:aspect>
	</aop:config>
</beans>

7,测试类

@Test
	public void testAdd() throws JDOMException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
		ApplicationContext acx = new ClassPathXmlApplicationContext("beans.xml");
		UserService userService = (UserService) acx.getBean("userService");
		User u = new User();
		userService.Add(u);
	} 
8,结果输出:
method start!
a user save !
method end!







0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1415次
    • 积分:178
    • 等级:
    • 排名:千里之外
    • 原创:17篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类