Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Specializes in Spring

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.

like image 744
fxnn Avatar asked Nov 13 '14 15:11

fxnn


People also ask

What is bean life cycle in Spring?

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.

What are bean scopes in Spring?

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.

How do you use annotations in Spring?

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.


2 Answers

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.

like image 187
Pavel Pscheidl Avatar answered Sep 17 '22 15:09

Pavel Pscheidl


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

like image 24
Maksym Avatar answered Sep 20 '22 15:09

Maksym