学习完了Spring AOPAnnotation之后在再学xml方式的我觉得就很简明易懂了。这个案例不再将一些细节性的问题再次叙述,请看案例代码,如下所示:
1、 编写被代理对象:
~~~
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、编写配置文件:
~~~
<?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-2.5.xsd" >
<context:annotation-config/>
<!-- 配置容器资源扫描的包 -->
<context:component-scan base-package="com.spring" />
<!-- 将前面类写入容器 -->
<bean id="logInterceptor" class="com.spring.aop.LogInterceptor" />
<!-- 配置AOP -->
<aop:config>
<!-- 声明pointcut -->
<aop:pointcut expression="execution(public * com.spring.service..*.*(..))" id="servicepoint" />
<!-- 声明切面类 -->
<aop:aspect id="aspectLog" ref="logInterceptor">
<aop:before method="BeforeMethod" pointcut-ref="servicepoint"/>
<aop:after-returning method="AfterMethod" pointcut-ref="servicepoint"/>
<aop:around method="aroundProcced" pointcut-ref="servicepoint"/>
</aop:aspect>
</aop:config>
</beans>
~~~
3、编写测试文件:
~~~
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();
}
}
~~~
- 前言
- 一、Ioc控制反转注入原理
- 二、Ioc控制反转集合注入和Scope
- 三、Bean的懒加载和生命周期
- 四、基于Annotation的bean
- 五、Spring容器组建注解@Component和Resouces实现完全注解配置
- 六、Spring自动装配注解@Autowired
- 七、Spring Aop的理解和简单实现
- 八、Spring Aop织入点语法和相关案例总结
- 九、Spring Aop 用xml的方式实现
- 十、Spring DBCP用xml和properties2种格式配置DataSource
- 十一、Spring整合Hibernte
- 十二、Spring中Annotation声明事务
- 十三、Spring中Xml声明事务
- 十四、Spring中hibernateTemplate的使用