论坛首页 入门技术论坛

Spring和log4j的问题

浏览 2235 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-11-02  
//定义的service接口
       public interface IUserService(){
        public void a();
      }


//有个实现类
       public class UserService implements IUserService{
        public void a(){
          try{
               //业务代码
              System.out.println(1/0);//故意抛出异常
           }catch(Exception e){
              throw new RuntimeException(e);
           }
        }
      }

//LogHelper 

import java.lang.reflect.Method;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.aop.ThrowsAdvice;

public class LogHelper implements ThrowsAdvice {
	private Logger logger = Logger.getLogger(this.getClass().getName());
	public void afterThrowing(Method method,Object[]args,Object target,Throwable subclass){
		logger.log(Level.INFO, "logger that a"+subclass+"Exception was throw in "+method);
		System.out.println("--------LogHelper--------");
	}
	public Logger getLogger() {
		return logger;
	}
	public void setLogger(Logger logger) {
		this.logger = logger;
	}
}


想给他配log日志 实现 异常时的log输出。片段如下
<bean id="txProxyTemplateMain" abstract="true"
			class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
			<property name="transactionManager">
				<ref bean="transactionManagerMain"/>
			</property>	
			<property name="transactionAttributes">
				<props>
		  			<prop key="*">PROPAGATION_REQUIRED, readOnly</prop>
					
				</props>
			</property>
	</bean>
<bean id="userService" parent="txProxyTemplateMain">
		<property name="target">
			<bean class="model.main.service.spring.UserService">
				<property name="userDao">
					<ref local="userDao"/>
				</property>
			</bean>
		</property>
	</bean>
 	<bean id="logHelper" class="model.util.LogHelper"/>
	<bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces">
			<value>model.main.service.IUserService</value>
		</property>
		<property name="target">
			<ref local="userService"/>
		</property>
		<property name="interceptorNames">
			<list>
				<value>logHelper</value>
			</list>
		</property>	
	</bean>

其他的Spring配置我检查过了 应该没有问题的,现在就是想实现 事务+日志的功能 ,可是事务可以了,日志就不行。请问这2个之间这么配置可以吗? 如果不用helper类在catch中直接创建日志是可以的,我试过。为什么用AOP 配出来就不可以了,希望解答下  谢谢。
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics