-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 将 EnableAopLogMethod 拆分为2个注解 "EnableAopLogMethod/EnableAopL…
…ogController",语意和功能清晰明了
- Loading branch information
Showing
10 changed files
with
199 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...rc/main/java/app/myoss/cloud/web/spring/web/method/aspectj/AopLogControllerRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2018-2018 https://github.com/myoss | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package app.myoss.cloud.web.spring.web.method.aspectj; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.context.ResourceLoaderAware; | ||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; | ||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | ||
import org.springframework.core.annotation.AnnotationAttributes; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.core.type.AnnotationMetadata; | ||
import org.springframework.core.type.filter.AnnotationTypeFilter; | ||
import org.springframework.util.ClassUtils; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
|
||
import app.myoss.cloud.web.spring.web.method.aspectj.annatation.EnableAopLogController; | ||
|
||
/** | ||
* 扫描当前package下的 {@link org.springframework.stereotype.Component},并进行 Bean 的自动注册 | ||
* | ||
* @author Jerry.Chen | ||
* @since 2018年12月28日 下午3:25:48 | ||
*/ | ||
public class AopLogControllerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware { | ||
private ResourceLoader resourceLoader; | ||
|
||
@Override | ||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { | ||
AnnotationAttributes attributes = AnnotationAttributes | ||
.fromMap(importingClassMetadata.getAnnotationAttributes(EnableAopLogController.class.getName())); | ||
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry); | ||
scanner.resetFilters(false); | ||
scanner.setResourceLoader(this.resourceLoader); | ||
List<String> scanPackages = new ArrayList<>(); | ||
|
||
// 监控 controller 异常 | ||
boolean enableAopLogControllerException = attributes.getBoolean("enableAopLogControllerException"); | ||
if (!enableAopLogControllerException) { | ||
scanner.addExcludeFilter(new AnnotationTypeFilter(ControllerAdvice.class)); | ||
} else { | ||
scanner.addIncludeFilter(new AnnotationTypeFilter(ControllerAdvice.class)); | ||
} | ||
|
||
// 扫描注册 | ||
String packageName = ClassUtils.getPackageName(AopLogControllerRegistrar.class); | ||
scanPackages.add(packageName); | ||
scanner.scan(scanPackages.toArray(new String[0])); | ||
} | ||
|
||
@Override | ||
public void setResourceLoader(ResourceLoader resourceLoader) { | ||
this.resourceLoader = resourceLoader; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
.../main/java/app/myoss/cloud/web/spring/web/method/aspectj/MonitorControllerProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2018-2018 https://github.com/myoss | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package app.myoss.cloud.web.spring.web.method.aspectj; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
import app.myoss.cloud.apm.constants.ApmConstants; | ||
import lombok.Data; | ||
|
||
/** | ||
* 使用slf4j记录 {@link org.springframework.stereotype.Controller} 的信息,属性配置 | ||
* | ||
* @author Jerry.Chen | ||
* @since 2018年4月13日 下午12:15:37 | ||
*/ | ||
@Data | ||
@ConfigurationProperties(prefix = ApmConstants.MONITOR_CONTROLLER_CONFIG_PREFIX) | ||
public class MonitorControllerProperties { | ||
/** | ||
* Controller异常时返回的errorCode | ||
* | ||
* @see AopLogControllerExceptionHandler#outputException(Throwable, | ||
* org.springframework.http.HttpHeaders, | ||
* org.springframework.http.HttpStatus, | ||
* javax.servlet.http.HttpServletRequest) | ||
*/ | ||
private String controllerExceptionErrorCode = "systemException"; | ||
/** | ||
* Controller异常时返回的errorCode | ||
* | ||
* @see AopLogControllerExceptionHandler#outputException(Throwable, | ||
* org.springframework.http.HttpHeaders, | ||
* org.springframework.http.HttpStatus, | ||
* javax.servlet.http.HttpServletRequest) | ||
*/ | ||
private String controllerExceptionErrorMsg = "We'll be back soon ..."; | ||
} |
63 changes: 63 additions & 0 deletions
63
...java/app/myoss/cloud/web/spring/web/method/aspectj/annatation/EnableAopLogController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2018-2018 https://github.com/myoss | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package app.myoss.cloud.web.spring.web.method.aspectj.annatation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Import; | ||
|
||
import app.myoss.cloud.web.spring.web.method.aspectj.AopLogControllerRegistrar; | ||
import app.myoss.cloud.web.spring.web.method.aspectj.MonitorControllerProperties; | ||
|
||
/** | ||
* 开启自动记录 {@link org.springframework.stereotype.Controller} 信息 | ||
* <ul> | ||
* <li>{@link app.myoss.cloud.web.spring.web.method.aspectj.AopLogControllerExceptionHandler} | ||
* </ul> | ||
* 使用例子: | ||
* | ||
* <pre> | ||
* @EnableAopLogController | ||
* @Configuration | ||
* public class Config { | ||
* } | ||
* </pre> | ||
* | ||
* @author Jerry.Chen | ||
* @since 2018年12月28日 下午3:25:48 | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
@Documented | ||
@EnableConfigurationProperties(MonitorControllerProperties.class) | ||
@Import(AopLogControllerRegistrar.class) | ||
public @interface EnableAopLogController { | ||
/** | ||
* 开启自动记录 controller异常, | ||
* 使用:{@link app.myoss.cloud.web.spring.web.method.aspectj.AopLogControllerExceptionHandler} | ||
* 处理异常信息 | ||
* | ||
* @return 默认开启(如果不是WebApplication,是不起作用的) | ||
*/ | ||
boolean enableAopLogControllerException() default true; | ||
} |