Getting a bean with all its dependencies using Spring ServiceFactoryBean - spring

I have custom factory class to get a bean using Spring's service factory bean. However, the bean that I want to get from this factory bean, has nested bean dependencies. My question is how do I get a bean from this factory with all its nested dependencies met?
Spring Config :
<bean id="beanFactory"
class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean">
<property name="serviceLocatorInterface" value="com.example.MyBeanFactory">
</bean>
Public class MyBean
{
JobBuilder builder;
}
Public interface MyBeanFactory
{
MyBean getMyBean();
}
Now when I do MyBeanFactory.getMyBean()... I want have to have MyBean instance that includes JobBuilde instance, inside it.

You can treat the implementation class as any other class in the Spring application - just autowire any dependencies that you need. One suggestion I have is to define an interface for the bean class. Example:
public interface MyBean {
void doStuff();
}
Provide an implementation:
#Component("defaultMyBean")
public class DefaultMyBean {
#Autowire
JobBuilder jobBuilder;
public void doStuff() {
// do stuff with jobBuilder
}
}
And then the factory interface:
public interface MyBeanFactory {
MyBean getMyBean();
}
Configuration remains unchanged.

Related

Spring - #Autowired bean from #Configuration is null

I define a bean in a configuration class. I would like to autowire this bean in a component. However, the property stays null. E.g.:
Appconfiguration.java
#Configuration
public class AppConfiguration {
#Bean
public SomeType someObject() {
return new SomeType();
}
}
AppComponent.java
#Component
public class AppComponent {
#Autowired
private SomeType someObject; // TODO why null?
public AppComponent() { // note: passing a SomeType constructor argument works
System.out.println(someObject);
}
}
How can I autowire a bean defined in a configuration class?
Are you missing a fundamental detail that properties are injected after a bean is created? Have you tried access your property after a bean is fully initialized?
Update:
I've reworked your example a little to demonstrate you the difference:
#Test
public void initializationTest() {
try (AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext()) {
context.register(AppConfiguration.class, AppComponent.class);
context.refresh();
}
}
#Configuration
public class AppConfiguration {
#Bean
public SomeType someObject() {
return new SomeType();
}
}
#Component
public class AppComponent {
#Autowired
private SomeType someObject;
public AppComponent() {
// Here properties are not yet injected by Spring IoC container
System.out.println(someObject); // Obviously prints null
}
/**
* Method is invoked after a bean is initialized and all its properties are being set.
*/
#PostConstruct
private void init() {
System.out.println(someObject); // Prints SomeType#6b419da
}
}
public class SomeType {
}
So basically bean lifecycle consists of the following steps:
1. Bean instance is created
2. Bean properties are set
3. In case bean implements Aware interfaces - those implemented methods are invoked
4. BeanPostProcessor#postProcessBeforeInitialization methods of custom bean post-processors are invoked
5. Initialization callbacks are invoked in the following order:
5.1. #PostConstruct method is invoked
5.2. InitializingBean#afterPropertiesSet() method is invoked
5.3. #Bean#initMethod() method is invoked
Bean is fully initialized now.
6. BeanPostProcessor#postProcessAfterInitialization methods of custom post-processors are invoked
are invoked.
7. Destruction callbacks are invoked in the following order:
7.1. #PreDestroy method is invoked
7.2. DisposableBean#destroy() method is invoked
7.3. #Bean#destroyMethod method is invoked

Does registering bean inside #Component class respect #Scope?

This website says beans registered inside component classes are not cglib proxied and do not go through the spring container. So does this mean if I register a bean inside a component class (snippet below), adding #Scope("request") wont make any difference, and a new instance of AnotherBean will always be created whenever testBean.anotherBean() is called from some external class?
#Component
public class TestBean {
#Bean
#Scope("request")
public AnotherBean anotherBean() {
return new AnotherBean();
}
}
The bean that is not cglib proxied is the #Component itself, not the bean registered using the #Bean annotation. If you are not calling the anotherBean method explicitly, it won't make a difference because the proxy is used to return the bean when the method annotated with #Bean is called. See the example
The bean testBeanComponent is not cglib proxied :
#Component
public class TestBeanComponent {
#Bean
#Scope("request")
public AnotherBeanComponent anotherBeanComponent() {
return new AnotherBeanComponent();
}
}
The bean testBeanConfiguration is cglib proxied :
#Configuration
public class TestBeanConfiguration {
#Bean
#Scope("request")
public AnotherBeanConfiguration anotherBeanConfiguration() {
return new AnotherBeanConfiguration();
}
}
What it mean :
#Service
public class TestService {
#Autowired //Inject a normal bean
private TestBeanComponent testBeanComponent;
#Autowired //Inject a proxy
private TestBeanConfiguration testBeanConfiguration;
public void test() {
//Calling anotherBeanComponent always return a new instance of AnotherBeanComponent
testBeanComponent.anotherBeanComponent()
.equals(testBeanComponent.anotherBeanComponent()); // is false
//Calling anotherBeanConfiguration return the bean managed by the container
testBeanConfiguration.anotherBeanConfiguration()
.equals(testBeanConfiguration.anotherBeanConfiguration()); // is true
}
}
But if you are injecting the bean instead of using the method, everything will work as you expected :
#Service
public class TestService2 {
#Autowired //Inject a proxy with scope request
private AnotherBeanComponent anotherBeanComponent;
#Autowired //Inject a proxy with scope request
private AnotherBeanConfiguration anotherBeanConfiguration;
}

Spring Autowired annotation with service implementation

I have few doubts in the scenario below.
Having one interface and two implementation classes:
Interface:
interface ServiceInt
{
public void save();
}
First implementation class:
Public class ServiceIntImpOne implements ServiceInt
{
public void save()
{
// I am first service implementation
}
}
Second implementation class:
Public class ServiceIntImpTwo implements ServiceInt
{
public void save()
{
// I am Second service implementation
}
}
Main class calling the save method of an implementation class:
Public class controller
{
#Autowired ServiceInt;
public void save()
{
ServiceInt.save()
}
}
My Questions:
ServiceInt.save() – which save method of service class implementation will it invoke?
How we can use the save method implementation of the ServiceIntImpTwo class?
How does autowired work?
ServiceInt.save() – which save method of service class implementation
will it invoke?
If you have two beans of the same type (beans defined via annotations or via xml) when you autowire that beans it will fail (throw exception), because Spring don't know what bean to inject.
caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No unique bean of type [ServiceInt] is defined:
How we can use the save method implementation of the ServiceIntImpTwo
class?
If you defined your bean in a xml file, then you can use the id property
<bean id="beanTwo" class="ServiceIntImpTwo" />
then you can autowire using Qualifier annotation
#Autowired
#Qualifier("beanTwo")
ServiceInt myServiceTwo;
if you are using annotations instead.
#Component
Class ServiceIntImpTwo impl...{}
Then you can autowire
#Autowired
#Qualifier("serviceIntImpTwo ")
ServiceInt myServiceTwo;
How does autowired work?
This you can read it on Internet.
http://memorynotfound.com/handling-multiple-autowire-dependencies-with-spring-qualifier/
https://www.mkyong.com/spring/spring-autowiring-qualifier-example/
https://www.tutorialspoint.com/spring/spring_qualifier_annotation.htm
If you have two implementation to one interface you will need to provide spring hint which one to use:
you can use #Qaulifier annotation
you can use #Profile and choose the active profile.

Java class xml vs java bean autowiring

In xml defined beans you can define two classes like this
<bean id="classA" class="ex.ClassA"/>
<bean id="classB" class="ex.classB"/>
Then in your java implementation you can autowire the constructor of one of the classes in example
public class ClassA {
#autowired
public(ClassB classB){
this.classB = classB;
}
Now how does one do that with java config beans since in example
#Bean
public ClassA classA(){
return new ClassB();
}
#Bean
public ClassB classB(){
return new ClassB()
}
the compiler would warn that Class a does not have any such constructor, how does one do that in java, with autowiring?
Thanks all
See http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-java-injecting-dependencies
Note that the ClassB bean is implicitly a singleton. The use of the annotation #Configuration on the Config class ensures that Spring returns the singleton instance of the ClassB bean in the classB() call.
#Configuration
public class Config {
#Bean
public ClassA classA(){
return new ClassA( classB() );
}
#Bean
public ClassB classB(){
return new ClassB();
}
}
Or you may prefer this approach (Spring 4.2.1+ required)
#Configuration
#Import(ClassA.class)
public class Config {
#Bean
public ClassB classB(){
return new ClassB();
}
}
#Component
public class ClassA {
#Autowired
public ClassA(ClassB classB) {
...
}
}
Pass the beans you want as parameters to the #Bean method, or use component scanning to create the dependent bean implicitly.

Injecting a bean inside another bean and use it in the constructor?

I have a bean that looks like this:
#Component
#Scope("session")
public class AlarmChartSettingsBean implements Serializable {
...
Inside this bean i inject another bean like this:
#Inject
private SessionInfoBean sessionInfoBean;
Then i call the injected bean inside the constructor of the first bean like this:
public AlarmChartSettingsBean() {
String atcaIp = sessionInfoBean.getNwConfigBean().getAtcaIP();
}
The problem is that the injected bean is null. So the question is when is that bean injected? Can i use it inside the constructor or i should use it after the bean has been constructed?
The constructor of a Spring bean is called before Spring has any chance to autowire any fields. This explains why sessionInfoBean is null inside the constructor.
If you want to initialize a Spring bean, you can:
annotate a method with #PostConstruct:
#PostConstruct
public void init() {
String atcaIp = sessionInfoBean.getNwConfigBean().getAtcaIP();
}
implement InitializingBean and write the initialization code inside the afterPropertiesSet method:
public class AlarmChartSettingsBean implements Serializable, InitializingBean {
#Override
void afterPropertiesSet() {
String atcaIp = sessionInfoBean.getNwConfigBean().getAtcaIP();
}
}
The #Inject on a Field will autowire after the constructor has been called.
Note: In some Spring-Apps the #Inject may not work, use #Autowire instead.

Resources