Building Microservices with Spring
上QQ阅读APP看书,第一时间看更新

Declaring Spring beans in an XML file

As with Java, we have to declare a class as a Spring bean into Spring's XML-based configuration by using an element of the Spring-beans schema as a <bean> element. The <bean> element is the XML analogue to JavaConfig's @Bean annotation. We add the following configuration to the XML-based configuration file:

    <bean id="transferService"    
class="com.packt.patterninspring.chapter4.
bankapp.service.TransferServiceImpl"/> <bean id="accountRepository"
class="com.packt.patterninspring.chapter4.
bankapp.repository.jdbc.JdbcAccountRepository"/> <bean id="transferService"
class="com.packt.patterninspring.chapter4.
bankapp.repository.jdbc.JdbcTransferRepository"/>

In the preceding code, I have created a very simple bean definition. In this configuration, the <bean> element has an id attribute to identify the individual bean definition. The class attribute is expressed as the fully qualified class name to create this bean. The value of the id attribute refers to collaborating objects. So let's see how to configure the collaborating beans to resolve the dependencies in the application.