spring复习20141221


1.applicationContext.xml

<?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:tx="http://www.springframework.org/schema/tx" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:jee="http://www.springframework.org/schema/jee" 
        xsi:schemaLocation=" 
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
            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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

<bean id="helloDao" class="com.qing.HelloDAO">
</bean>
<bean id="hello" class="com.qing.Hello">
	<property name="helloDAO" ref="helloDao">  </property>
</bean>



<bean id="loggerBean" class="com.aspect.LoggerBean"> 
</bean> 

<bean id="exceptionBean"  class="com.aspect.ExceptionLogger"></bean>


    <aop:config> 
        <aop:pointcut id="comqing" expression="within(com.qing.*)"/>
         <!-- 采用AOP配置将LoggerBean组件 作用到某一批Service的方法上 --> 
        <aop:aspect id="loggerAspect" ref="loggerBean"> 
            <aop:around method="loggerOperation" pointcut-ref="comqing"/>
        </aop:aspect> 
             <!-- 异常处理 --> 
        <aop:aspect id="exceptionAspect"  ref="exceptionBean"> 
            <aop:after-throwing method="execute"   pointcut-ref="comqing"  throwing="ex"/> 
		</aop:aspect>
		
    </aop:config>



</beans>

2.log4j.properties


log4j.rootLogger=warn,myconsole 
log4j.appender.myconsole=org.apache.log4j.ConsoleAppender 
log4j.appender.myconsole.layout=org.apache.log4j.SimpleLayout


3.test1

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qing.Hello;

public class Test1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//根据conf指定的配置实例化spring容器         
		String conf = "applicationContext.xml";         
		ApplicationContext ac =new ClassPathXmlApplicationContext(conf); 
		//通过spring容器获取定义的Bean对象         
		Hello h =(Hello)ac.getBean("hello");         
		//使用       
		System.out.println(h.findA("spring"));
	}

}

4.test2

package com.test;

import org.apache.log4j.Logger;



public class Test2 {
    static Logger logger =   Logger.getLogger(Test2 .class); 
    public static void main(String[] args) { 
        logger.debug("调试信息"); 
        logger.info("普通信息"); 
        logger.warn("警告信息"); 
        logger.error("错误信息"); 
        logger.fatal("致命信息"); 
    }
}

5.LoggerBean.java

package com.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class LoggerBean {

	// 利用环绕通知实现
	/**
	 * 记录操作日志 ProceedingJoinPoint 连接点对象
	 */
	public Object loggerOperation(ProceedingJoinPoint pjp) throws Throwable {
		// 获取当前指行的类型名
		String className = pjp.getTarget().getClass().getName();
		// 获取当前执行的方法名
		String methodName = pjp.getSignature().getName();
		

		
		System.out.println("你执行" + className + "类的" + methodName + "方法!!!!!之前!!!!!!!!!!!");
		
		Object obj = pjp.proceed();// 执行目标对象方法
		// ---------在目标方法之后执行-------------

		System.out.println("你执行" + className + "类的" + methodName + "方法!!!!之后!!!!!!!!!!");
		// String key = className+"."+methodName;
		// String msg = PropertiesUtil.getValue(key);
		// System.out.println();
		return obj;
	}

}


6.ExceptionLogger.java

package com.aspect;

import org.apache.log4j.Logger;

public class ExceptionLogger {
	
	public void mypoint(){ 
    } 
    /** 
     * 将异常信息记录到文件中 
     * @param ex 目标对象方法抛出的异常对象 
     */ 
    public void execute(Exception ex){ 
        System.out.println("====记录异常信息====="); 
        StackTraceElement[] els = ex.getStackTrace(); 
        Logger logger = Logger.getLogger(this.getClass()); 
        logger.error(ex.getClass().getName()); 
        logger.error(els[0]);//利用Log4j工具输出错误 
    }

}

7.Hello.java 

package com.qing;

public class Hello {
	
	private String s;
	public void setS(String s) {
		this.s = s;
	}

	private HelloDAO helloDAO;
	public void setHelloDAO(HelloDAO helloDAO) {
		this.helloDAO = helloDAO;
	}




	public String findA(String a){

		
		if(helloDAO!=null){
			//return helloDAO.findById(123);
			return s;
		}
		return a;
	}

}

8.HelloDAO.java

package com.qing;

public class HelloDAO {
	
	public String findById(int id){
		//throw new NullPointerException();//用于测试aop异常异常处理
		return "获得了id为"+id+"的数据";
	}

}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值