- Building Microservices with Spring
- Dinesh Rajput Rajesh R V
- 273字
- 2021-07-02 14:54:11
Sample implementation of FactoryBean interface
Suppose you have a TransferService class whose definition is thus:
package com.packt.patterninspring.chapter4.bankapp.service; import com.packt.patterninspring.chapter4.
bankapp.repository.IAccountRepository; public class TransferService { IAccountRepository accountRepository; public TransferService(IAccountRepository accountRepository){ this.accountRepository = accountRepository; } public void transfer(String accountA, String accountB, Double
amount){ System.out.println("Amount has been tranferred"); } }
And you have a FactoryBean whose definition is thus:
package com.packt.patterninspring.chapter4.bankapp.repository; import org.springframework.beans.factory.FactoryBean; public class AccountRepositoryFactoryBean implements
FactoryBean<IAccountRepository> { @Override public IAccountRepository getObject() throws Exception { return new AccountRepository(); } @Override public Class<?> getObjectType() { return IAccountRepository.class; } @Override public boolean isSingleton() { return false; } }
You could wire up an AccountRepository instance using a hypothetical AccountRepositoryFactoryBean like this:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="transferService" class="com.packt.patterninspring.
chapter4.bankapp.service.TransferService"> <constructor-arg ref="accountRepository"/> </bean> <bean id="accountRepository"
class="com.packt.patterninspring.chapter4.
bankapp.repository.AccountRepositoryFactoryBean"/> </beans>
In the preceding example, the TransferService class depends on the AccountRepository bean, but in the XML file, we have defined AccountRepositoryFactoryBean as an accountRepository bean. The AccountRepositoryFactoryBean class implements the FactoryBean interface of Spring. The result of the getObject method of FactoryBean will be passed, and not the actual FactoryBean itself. Spring injects that object returned by FactoryBean's getObjectType() method, and the object type returned by FactoryBean's getObjectType(); the scope of this bean is decided by the FactoryBean's isSingleton() method.
The following is the same configuration for the FactoryBean interface in a Java Configuration:
package com.packt.patterninspring.chapter4.bankapp.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.packt.patterninspring.chapter4.bankapp.
repository.AccountRepositoryFactoryBean; import com.packt.patterninspring.chapter4.
bankapp.service.TransferService; @Configuration public class AppConfig { public TransferService transferService() throws Exception{ return new TransferService(accountRepository().getObject()); } @Bean public AccountRepositoryFactoryBean accountRepository(){ return new AccountRepositoryFactoryBean(); } }
As other normal beans in the Spring container, the Spring FactoryBean also has all the other characteristics of any other Spring bean, including the life cycle hooks and services that all beans in the Spring container enjoy.