Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring:将属性作为值注入注释_Spring_Dependency Injection_Annotations_Spring Aop - Fatal编程技术网

Spring:将属性作为值注入注释

Spring:将属性作为值注入注释,spring,dependency-injection,annotations,spring-aop,Spring,Dependency Injection,Annotations,Spring Aop,我试图向自定义注释中注入一个值,但Spring似乎没有进行计算 这是我的注解: @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyCustomAnno { String var1(); String var2(); } public class MyClass { @MyCustomAnno(var1 = "${some.property.one}",

我试图向自定义注释中注入一个值,但Spring似乎没有进行计算

这是我的注解:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnno {
    String var1();
    String var2();
}
public class MyClass {

    @MyCustomAnno(var1 = "${some.property.one}",
                  var2 = "${some.property.two}")
    public void someMethod() {
    // do something here
    }
}
@Aspect
public class MyAop {

    @Around(value="@annotation(myCustomAnno)",argNames="myCustomAnno")
    public Object aroundMethod(MyCustomAnno myCustomAnno) {
        int intVar1 = Integer.parseInt(myCustomAnno.var1());
        int intVar2 = Integer.parseInt(myCustomAnno.var2());

        // ....
    }
}
这是我的Bean,它是在带有注释的Spring配置文件中创建的:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnno {
    String var1();
    String var2();
}
public class MyClass {

    @MyCustomAnno(var1 = "${some.property.one}",
                  var2 = "${some.property.two}")
    public void someMethod() {
    // do something here
    }
}
@Aspect
public class MyAop {

    @Around(value="@annotation(myCustomAnno)",argNames="myCustomAnno")
    public Object aroundMethod(MyCustomAnno myCustomAnno) {
        int intVar1 = Integer.parseInt(myCustomAnno.var1());
        int intVar2 = Integer.parseInt(myCustomAnno.var2());

        // ....
    }
}
以下是我尝试使用传递到注释中的值的方面:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnno {
    String var1();
    String var2();
}
public class MyClass {

    @MyCustomAnno(var1 = "${some.property.one}",
                  var2 = "${some.property.two}")
    public void someMethod() {
    // do something here
    }
}
@Aspect
public class MyAop {

    @Around(value="@annotation(myCustomAnno)",argNames="myCustomAnno")
    public Object aroundMethod(MyCustomAnno myCustomAnno) {
        int intVar1 = Integer.parseInt(myCustomAnno.var1());
        int intVar2 = Integer.parseInt(myCustomAnno.var2());

        // ....
    }
}
在around方法中,我接收到一个NumberFormatException:对于输入字符串:${some.property.one}。这意味着Spring出于某种原因没有注入该值

如果您想知道,在同一个类中,我可以进行正常的@Value注释,并且正确地注入值:

@Value("${some.property.one}")
private propertyOne;            // This works

是否可以执行我想执行的操作?如果可以,如何执行?

好的,占位符在自定义注释中不会解析。但是,您可以在方面本身中解决它们

例如:

@Aspect
class MyAop implements EmbeddedValueResolverAware {

    private StringValueResolver resolver;

    @Around(value="@annotation(myCustomAnno)",argNames="myCustomAnno")
    public void play(MyCustomAnno ann) {
        System.out.println(resolver.resolveStringValue(ann.var1()));
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.resolver = resolver;
    }
}