您的位置:首页 > 编程语言 > Java开发

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";
}
}
五、页面访问测试

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息