Announcement Announcement Module
Collapse
No announcement yet.
AOP With Log4j and Annotation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP With Log4j and Annotation

    Im trying to use: Log4J, Annotation, AOP and SpringFrameWork, its not work, the app runs fine but dont log in Log4j.

    i put the code here, what im missing?


    aop.AuditLogProcessor.java
    Code:
    package aop;
    
    import org.apache.log4j.Logger;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Aspect;
    
    @Aspect
    public class AuditLogProcessor
    {
        private static Logger log = Logger.getLogger(AuditLogProcessor.class);
    
        @After("execution(* aop.GreetingService.sayGreeting())")
        public void after() {
            log.info("==> after");
        }
    
        @After("execution(* aop.GreetingService.sayGreeting())")
        public void before() {
            log.info("==> before");
        }
    }
    aop.GreetingService.java
    Code:
    package aop;
    
    public class GreetingService {
    	private String greeting;
    	
    	GreetingService() {}
    	GreetingService(String greeting) {
    		setGreeting(greeting);
    	}
    	
    	public void sayGreeting() {
    		System.out.println( greeting );
    	}
    	
    	public void setGreeting(String greeting) {
    		this.greeting = greeting;
    	}
    	
    }
    aop.HelloApp.java
    Code:
    package aop;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.FileSystemResource;
    
    public class HelloApp {
    	public static void main(String args[]) {
    		BeanFactory factory = 
    			new XmlBeanFactory( 
    					new FileSystemResource("aop/spring.xml") );
    		
    		GreetingService greetingServiceImpl = (GreetingService) factory.getBean("GreetingService");
    		greetingServiceImpl.sayGreeting();
    	}
    }
    spring.xml
    Code:
    <?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:aop="http://www.springframework.org/schema/aop"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    		http://www.springframework.org/schema/aop
    		http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    	
    	<bean id="AuditLogProcessor" class="aop.AuditLogProcessor" />
    	<bean id="GreetingService" class="aop.GreetingService">
    		<property name="greeting">
    			<value>Teste de AOP-Spring!</value>
    		</property>
    	</bean>
    	
    	<aop:aspectj-autoproxy>
    		<aop:include name="GreetingService"/>
    	</aop:aspectj-autoproxy>
    	
    </beans>
    log4j.properties
    Code:
    log4j.rootLogger=INFO, stdout, logfile
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
    
    log4j.appender.logfile=org.apache.log4j.RollingFileAppender
    log4j.appender.logfile.File=springapp.log
    log4j.appender.logfile.MaxFileSize=512KB
    
    log4j.appender.logfile.MaxBackupIndex=3
    
    log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
    log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
    anyone knows what im didnt do or do wrong?
    thx,

  • #2
    Change XmlBeanFactory to ClasspathXmlApplicationContext. I haven't checked the rest of the code, but XmlBeanFactory definitely won't work.

    -Ramnivas

    Comment


    • #3
      Originally posted by ramnivas View Post
      Change XmlBeanFactory to ClasspathXmlApplicationContext. I haven't checked the rest of the code, but XmlBeanFactory definitely won't work.

      -Ramnivas
      i did it but still not working ...

      Code:
      public static void main(String args[]) {
      		ClassPathXmlApplicationContext factory = 
      			new ClassPathXmlApplicationContext("aop/spring.xml");
      		
      		GreetingService greetingServiceImpl = (GreetingService) factory.getBean("GreetingService");
      		greetingServiceImpl.sayGreeting();
      	}

      Comment

      Working...
      X