aop配置,增加log接口

master
luffy9412 7 years ago
parent 79eced21e7
commit 2ee63efef7
  1. 2
      README.md
  2. 7
      core/src/main/java/org/pqh/core/aop/InsertAdvice.java
  3. 13
      core/src/main/java/org/pqh/core/aop/InsertAdvisor.java
  4. 12
      core/src/main/java/org/pqh/core/aop/LogAspect.java
  5. 12
      core/src/main/java/org/pqh/core/aop/MatcherRule.java
  6. 7
      core/src/main/java/org/pqh/core/config/HibernateConfig.java
  7. 7
      core/src/main/java/org/pqh/core/service/BaseService.java
  8. 11
      core/src/main/java/org/pqh/core/util/LogManger.java
  9. 5
      core/src/main/java/org/pqh/core/util/SpringUtil.java
  10. 6
      web/src/main/java/org/pqh/achieve/config/AbstractWebConfig.java
  11. 6
      web/src/main/java/org/pqh/achieve/config/WebConfigImpl.java
  12. 6
      web/src/main/java/org/pqh/achieve/controller/AdminController.java
  13. 7
      web/src/main/java/org/pqh/achieve/controller/BaseController.java

@ -1 +1 @@
#webCrawler
#WebCrawler

@ -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));
}
}

@ -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);
}
}

@ -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() {
}
}

@ -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() {
}
}

@ -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;

@ -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<T extends AbstractModel> {
protected Logger log = LogManager.getLogger();
public class BaseService<T extends AbstractModel> implements LogManger {
@Resource
private BaseDao baseDao;

@ -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();
}

@ -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>T getBean(Class<T> c){
return applicationContext.getBean(c);

@ -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,"/*");

@ -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容器配置类

@ -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";
}
}

@ -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<T extends AbstractModel> {
protected Logger log = LogManager.getLogger();
public class BaseController<T extends AbstractModel> implements LogManger{
@Resource
protected BaseService<T> baseService;

Loading…
Cancel
Save