SpringAOP之XML配置

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";
	}
}
五、頁面訪問測試









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章