- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为 Retry 的自定义注释,带有以下 Bean 后处理器:
@Component
public final class RetryBPP implements BeanPostProcessor {
private final Map<String, ClassDefinition> map = new HashMap<>(40);
@Override
public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException {
final Class<?> asClass = bean.getClass();
final Method[] methods = asClass.getMethods();
final List<Method> collect = Stream.of(methods)
.filter(method -> method.isAnnotationPresent(Retriable.class))
.collect(Collectors.toList());
if(!collect.isEmpty()){
this.map.put(beanName,new ClassDefinition(collect,asClass));
}
return bean;
}
@Override
public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
final ClassDefinition definition = this.map.get(beanName);
if(definition != null){
final Class beanClass = definition.baseCLass;
return Proxy.newProxyInstance(beanClass.getClassLoader(), beanClass.getInterfaces(), (proxy, method, args) -> {
if(definition.isMethodPresent(method)){
return this.retry(definition.originalMethod(method),bean,args);
} else{
return method.invoke(bean,args);
}
});
} else{
return bean;
}
}
private Object retry(final Method method,final Object originalBean,Object[] argc){
final Retriable retriable = method.getAnnotation(Retriable.class);
int attempts = retriable.attempts();
while(true){
try{
return method.invoke(originalBean,argc);
}catch (final Exception throwable){
if(this.support(retriable.exceptions(),throwable.getClass()) && attempts != 0){
attempts--;
log.warn("Error on method {} wait to repeat",method.getName());
this.sleep(retriable.delay(),retriable.timeUnit());
} else{
throw new RuntimeException(throwable);
}
}
}
}
@SneakyThrows(InterruptedException.class)
private void sleep(final long time, final TimeUnit timeUnit){
timeUnit.sleep(time);
}
private boolean support(final Class<? extends Exception>[] exceptions,Class<? extends Exception> exception){
boolean support = false;
for (Class _class : exceptions) {
if(_class.equals(exception)){
support = true;
break;
}
}
return support;
}
@AllArgsConstructor
private static final class ClassDefinition{
final List<Method> methods;
final Class baseCLass;
boolean isMethodPresent(final Method method){
return this.methods.stream()
.anyMatch(mthd->methodEquals(mthd,method));
}
Method originalMethod(final Method method){
return this.methods.stream()
.filter(mthd->methodEquals(mthd,method))
.findFirst().orElseThrow(NullPointerException::new);
}
}
}
界面:
public interface Inter {
String qqq(String url);
}
抽象类:
public abstract class Abs implements Inter {
@Override
@Retry
public String qqq(final String url) {
someLogic(url);
}
protected String someLogic(String str);
自定义限定符注释:
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
@Target({ElementType.CONSTRUCTOR, ElementType.TYPE, ElementType.PARAMETER, ElementType.FIELD})
public @interface Qualif {
VV.v type();
}
和实现:
@Service
@Qualif(type = First)
@Slf4j
public final class Impl extends Abs {
@Override
protected String someLogic(String str){...}
}
当我像这样通过限定符 Autowiring 时:
@Autowired
@Qualif(type = First)
Inter inter;
Spring抛出异常没有找到依赖Inter的[com.devadmin.downloader.sites.VideoDownloader]类型的合格bean
:,但是当我从abctract类中删除@Retry注释时,一切都很好。如何解决这个问题。
顺便说一句,我检查了我的类在 ApplicationContext
中扩展了 Abstract 类,但 Spring 没有看到我的自定义限定符
最佳答案
终于找到答案了。对于那些不明白这个问题的人。
我有一个带有方法 foo()
的接口(interface),然后是一个实现该接口(interface)并具有自己的抽象方法的抽象类
abstract class FooAbs impl Inter{
@Override
void foo(){
childMethod();
}
protected void childMethod();
}
最后我用 @Component 扩展了 FooAbs 类。
@Component
class First extends FooAbs{
@Override
void childMethod(){}
}
问题出在动态代理上。
下面的代码无法从类获取接口(interface)
Proxy.newProxyInstance(beanClass.getClassLoader(), beanClass.getInterfaces(), (proxy, method, args) -> {}
动态代理看不到 First
类的 getInterfaces
,因为该类没有直接实现接口(interface),因此我将类签名更改为:
@Component
class First extends FooAbs implement Inter{
@Override
void childMethod(){}
}
此步骤之后
beanClass.getInterfaces()
看到Inter界面。我知道这是一种解决方法,我将重写我的代码以避免这种结构。但它确实有效,也许对某些人会有帮助
关于java - 无法通过限定符注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50811357/
我一直在使用 OpenGL 编写程序。最近,我开始学习OpenGL着色语言。我是新手;因此,请在回答中详细说明。 我的问题是: GLSL 中有哪些不同类型的变量(限定符)? 它们的用途是什么? 它们有
这个问题在这里已经有了答案: typedef pointer const weirdness (6 个答案) 关闭 8 年前。 我有一个结构体 type_s。然后我将指向 struct type_s
我有一个 Point2D 类如下: class Point2D{ int x; int y; public: Point2D(int inX,
当应用于指向指针的指针等时,我在推断什么是 const 时遇到了一些麻烦。即,什么是 const 当你有 const Foo **foo; 我可以在这里更改 **foo 中的内容吗?如 foo[0]
限定符 有时候不知道要匹配多少字符。为了能适应这种不确定性,正则表达式支持限定符的概念。这些限定符可以指定正则表达式的一个给定组件必须要出现多少次才能满足匹配。 下表给出了各种限定符及其含义的说
我有一个包含字符名称的数组结构。我想使用 qsort 按字母顺序对它们进行排序,但是我一直收到一条错误消息,说“初始化丢弃了指针目标类型中的‘const’限定符”。我相信我的 cmpmi() 函数和
我是 Java EE/JSF 的新手,现在阅读 CDI 限定符 - 更改类实现的可能性。这很好,但我有一个问题。据我了解,我可以使用限定符更改类实现,但我需要在使用此实现的任何地方更改它。在一个地方进
假设我有以下目录结构: $ mkdir -p a/1 $ ln -s a b 查找目录,我也得到了符号链接(symbolic link)中的目录: $ print -l */*(/) a/1 b/1
我正在尝试创建一个 const 结构数组,但我不断得到 error initializer element is not a compile time constant 我使用的是keil IDE。这
#include using namespace std; class Point { private: int x, y; // Private data members public:
我想使一个方法有条件地成为常量。也就是说,我们有 template class A { // stuff }; 根据模板参数的值,要么 void method() const; 或 void
由于 const int 特化导致以下错误: #include using std::cout; using std::endl; template void g(T val) { cou
同时适用于移动设备和平板电脑的 Titanium 项目,完美地完成了移动版本,但在平板设计时面临一些复杂性。 下面是 Titanium 支持的限定符矩阵,但我找不到任何针对 Android-Table
我正在研究 C++ 和 gtkmm,试图创建一个自定义类,它调用 .glade 文件来构建一个 Gtk 而不是基本的界面。此类派生自 Gtk::Window,但是当我调用类本身的 get_widget
我编写了以下代码来测试 const 成员函数:当我有数据成员的 const 限定符时,它编译并运行良好char *data 和构造函数的参数。但是,如果我从中删除 const数据成员和构造函数,我得到
您好,我正在使用 C++ 内置算法。我这里有这段代码: #include #include #include using namespace std; bool isDon(string& na
我的问题与 Time 项目有关,我将在下面完整发布该项目以提供背景信息。我所做的大部分都是正确的,但是当涉及到加法和减法运算符定义时,我遇到了范围界定错误的问题。编译器找不到我的私有(private)
我有以下代码- int acb(const uint16 *MsgPtr) { uint16 *p = (MsgPtr + 1); printf("%d", *p); } 我收到以下警告 - 在为 p
我在结构中有一个指向结构实例链表的指针字段。 struct myStruct { some fields... struct list_objects * volatile list;
所以我在维基百科的一篇文章(粗略翻译)中遇到了以下定义: Modifier (programming) - element of source code being a phrase of given
我是一名优秀的程序员,十分优秀!