I am trying to customize the logging aspect, but I do not understand how it works.
I have a spring web mvc application. Consider this:
package of configuration classes with LoggingConfiguration:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; import my.package.aspects.LoggingAspect; import my.package.web.controller.MemberController; @Configuration @EnableAspectJAutoProxy public class LoggingConfiguration { @Bean public LoggingAspect loggingAspect() { return new LoggingAspect(); } @Bean MemberController memberController(){ return new MemberController(); } }
Aspect:
@Aspect @Component public class LoggingAspect { static Logger log = LoggerFactory.getLogger(LoggingAspect.class); @Before("execution(* my.package..*.*(..) )") public void logBefore(JoinPoint joinPoint) { log.debug("logBefore() is running!"); log.debug(joinPoint.getSignature().getName()); } }
log4j.xml (appender defined)
<logger name="my.package" additivity="false"> <appender-ref ref="consoleAppender" /> <appender-ref ref="fileControllerAppender" /> </logger>
Why doesn't the configuration work?
thanks
EDIT
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>my.package.config.JpaConfiguration</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value>my.package.config.WebConfiguration</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> <url-pattern>*.html</url-pattern> </servlet-mapping> <!-- <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.xml</param-value> </context-param> --> <session-config> <session-timeout>30</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
EDIT I am missing adding LoggingConfiguration to web.xml. Now I have this:
<init-param> <param-name>contextConfigLocation</param-name> <param-value>my.package.config.WebConfiguration, my.package.config.LoggingConfiguration</param-value> </init-param>
but I have this message:
Error creating bean with name 'viewResolver' defined in class my.package.config.WebConfiguration: No matching factory method found: factory bean 'webConfiguration'; factory method 'getViewResolver()'. Check that a method with the specified name exists and that it is non-static.
I tried to delete the line in web.xml and add @Import(LoggingConfiguration.class) to WebConfiguration, but I got the same message.
Webconfiguration:
@Configuration @EnableWebMvc @ComponentScan("my.package.web.controller") public class WebConfiguration { @Bean(name = "viewResolver") public InternalResourceViewResolver getViewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/pages/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }
EDIT
Full stackTrace:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'viewResolver' defined in class my.package.config.WebConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.web.servlet.view.InternalResourceViewResolver my.package.config.WebConfiguration.viewResolver()] threw exception; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException