简体   繁体   English

如何包装Spring bean

[英]How to wrap Spring beans

I am new to spring framework.. 我是Spring框架的新手..

Here is my question: How i can wrap beans in runtime onto another class? 这是我的问题:我如何将运行时的bean包装到另一个类?

I have classes as follows for every data struct && java type: 对于每个数据结构&& java类型,我都有以下类:

@Component
    public class ByteCodec extends Codec<Byte> {
    public ByteCodec() {
        super(Byte.class);
    }

    public void encode(... buffer, Byte object) {
        buffer.writeByte(object);
    }

    public Byte decode(... buffer) {
        return buffer.readByte();
    }
}

and this class is a managed spring singleton. 这个班级是一个有管理的春天单身人士。 I need to wrap that codec by next class: 我需要通过下一个类包装该编解码器:

class OptionalCodec<T> extends Codec<Boolean> {
    public OptionalCodec(Codec<T> clazz) {
    }
    ... some implementation of encode && decode method's ...
}

How i can do this? 我怎么能这样做? Hint: i want automatic wrap in RUNTIME for every Codec instance.. 提示:我希望每个Codec实例都在RUNTIME中自动换行。

And how to extend Autowired annotation, like that: 以及如何扩展Autowired注释,如下所示:

@AutowireCodec(targetClass=Integer.class, canBeNull=false)
private Codec<Integer> codec;

And how to do registry of the all runtime-created codecs with map: 以及如何使用map执行所有运行时创建的编解码器的注册表:

Map<*MyCodecInfoClass*, Codec>

?? ?? Thanks for any replies! 谢谢你的回复!

Spring allows you to inject beans by types and generic types out of the box, so for your use case I don't think it is really necessary to create a new Autowired annotation. Spring允许您按开箱即用的类型和泛型类型注入bean,因此对于您的用例,我认为没有必要创建新的Autowired注释。 You can simply use the existing @Autowired like this: 您可以像这样简单地使用现有的@Autowired:

@Autowired
private Codec<Byte> codec;

Just keep in mind that if you define more than one bean for the same generic type and you use the code above, you'll get an error because more than one bean exist with that definition. 请记住,如果为同一泛型类型定义多个bean并使用上面的代码,则会出现错误,因为该定义存在多个bean。 You could get around that injecting a collection instead of a single object, for example: 您可以绕过注入集合而不是单个对象,例如:

@Autowired
private List<Codec<Byte>> byteCodecs;

Or if you want all codecs, regardless its generic type you can simply do something like this: 或者,如果您想要所有编解码器,无论其通用类型如何,您都可以执行以下操作:

@Autowired
private List<Codec<?>> allCodecs;

Regarding your question on instance wrapping, I'm not sure if I fully understand what you're trying to achieve but you can inject a codec into another codec like I stated above, or you can take a look to Spring AOP and use it to wrap calls to your beans: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html 关于你关于实例包装的问题,我不确定我是否完全理解你想要实现的目标但是你可以将编解码器注入到我上面提到的另一个编解码器中,或者你可以看看Spring AOP并使用它来包装对bean的调用: http//docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html


I'm still not sure what would be the purpose of of the OptionalCodec as it will accept booleans on the encode/decode methods. 我仍然不确定OptionalCodec的目的是什么,因为它会在编码/解码方法上接受布尔值。 The OptionalCodec is a different class than the base Codec interface, so if you do: OptionalCodec是一个与基本Codec接口不同的类,所以如果你这样做:

@Autowired
private OptionalCodec<Byte> codec;

Will inject the optional Byte codec. 将注入可选的Byte编解码器。 If you do: 如果你这样做:

@Autowired
private Codec<Byte> codec;

Will inject the original Byte codec. 将注入原始的Byte编解码器。 But if you do: 但如果你这样做:

@Autowired
private Codec<Boolean> codec;

It will match all the OptionalCodec beans (because the type signature for an OptionalCodec is Codec<Boolean> ) and throw an error as it will not be able to pick a single one. 它将匹配所有OptionalCodec豆(因为对于类型签名OptionalCodecCodec<Boolean> ),并抛出一个错误,因为它不会能够挑选的单独一个。

That said, if you really need to fine tune the autowiring of same type of beans I suggest you to check this relevant section of Spring documentation where annotations like @Primary and @Qualifier are explained, and let you do exactly that: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers 也就是说,如果你真的需要微调相同类型的bean的自动装配,我建议你查看一下Spring文档的相关部分,其中解释了@Primary@Qualifier等注释,让你这样做: http:// docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM