最近在学习了springboot,但是遇到了这样一个问题,我搭建完架构后,想加上一个请求日志打印和响应日志打印,这无疑就用到了spring的AOP机制了,但是我配置完了之后,确是没有生效。我也百度了,但是看配置都和我差不多,但是我的却不生效,这令我百思不得其解,希望大家帮我看看。我的代码如下:/**
* 这配置AOP的代码
*/
@Aspect
@Component
public class HttpAspect{ private Logger logger = LoggerFactory.getLogger(HttpAspect.class); public HttpAspect(){
logger.info("=============初始化AOP===============");
} @Pointcut("execution(public * com.springboot.mybatis.controller.*.*(..))")
public void log(){
} /**
*
* 获取请求的信息
*/
@Before("log()")
public void doBefore(JoinPoint joinPoint){ ServletRequestAttributes arrt = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = arrt.getRequest();
logger.info("请求地址 url:{}",request.getRequestURL());
logger.info("请求方式 :{}",request.getMethod());
logger.info("请求者ip地址:{}",request.getRemoteAddr());
logger.info("访问的类与方法:{}",joinPoint.getSignature().getDeclaringTypeName()+"."
+joinPoint.getSignature().getName());
logger.info("请求参数:{}",joinPoint.getArgs());
} /**
* 获取响应的信息
* @param object
*/
@AfterReturning(returning = "object", argNames = "object", pointcut = "log()")
public void doAfterReturning(Object object){ logger.info("响应信息如下:");
logger.info("response:{}",object.toString());
}
}
/**
* 这是启动应用的代码
*/
@SpringBootApplication(scanBasePackages = "com")
@MapperScan(basePackages = "com.springboot.mybatis.dao")
public class SpringbootMybatisApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
#这是我的配置文件
server:
port: 8080
context-path: /sbmspring:
profiles:
active: dev
#数据库配置
datasource:
url: jdbc:mysql://localhost:3306/testaclec?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
#页面跳转配置
mvc:
view:
prefix: /view/
suffix: .html#mybatis 配置
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.springboot.mybatis.model#分页插件配置
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSqllogging:
config: classpath:logback-spring.xml
* 这配置AOP的代码
*/
@Aspect
@Component
public class HttpAspect{ private Logger logger = LoggerFactory.getLogger(HttpAspect.class); public HttpAspect(){
logger.info("=============初始化AOP===============");
} @Pointcut("execution(public * com.springboot.mybatis.controller.*.*(..))")
public void log(){
} /**
*
* 获取请求的信息
*/
@Before("log()")
public void doBefore(JoinPoint joinPoint){ ServletRequestAttributes arrt = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = arrt.getRequest();
logger.info("请求地址 url:{}",request.getRequestURL());
logger.info("请求方式 :{}",request.getMethod());
logger.info("请求者ip地址:{}",request.getRemoteAddr());
logger.info("访问的类与方法:{}",joinPoint.getSignature().getDeclaringTypeName()+"."
+joinPoint.getSignature().getName());
logger.info("请求参数:{}",joinPoint.getArgs());
} /**
* 获取响应的信息
* @param object
*/
@AfterReturning(returning = "object", argNames = "object", pointcut = "log()")
public void doAfterReturning(Object object){ logger.info("响应信息如下:");
logger.info("response:{}",object.toString());
}
}
/**
* 这是启动应用的代码
*/
@SpringBootApplication(scanBasePackages = "com")
@MapperScan(basePackages = "com.springboot.mybatis.dao")
public class SpringbootMybatisApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
#这是我的配置文件
server:
port: 8080
context-path: /sbmspring:
profiles:
active: dev
#数据库配置
datasource:
url: jdbc:mysql://localhost:3306/testaclec?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
#页面跳转配置
mvc:
view:
prefix: /view/
suffix: .html#mybatis 配置
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.springboot.mybatis.model#分页插件配置
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSqllogging:
config: classpath:logback-spring.xml
解决方案 »
- ssh2项目中 多对多表间关系 新增数据问题
- xfire做客户端调用服务器出错!
- 菜鸟求助can't bind arbitrary objects in an FSContext
- 问个简单的xml与xsd的问题,请懂的来帮忙
- jdk1.2.2如果解决页面之间中文参数乱码问题?高手解答!!!
- java中使用xerces修改xml文件后,如何保存文件才能保证属性的顺序不变?
- sql server 2000和2005连接池包在哪下
- 请推荐junit group
- 毕业论文
- dubbo kryo 序列化问题,在线等。。。急,各位大神进来帮忙看看,谢谢了。
- 上传文件MultipartFile类转为File文件不使用临时文件
- Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Cou
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) 他是不会定义你自己的扫描包的,应该把你的你的@SpringBootApplicatioin运行的类放在上层的 例如:com.kkk.clm 放在 com.kkk里面就能扫描到了
/**
* 这是启动应用的代码
*/
@SpringBootApplication(scanBasePackages = "com")
@MapperScan(basePackages = "com.springboot.mybatis.dao")
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
@SpringBootApplication这个注解不会扫描自定义包的,无论你写很多包名都是不行的,@MapperScan(basePackages = "com.springboot.mybatis.dao")这个注解在这面不起作用的直接去掉,直接把这个类移到com目录下,就可以了
@SpringBootApplication
@ComponentScan("com.xxx.*") //这里要能扫描到你注解的HttpAspect 才行
public class PheryWebApplication
{ public static void main(String[] args)
{
SpringApplication.run(PheryWebApplication.class, args);
}
}你把你的HttpAspect类随便注入其他类中,看是不是能注入,我的不能,我就加了扫描包,可以注入了, 注解也生效了
@Component//这个必不可缺,否则aop不生效
@Aspect//开启切面Aop//不能只使用@Component,因为只使用此注解启动会报aop切面类注入容器失败(我这里是这样),
//需加上@ComponentScan(basePackages="XXX.XXX.XXX")扫描具体包才能注入成功
我按照7楼的办法,可以使用了,没有加入basePackage.