SpringAOP之XML配置
2018-01-05 20:21
537 查看
SpringAOP之XML配置
XML的配置方式与注解方式总体上差不多。用XML的配置来代替注解。有Spring来管理。采用Maven的方式,SSM的框架来演示。注解配置一、XML的配置
注意在配置文件中添加AOP的引用xmlns:aop="http://www.springframework.org/schema/aop"http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 自动扫描 --> <context:component-scan base-package="com.yuanjun" /> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/yuanjun/mapping/*.xml"></property> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.yuanjun.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- AOP的配置 --> <aop:aspectj-autoproxy/> <bean class="com.yuanjun.aop.MyAspect" id="myAspect"/> <aop:config> <!-- 切面表达式,对应注解@Pointcut --> <aop:pointcut expression="execution(public * com.yuanjun.service..*(..))" id="txpointcut"/> <aop:aspect ref="myAspect"> <!-- 前置通知 --> <aop:before method="before" pointcut-ref="txpointcut"/> <!-- 后置通知 --> <aop:after method="after" pointcut-ref="txpointcut"/> </aop:aspect> </aop:config> </beans>二、实现类
/** * * @ClassName:MyAspect * @Description :TODO * @author yuanjun * @date 2018-1-5 */ public class MyAspect { public void before(){ System.out.println("###前置通知###"); } public void after(){ System.out.println("###后置通知###"); } }三、简单的Servcie测试
@Service public class PersonServiceImpl implements PersonService { @Autowired private PersonDao personDao; public PersonDao getPersonDao() { return personDao; } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public List<Person> findAll() { System.out.println("execute findall method"); return personDao.findall(); } }四、控制层(Controller)
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.yuanjun.bean.Person; import com.yuanjun.service.PersonService; @Controller public class LoginControl { @Autowired private PersonService personService; @RequestMapping("/main") public String test(){ List<Person> list = personService.findAll(); for (Person person : list) { System.out.println(person); } return "main"; } }五、页面访问测试
相关文章推荐
- Spring AOP基础知识学习——XML配置
- Spring AOP(二)——在XML中配置切面
- springAOP基于xml的配置前后环绕通知
- Spring Aop实现——Annotation方式(注解式)and Schema-based式(xml配置)
- Spring Aop实例之xml配置
- spring-3-xml配置AOP
- Spring3.0 学习-AOP面向切面编程_Spring AOP的XML配置模式
- Spring Aop实例之xml配置
- Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
- Spring基于XML配置AOP
- spring学习总结(十):基于 XML 的配置AOP
- Spring之AOP使用xml配置更方便(使用AspectJ框架)(重点)
- spring aop的xml配置详解
- Spring AOP---基于Xml配置应用demo
- 使用XML配置Spring AOP
- shiro用户授权的时候,在springmvc.xml配置出现The prefix "aop" for element "aop:config" is not bound.
- Spring AOP(3)基于XML配置实现的示例
- Spring AOP+AspectJ在XML配置实例(详解)
- spring使用aop时xml中的配置
- 【Spring四】AOP之XML配置