If I have a bean created in Java, with @Bean, how do I reference this bean in XML when creating a bean there? And if I have a bean created in XML, how do I reference this bean in Java when creating a bean there?
3 Answers
Creating a bean in Java class is equivalent to creating the bean in the XML file. So if you want to refer a bean created in Java class in an XML file, simply use ref= beanName
attribute to refer the bean and vice versa.
In the official documentation it says:
4.2.1. Declaring a bean
To declare a bean, simply annotate a method with the @Bean annotation. When JavaConfig encounters such a method, it will execute that method and register the return value as a bean within a BeanFactory. By default, the bean name will be that of the method name.
@Configuration
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}
The above is exactly equivalent to the following appConfig.xml:
<beans>
<bean name="transferService" class="com.company.TransferServiceImpl"/>
</beans>
Both will result in a bean named transferService being available in the BeanFactory/ApplicationContext, bound to an object instance of type TransferServiceImpl:
And also you need to make sure you add
<bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/>
in your xml so your XML configuration and Java configuration has same bean definitions if they are declared in different contexts.
And if you want to include your traditional bean configuration from XML to Java configuration, then you need to Import the XML resource to that class as below:
@ImportResource({"classpath*:your-configuration.xml"})
-
1Also if you would like to read more about this I would suggest you to have a look at the official documentation: docs.spring.io/spring-javaconfig/docs/1.0.0.m3/reference/html/… Sep 9, 2019 at 5:51
-
-
Thanks! Could you elaborate on the last Bean, the ConfigurationPostProcessor. What does it do, and why do I always need it when beans are declared both in Java and XML?– KimsSep 10, 2019 at 4:58
-
It is added to pick up all the beans declared in Java configuration from an XML file so as Spring container can pick and register those beans. However, you can also add <context:annotation-config/> in XML file which will add all the postbeanProcessors including ConfigurationPostProcessor. Sep 10, 2019 at 5:17
-
1bean declaration in java and using them in XML: you need <context:annotation-config/>(if registering all processors). bean declaration in XML and using them in Java: you need the ImportResource annotation Sep 10, 2019 at 6:03
Other answers only answer part of the picture. Let me try to summarize the big picture. The key point is that every bean has an identifier which is unique within the spring container. We can use this identifier to reference a bean.
The bean identifier can be configured by :
- XML :
id
andname
attribute in the<bean/>
- Java Config :
value
andname
attribute in@Bean
.value
attribute in@Component
and its stereotype annotation such as@Service
,@Controller
etc.
If the identifier is not configured explicitly , depending on how the beans are declared , a default one will be generated for them:
For beans declared using XML,
@Component
and its stereotype , it is the class name of that bean formatted in lowerCamelCase.For beans declared using
@Bean
, it is the name of that bean method
To reference a bean declared in Java configuration in XML , use ref
property to refer to its identifier such as :
<bean name="foo" class="com.example.Foo">
<property name="service" ref="bar"/>
</bean>
So service
property is reference to a bean which the identifier is called bar
To reference a bean declared in XML in Java , you can simply annotate @Autowired
on a field. If there is only one spring bean which the type is the same as the type of this field (i.e. Bar in this case) , this bean will automatically be referenced and injected . It is called auto-wired by type which you does not require to reference it by the bean identifier :
@Service
public class Foo {
@Autowired
private Bar service;
}
On the other hand , if there are multiple beans which the type are also Bar, you have to use @Qualifier
with the bean identifier to reference a particular bean among these Bar beans. This is called auto-wired by name :
@Service
public class Foo {
@Autowired
@Qualifier("bar123")
private Bar service;
}
You can create a simple Spring bean, Grandmother and add a reference to beans MotherBean and FatherBean.
You have two approaches to do this:
First Approach
GrandMother Bean:
@Service("grandmotherBean")
public class Grandmother {
@Autowired
private MotherBean motherBean;
@Autowired
private FatherBean father;
}
Mother Bean here
@Service("motherBeanBean")
public class MotherBean {
public MotherBean() {
}
@Override
public String toString() {
return " it is me ! MotherBean";
}
}
Father Bean Here
@Service("fatherBeanBean")
public class FatherBean {
public FatherBean (){
}
@Override
public String toString() {
return " it is me ! FatherBean";
}
}
And another way is you can configure Spring bean references directly to the applicationContext.xml
Second Approach
<beans xmlns="http://www.springframework.org/schema/beans"
<bean id="Grandmother"
class="com.dulaj.stack.Grandmother">
<property name="FatherBean" >
<ref local="fatherBeanBean"/>
</property>
</bean>
<bean id="fatherBeanBean" class="com.dulaj.stack.FatherBean">
</bean>
</beans>
-
1