CDI has the feature of Specialization, and I'm looking for that in the Spring world.
Details.
In CDI, the @Specializes
annotation allows one to change the behaviour of a bean just by overriding it. This is completely transparent to users of that bean, e.g. if we'd have
public class OneBean {
public String whoAmI() { return "OneBean"; }
}
@Specializes
public class AnotherBean extends OneBean {
@Override
public String whoAmI() { return "AnotherBean"; }
}
we could
public class SomewhereElse {
@Inject
OneBean oneBean; // we know nothing of AnotherBean here!
public void guessWhosThere() {
return oneBean.whoAmI(); // yet it returns "AnotherBean"
}
}
This gets really useful as soon as OneBean
is actually used with and without AnotherBean
. For example, if OneBean
is in one.jar
and AnotherBean
is in another.jar
, we can change the bean's behaviour just by reconfiguring the classpath.
Question. Does something like Specialization also exist in Spring?
I could only find the @Primary
annotation, which however has a different semantics: @Primary
does not replace one bean, but only marks one of multiple alternatives as the primary one. Especially, as I understood, I could not build a deep inheritance hierarchy as it's possible with @Specializes
.
Bean life cycle is managed by the spring container. When we run the program then, first of all, the spring container gets started. After that, the container creates the instance of a bean as per the request, and then dependencies are injected. And finally, the bean is destroyed when the spring container is closed.
This scopes the bean definition to a single instance per Spring IoC container (default). This scopes a single bean definition to have any number of object instances. This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.
So consider the following configuration file in case you want to use any annotation in your Spring application. Sr.No. The @Required annotation applies to bean property setter methods. The @Autowired annotation can apply to bean property setter methods, non-setter methods, constructor and properties.
Short answer In Spring 4, this is not possible. Period. Still, in 2016, nothing like this is possible with Spring's obsolete dependency injection model.
Seems like there is no similar annotation in spring, but you can achive it via @Qualifier.
Beans:
@Resource("oneBean")
public class OneBean {
public String whoAmI() { return "OneBean"; }
}
@Resource("anotherBean")
public class AnotherBean extends OneBean {
@Override
public String whoAmI() { return "AnotherBean"; }
}
SomewhereElse:
public class SomewhereElse {
@Autowired
@Qualifier("anotherBean")
OneBean oneBean;
public void guessWhosThere() {
return oneBean.whoAmI(); // returns "AnotherBean"
}
}
Edited.
Also you can develop your own annotation and use it in BeanPostProcessor, look at spring docs here
OR even better to use CustomAutowireConfigurer, see here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With