Home > Back-end >  Spring AOP boot configuration without effect
Spring AOP boot configuration without effect

Time:12-08

Recently in learning springboot, but encountered such a problem, after I set up the architecture, want to add a request log print and log response print, it will use the spring AOP mechanism, but after I finished the configuration, it is not effective, I also baidu, but the configuration is similar to me, but I did not take effect, this makes me puzzled, hope everybody to help me have a look, my code is as follows:
 
/* *
* the configuration of AOP code
*/
@ Aspect
@ Component
Public class HttpAspect {

Private Logger Logger=LoggerFactory. GetLogger (HttpAspect. Class);

Public HttpAspect () {
Logger. The info ("=============initialization AOP===============");
}

The @pointcut (" execution (public * com. Springboot. Mybatis. Controller. *. * (.. )))
Public void the log () {
}

/* *
*
* get requested information
*/
@ Before (" the log () ")
Public void doBefore (JoinPoint JoinPoint) {

ServletRequestAttributes arrt=(ServletRequestAttributes) RequestContextHolder. GetRequestAttributes ();
It request=arrt. GetRequest ();
Logger. The info (" url request address: {} ", request. GetRequestURL ());
Logger. The info (" request: {} ", request getMethod ());
Logger. The info (" requester IP address: {} ", request. GetRemoteAddr ());
Logger. The info (" access classes and methods: {} ", joinPoint. GetSignature () getDeclaringTypeName () + ", "
+ joinPoint. GetSignature (). The getName ());
Logger. The info (" request parameters: {} ", joinPoint getArgs ());
}

/* *
* for response information
* @ param object
*/
@ AfterReturning (returning="object", argNames="object", pointcut="log ()")
Public void doAfterReturning Object (Object) {

Logger. The info (" response information is as follows: ");
Logger. The info (" response: {} ", the object. The toString ());
}
}

 
/* *
* this is to start the application code
*/
@ SpringBootApplication (scanBasePackages="com")
@ MapperScan (basePackages="com. Springboot. Mybatis. Dao")
Public class SpringbootMybatisApplication {

Public static void main (String [] args) {
SpringApplication. Run (SpringbootMybatisApplication. Class, args);
}
}

 
# this is my profile
Server:
Port: 8080
The context path:/SBM

Spring:
Profiles:
Active: dev
# database configuration
Datasource:
Url: JDBC: mysql://localhost: 3306/testaclec? useUnicode=true& characterEncoding=utf-8& UseSSL=false
Username: root
Password: 123456
Type: com. Alibaba. Druid. Pool. DruidDataSource
The driver - class - name: com. Mysql.. JDBC driver
# page jump configuration
MVC:
View:
The prefix:/view/
Suffix: HTML

# mybatis configuration
Mybatis:
Mapper - locations: the classpath: XML mapper/*.
Type - aliases - package: com. Springboot. Mybatis. Model

# paging plug-in configuration
Pagehelper:
HelperDialect: mysql
Reasonable: true
SupportMethodsArguments: true
Params: count=countSql

Logging:
XML config: the classpath: logback - spring.

CodePudding user response:

Don't sink! Is there a master!

CodePudding user response:

ok! Heavy, don't know can top up,

CodePudding user response:

You may run the Spring in the boot of @ SpringBootApplicatioin @ ComponentScan (excludeFilters={
@ Filter (type=FilterType. CUSTOM classes=TypeExcludeFilter. Class),
@ Filter (type=FilterType. CUSTOM classes=AutoConfigurationExcludeFilter. Class)}) he won't define your own scan packages, should have your your @ SpringBootApplicatioin run classes on the top, for example: com. KKK. CLM on com. KKK can scan inside the

CodePudding user response:

@ the flying fish do you mean to @ SpringBootApplication (scanBasePackages="com. Springboot. Mybatis") that, not tried!

CodePudding user response:

Not to change that, the custom package, he will not go to scan
/* *
* this is to start the application code
*/
@ SpringBootApplication (scanBasePackages="com")
@ MapperScan (basePackages="com. Springboot. Mybatis. Dao")
Public class SpringbootMybatisApplication {

Public static void main (String [] args) {
SpringApplication. Run (SpringbootMybatisApplication. Class, args);
}
}
@ SpringBootApplication this annotation will not scan the custom package, whether you write a lot of package names are not enough, @ MapperScan (basePackages="com. Springboot. Mybatis. Dao") this annotation on the face don't work directly removed, directly move the class to com directory, you can

CodePudding user response:

There are some great god have different opinions, please give advice or comments,

CodePudding user response:

@ EnableEurekaClient
@ SpringBootApplication
@ ComponentScan (" com. XXX. *) //here need to be able to scan to you annotate the HttpAspect
Public class PheryWebApplication
{

Public static void main (String [] args)
{
SpringApplication. Run (PheryWebApplication. Class, args);
}
}

You put your HttpAspect casually into the other class, can see if injection, I can't, I will add the scan package, can be injected, annotation takes effect

CodePudding user response:

@ the flying fish thank you for your inspiration

CodePudding user response:

@ ComponentScan (basePackages="com. ZCW)
@ Component//the essential, otherwise the aop not
@ Aspect//open section of Aop

//not only using the @ Component, because only use this annotation can quote aop aspects class injection container failure (I) here,
//need to add @ ComponentScan (basePackages="XXX. XXX. XXX") scans the package to inject success

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related