From 2ee63efef7f5b31d6e2a81a931f694eaf7ce5d16 Mon Sep 17 00:00:00 2001 From: luffy9412 <1029559041@qq.com> Date: Tue, 19 Sep 2017 22:10:44 +0800 Subject: [PATCH] =?UTF-8?q?aop=E9=85=8D=E7=BD=AE,=E5=A2=9E=E5=8A=A0log?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../main/java/org/pqh/core/aop/InsertAdvice.java | 7 +++++-- .../main/java/org/pqh/core/aop/InsertAdvisor.java | 13 ++++++++++++- core/src/main/java/org/pqh/core/aop/LogAspect.java | 12 +++++++++--- .../src/main/java/org/pqh/core/aop/MatcherRule.java | 12 ------------ .../java/org/pqh/core/config/HibernateConfig.java | 7 ++----- .../main/java/org/pqh/core/service/BaseService.java | 7 ++----- core/src/main/java/org/pqh/core/util/LogManger.java | 11 +++++++++++ .../src/main/java/org/pqh/core/util/SpringUtil.java | 5 +---- .../org/pqh/achieve/config/AbstractWebConfig.java | 6 +++--- .../java/org/pqh/achieve/config/WebConfigImpl.java | 6 +++--- .../org/pqh/achieve/controller/AdminController.java | 6 +++--- .../org/pqh/achieve/controller/BaseController.java | 7 ++----- 13 files changed, 54 insertions(+), 47 deletions(-) delete mode 100644 core/src/main/java/org/pqh/core/aop/MatcherRule.java create mode 100644 core/src/main/java/org/pqh/core/util/LogManger.java diff --git a/README.md b/README.md index 90804e8..1b9fb59 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -#webCrawler +#WebCrawler diff --git a/core/src/main/java/org/pqh/core/aop/InsertAdvice.java b/core/src/main/java/org/pqh/core/aop/InsertAdvice.java index e7092a8..b45a928 100644 --- a/core/src/main/java/org/pqh/core/aop/InsertAdvice.java +++ b/core/src/main/java/org/pqh/core/aop/InsertAdvice.java @@ -1,6 +1,8 @@ package org.pqh.core.aop; +import org.pqh.core.util.LogManger; import org.springframework.aop.MethodBeforeAdvice; +import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.Arrays; @@ -8,10 +10,11 @@ import java.util.Arrays; /** * Created by reborn on 2017/8/3. */ -public class InsertAdvice implements MethodBeforeAdvice { +@Component +public class InsertAdvice implements MethodBeforeAdvice,LogManger { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { - System.out.println("类:" + o.getClass() + ",方法:" + method.getName() + "被调用,传入参数:" + Arrays.asList(objects)); + log.info("类:" + o.getClass() + ",方法:" + method.getName() + "被调用,传入参数:" + Arrays.asList(objects)); } } diff --git a/core/src/main/java/org/pqh/core/aop/InsertAdvisor.java b/core/src/main/java/org/pqh/core/aop/InsertAdvisor.java index cb2b662..f942674 100644 --- a/core/src/main/java/org/pqh/core/aop/InsertAdvisor.java +++ b/core/src/main/java/org/pqh/core/aop/InsertAdvisor.java @@ -1,17 +1,28 @@ package org.pqh.core.aop; +import org.aopalliance.aop.Advice; import org.pqh.core.dao.BaseDao; import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor; +import org.springframework.stereotype.Component; +import javax.annotation.PostConstruct; +import javax.annotation.Resource; import java.lang.reflect.Method; /** * Created by reborn on 2017/8/3. */ +@Component public class InsertAdvisor extends StaticMethodMatcherPointcutAdvisor { + @Resource + private Advice insertAdvice; @Override public boolean matches(Method method, Class aClass) { - return method.getName().equals("get")&& BaseDao.class.isAssignableFrom(aClass); + return BaseDao.class.isAssignableFrom(aClass); } + @PostConstruct + public void init(){ + this.setAdvice(insertAdvice); + } } diff --git a/core/src/main/java/org/pqh/core/aop/LogAspect.java b/core/src/main/java/org/pqh/core/aop/LogAspect.java index f1e169a..f0ec13b 100644 --- a/core/src/main/java/org/pqh/core/aop/LogAspect.java +++ b/core/src/main/java/org/pqh/core/aop/LogAspect.java @@ -3,6 +3,8 @@ package org.pqh.core.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.pqh.core.util.LogManger; import org.springframework.stereotype.Component; /** @@ -10,13 +12,17 @@ import org.springframework.stereotype.Component; */ @Aspect @Component -public class LogAspect { +public class LogAspect implements LogManger { - @Before(value = "org.pqh.core.aop.MatcherRule.biliDaoRule()") + @Before(value = "biliDaoRule()") public void curdCallAction(JoinPoint joinPoint) { for (Object arg : joinPoint.getArgs()) { - System.out.println("参数" + arg); + log.info("参数" + arg); } } + @Pointcut("execution(* org.pqh.core.dao.BaseDao.*(..))") + public void biliDaoRule() { + } + } diff --git a/core/src/main/java/org/pqh/core/aop/MatcherRule.java b/core/src/main/java/org/pqh/core/aop/MatcherRule.java deleted file mode 100644 index 3d26e45..0000000 --- a/core/src/main/java/org/pqh/core/aop/MatcherRule.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.pqh.core.aop; - -import org.aspectj.lang.annotation.Pointcut; - -/** - * Created by reborn on 2017/8/3. - */ -public class MatcherRule { - @Pointcut("execution(* org.pqh.core.dao.BaseDao.*(..))") - public void biliDaoRule() { - } -} diff --git a/core/src/main/java/org/pqh/core/config/HibernateConfig.java b/core/src/main/java/org/pqh/core/config/HibernateConfig.java index ec55145..84c7fb1 100644 --- a/core/src/main/java/org/pqh/core/config/HibernateConfig.java +++ b/core/src/main/java/org/pqh/core/config/HibernateConfig.java @@ -1,8 +1,7 @@ package org.pqh.core.config; import org.apache.commons.dbcp2.BasicDataSource; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.pqh.core.util.LogManger; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -22,9 +21,7 @@ import java.util.Properties; @Configuration @PropertySource("classpath:config.properties") @EnableTransactionManagement -public class HibernateConfig{ - - private Logger log= LogManager.getLogger(); +public class HibernateConfig implements LogManger{ @Value("${hibernate.connection.pool_size}") private String pool_size; diff --git a/core/src/main/java/org/pqh/core/service/BaseService.java b/core/src/main/java/org/pqh/core/service/BaseService.java index 40a39a0..ad15e69 100644 --- a/core/src/main/java/org/pqh/core/service/BaseService.java +++ b/core/src/main/java/org/pqh/core/service/BaseService.java @@ -1,10 +1,9 @@ package org.pqh.core.service; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.pqh.core.dao.BaseDao; import org.pqh.core.model.AbstractModel; import org.pqh.core.util.DBAction; +import org.pqh.core.util.LogManger; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -13,9 +12,7 @@ import javax.annotation.Resource; * Created by reborn on 2017/7/28. */ @Service -public class BaseService { - - protected Logger log = LogManager.getLogger(); +public class BaseService implements LogManger { @Resource private BaseDao baseDao; diff --git a/core/src/main/java/org/pqh/core/util/LogManger.java b/core/src/main/java/org/pqh/core/util/LogManger.java new file mode 100644 index 0000000..a88dfac --- /dev/null +++ b/core/src/main/java/org/pqh/core/util/LogManger.java @@ -0,0 +1,11 @@ +package org.pqh.core.util; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * Created by reborn on 2017/9/19. + */ +public interface LogManger { + Logger log=LogManager.getLogger(); +} diff --git a/core/src/main/java/org/pqh/core/util/SpringUtil.java b/core/src/main/java/org/pqh/core/util/SpringUtil.java index 2648d04..0caca0f 100644 --- a/core/src/main/java/org/pqh/core/util/SpringUtil.java +++ b/core/src/main/java/org/pqh/core/util/SpringUtil.java @@ -1,7 +1,5 @@ package org.pqh.core.util; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; @@ -19,9 +17,8 @@ import java.util.Map; */ @Component @Lazy(false) -public class SpringUtil implements ApplicationContextAware { +public class SpringUtil implements ApplicationContextAware,LogManger { private static ApplicationContext applicationContext; - private Logger log= LogManager.getLogger(); public static T getBean(Class c){ return applicationContext.getBean(c); diff --git a/web/src/main/java/org/pqh/achieve/config/AbstractWebConfig.java b/web/src/main/java/org/pqh/achieve/config/AbstractWebConfig.java index 66c1f68..227cfa4 100644 --- a/web/src/main/java/org/pqh/achieve/config/AbstractWebConfig.java +++ b/web/src/main/java/org/pqh/achieve/config/AbstractWebConfig.java @@ -1,6 +1,6 @@ package org.pqh.achieve.config; -import org.apache.logging.log4j.LogManager; +import org.pqh.core.util.LogManger; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -14,7 +14,7 @@ import java.util.EnumSet; * Created by reborn on 2017/7/28. */ -public class AbstractWebConfig extends AbstractAnnotationConfigDispatcherServletInitializer { +public class AbstractWebConfig extends AbstractAnnotationConfigDispatcherServletInitializer implements LogManger { private String encoding ="UTF-8"; @@ -35,7 +35,7 @@ public class AbstractWebConfig extends AbstractAnnotationConfigDispatcherServlet @Override public void onStartup(ServletContext servletContext) throws ServletException { - LogManager.getLogger().info("onStartup"); + log.info("onStartup"); super.onStartup(servletContext); FilterRegistration.Dynamic dynamic=super.registerServletFilter(servletContext,new CharacterEncodingFilter(encoding)); dynamic.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),true,"/*"); diff --git a/web/src/main/java/org/pqh/achieve/config/WebConfigImpl.java b/web/src/main/java/org/pqh/achieve/config/WebConfigImpl.java index 60dfc44..ea585bf 100644 --- a/web/src/main/java/org/pqh/achieve/config/WebConfigImpl.java +++ b/web/src/main/java/org/pqh/achieve/config/WebConfigImpl.java @@ -1,6 +1,6 @@ package org.pqh.achieve.config; -import org.apache.logging.log4j.LogManager; +import org.pqh.core.util.LogManger; import org.springframework.web.WebApplicationInitializer; import javax.servlet.ServletContext; @@ -9,12 +9,12 @@ import javax.servlet.ServletException; /** * Created by reborn on 2017/9/18. */ -public class WebConfigImpl implements WebApplicationInitializer{ +public class WebConfigImpl implements WebApplicationInitializer,LogManger{ private String encoding ="UTF-8"; @Override public void onStartup(ServletContext servletContext) throws ServletException { - LogManager.getLogger().info("onStartup"); + log.info("onStartup"); // //基于注解配置的上下文 // AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); // //注册Spring容器配置类 diff --git a/web/src/main/java/org/pqh/achieve/controller/AdminController.java b/web/src/main/java/org/pqh/achieve/controller/AdminController.java index 0cd0d62..974109b 100644 --- a/web/src/main/java/org/pqh/achieve/controller/AdminController.java +++ b/web/src/main/java/org/pqh/achieve/controller/AdminController.java @@ -1,6 +1,6 @@ package org.pqh.achieve.controller; -import org.apache.logging.log4j.LogManager; +import org.pqh.core.util.LogManger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @@ -8,10 +8,10 @@ import org.springframework.web.bind.annotation.RequestMapping; * Created by reborn on 2017/9/14. */ @Controller -public class AdminController{ +public class AdminController implements LogManger{ @RequestMapping({"","index"}) private String index(){ - LogManager.getLogger().info("进入首页"); + log.info("进入首页"); return "index"; } } diff --git a/web/src/main/java/org/pqh/achieve/controller/BaseController.java b/web/src/main/java/org/pqh/achieve/controller/BaseController.java index 0946c94..74446cd 100644 --- a/web/src/main/java/org/pqh/achieve/controller/BaseController.java +++ b/web/src/main/java/org/pqh/achieve/controller/BaseController.java @@ -1,10 +1,9 @@ package org.pqh.achieve.controller; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.pqh.core.model.AbstractModel; import org.pqh.core.service.BaseService; import org.pqh.core.util.DBAction; +import org.pqh.core.util.LogManger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -16,9 +15,7 @@ import javax.annotation.Resource; * Created by reborn on 2017/9/14. */ @Controller -public class BaseController { - - protected Logger log = LogManager.getLogger(); +public class BaseController implements LogManger{ @Resource protected BaseService baseService;