简体   繁体   English

是spring framework 3.0类型安全的

[英]is spring framework 3.0 type-safe

In another question I asked, raised a concern that spring framework is not type safe. 在我问的另一个问题中 ,提出了一个问题,即spring框架不是类型安全的。 Is it true, or fixed, and can you give an example what it means exactly? 它是真的还是固定的,你能给出一个例子吗?

First of all, what does "type-safe" mean for a dependency injection framework. 首先,“类型安全”对于依赖注入框架意味着什么。 What I can think of is that you can get a bean from the context by specifying a type, and not just a bean name. 我能想到的是,您可以通过指定类型而不仅仅是bean名称来从上下文中获取bean。 Spring 3 allows this. Spring 3允许这样做。

Otherwise, type-safety means that when you can define your dependencies by their type. 否则,类型安全意味着您可以按类型定义依赖项。 And you can do this in all versions of spring. 你可以在所有版本的弹簧中执行此操作。

Another thing is compile-time safety. 另一件事是编译时安全。 With spring pre-3.0 when you had to differentiate between two beans that share the same interface (or supertype) by using their string-based name. 使用spring 3.0之前的版本时,必须使用基于字符串的名称来区分共享相同接口(或超类型)的两个bean。 In spring 3.0 you can use annotation-based qualifiers (using javax.inject.Qualifier ), so it is compile-time safer as well. 在Spring 3.0中,您可以使用基于注释的限定符(使用javax.inject.Qualifier ),因此编译时也更安全。

Another thing to mention is the use of generics. 另外要提到的是泛型的使用。 You can have, for example @Inject List<MyService> in spring. 你可以在春天使用@Inject List<MyService>

Define a custom annotation using @Qualifier 使用@Qualifier定义自定义注释

To identify the injected bean without specifying the name, we need to create a custom annotation. 要在不指定名称的情况下识别注入的bean,我们需要创建自定义注释。 This is an equivalent procedure to the use of JSR 330 annotations(Inject) in CDI. 这是在CDI中使用JSR 330注释(Inject)的等效过程。

@Target({ElementType.Field, ElementType.Parameter})  
@Retention(RetentionPolicy.RUNTIME)  
@Qualifier  
public @Interface Student  {  
}  

Now assign this custom annotation to implementation of EntityDao Interface 现在将此自定义注释分配给EntityDao接口的实现

@Component  
@Student  
public class StudentDao implements EntityDao   {  
}  

@Component tells Spring that this a bean definition. @Component告诉Spring这是一个bean定义。 @Student annotation is used by Spring IoC to identify StudentDao as EntityDao's implementation whenever reference of EntityDao is used. 只要使用EntityDao的引用,Spring IoC就会使用@Student注释来识别StudentDao作为EntityDao的实现。 Inject the bean using @Autowired and custom qualifier Something like this. 使用@Autowired和自定义限定符注入bean这样的东西。

@Autowired  
@Student  
private EntityDao studentDao; // So the spring injects the instance of StudentDao here.  

This makes less use of String-names, which can be misspelled and are harder to maintain. 这减少了字符串名称的使用,这可能拼写错误并且难以维护。 - I find this post very useful. - 我发现这篇文章非常有用。 http://www.coolcoder.in/2011/08/how-to-use-type-safe-dependency.html http://www.coolcoder.in/2011/08/how-to-use-type-safe-dependency.html

It depends on how you use it and what you mean by type-safe (see Bozho's answer for more info on the latter): if you use the xml config to produce your beans, then you're probably type-safe after start-up. 这取决于你如何使用它以及你的类型安全的意思(有关后者的更多信息,请参阅Bozho的答案):如果你使用xml配置生成你的bean,那么你在启动后可能是类型安全的。

However, if you use the new Java Bean config (which has its own limitations) you get compile-time safety. 但是,如果您使用新的Java Bean配置(它有自己的限制),您将获得编译时的安全性。

I'm not advocating the latter over the former, but it's something to consider. 我不是在提倡后者而是前者,但这是需要考虑的事情。

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

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