Hi,
Can some one let me know if i can induce logging for classes that are not instantiated by spring.
Following is my code,
I have a main class,
Code:public class MainApplication { public static void main(String [] args) { ApplicationContext ctx = new FileSystemXmlApplicationContext("src/springconfig.xml"); //Instantiate an object //IBusinessLogic testObject = (IBusinessLogic) ctx.getBean("businesslogicbean"); //BusinessLogic testObject = (BusinessLogic) ctx.getBean("businesslogicbean"); BusinessLogic testObject = (BusinessLogic) ctx.getBean("beanTarget1"); //Execute the public methods of the bean testObject.foo(); try { testObject.bar(); } catch(Exception e) { System.out.println("Caught BusinessLogicException"); } } }
The business logic class that is called is,
my xml file isCode:package src; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; //public class BusinessLogic implements IBusinessLogicNUSing public class BusinessLogic { public void init() { System.out.println("Entering Main Application"); } public void foo() { System.out.println("Inside BusinessLogic.foo()"); //testingPurpose testing = new testingPurpose(); ApplicationContext ctx = new FileSystemXmlApplicationContext("src/springconfig.xml"); testingPurpose testing = (testingPurpose) ctx.getBean("beanTarget2"); testing.test(); } public void bar() throws BusinessLogicException { System.out.println("Inside BusinessLogic.bar()"); throw new BusinessLogicException(); } }
TracingAfterAdvice is,Code:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!--CONFIG--> <!-- <bean id="businesslogicbean" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>src.IBusinessLogic</value> </property> <property name="target"> <ref local="beanTarget"/> </property> <property name="interceptorNames"> <list> <value>tracingBeforeAdvisor</value> <value>tracingAfterAdvisor</value> <value>loggingThrowsAdvisor</value> </list> </property> </bean> --> <bean id="beanTarget1" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyTargetClass"><value>true</value></property> <property name="target"> <ref local="beanTarget"/> </property> <property name="interceptorNames"> <list> <value>tracingBeforeAdvisor</value> <value>tracingAfterAdvisor</value> <value>loggingThrowsAdvisor</value> </list> </property> </bean> <bean id="beanTarget2" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyTargetClass"><value>true</value></property> <property name="target"> <ref local="Pradeep"/> </property> <property name="interceptorNames"> <list> <value>tracingBeforeAdvisor</value> <value>tracingAfterAdvisor</value> <value>loggingThrowsAdvisor</value> </list> </property> </bean> <bean id="Pradeep" class="src.testingPurpose"/> <!--CLASS --> <bean id="beanTarget" class="src.BusinessLogic"/> <!-- Advisor pointcut definition for before advice --> <bean id="tracingBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local="theTracingBeforeAdvice"/> </property> <property name="pattern"> <value>.*</value> </property> </bean> <!-- Advisor pointcut definition for after advice --> <bean id="tracingAfterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local="theTracingAfterAdvice"/> </property> <property name="pattern"> <value>.*</value> </property> </bean> <!-- Advisor pointcut definition for throws advice --> <bean id="loggingThrowsAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local="theLoggingThrowsAdvice"/> </property> <property name="pattern"> <value>.*</value> </property> </bean> <!--ADVICE--> <bean id="theTracingBeforeAdvice" class="src.TracingBeforeAdvice"/> <bean id="theTracingAfterAdvice" class="src.TracingAfterAdvice"/> <bean id="theLoggingThrowsAdvice" class="src.LoggingThrowsAdvice"/> </beans>
Once i run this code i get the following message,Code:package src; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; public class TracingAfterAdvice implements AfterReturningAdvice { public void afterReturning(Object object, Method m, Object[] args, Object target) throws Throwable { System.out.println("Hello world! (by " + this.getClass().getName() + ")"); } }
Code:log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory). log4j:WARN Please initialize the log4j system properly. Hello world! (by src.TracingBeforeAdvice) ******class src.BusinessLogic Inside BusinessLogic.foo() Hello world! (by src.TracingBeforeAdvice) ******class src.testingPurpose Hello world! (by src.TracingAfterAdvice) Hello world! (by src.TracingAfterAdvice) Hello world! (by src.TracingBeforeAdvice) ******class src.BusinessLogic Inside BusinessLogic.bar() Logging that a src.BusinessLogicExceptionException was thrown. Caught BusinessLogicException
I am fine with this. But if i had to make a small change in my BusinessLogic class, ie if i instantiate testingPurpose class and dont allow spring to instantiate i am getting the following the message,
As you can spot, if i instantiate the testingPurpose class, AOP is not able to recognise the class. But i have a requirement to make the logging for the classes i instantiate aswell. Is this possible ? Can someone please let me know ??Code:log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory). log4j:WARN Please initialize the log4j system properly. Hello world! (by src.TracingBeforeAdvice) ******class src.BusinessLogic Inside BusinessLogic.foo() Hello world! (by src.TracingAfterAdvice) Hello world! (by src.TracingBeforeAdvice) ******class src.BusinessLogic Inside BusinessLogic.bar() Logging that a src.BusinessLogicExceptionException was thrown. Caught BusinessLogicException


Reply With Quote