applicationContext.xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 4 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/aop 8 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 11 <aop:aspectj-autoproxy/> 12 <!-- 注入日志类 LogAspect--> 13 <bean id="logAspect" class="com.test.LogAspect"/> 14 <!-- 注入业务类 --> 15 <bean name="test" class="com.test.TestServiceImpl"></bean> 16 <!-- AOP配置 --> 17 <aop:config> 18 <aop:aspect id="simpleApect" ref="logAspect"> 19 <!-- 配置切入点 --> 20 <aop:pointcut expression="execution(* com.test..*(..))" id="simplePointCut"/> 21 <!-- 配置通知 --> 22 <aop:before method="beforeShow" pointcut-ref="simplePointCut"/> 23 <aop:after method="afterShow" pointcut-ref="simplePointCut"/> 24 <!-- <aop:after-returning method="afterReturn" pointcut-ref="simplePointCut"/> --> 25 <aop:after-throwing method="doAfterThrowing" pointcut-ref="simplePointCut" throwing="ex"/> 26 </aop:aspect> 27 </aop:config> 28 </beans> |
测试主入口
1 package com.test; 2 3 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.springframework.context.support.ClassPathXmlApplicationContext; 8 9 public class TestMain{ 10 public static void main(String[] args) { 11 //获取BeanFactory 12 ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml"); 13 TestService test = (TestService)factory.getBean("test"); 14 List l = new ArrayList(); 15 l.add("param1"); 16 l.add("param2"); 17 l.add("param3"); 18 //调用测试方法 19 test.test("ok",1,l); 20 } 21 } |
日志管理有两种方式:一种是注解方式,另外一种是xml配置方式(后续研究...)。主要用来与项目业务逻辑层进行分割,就是所谓的解耦和,便于日后管理及维护。
测试时,需要导入Spring的包有:Spring基础包、Spring AOP相关包。