Announcement Announcement Module
Collapse
No announcement yet.
ServiceLocatorFactoryBean Java Configuration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ServiceLocatorFactoryBean Java Configuration

    Can someone please point me in the right direction.
    I have a service locator:
    Code:
    @Component
    public class DefaultServiceLocator {
      
     @Autowired
         private ServiceLocator serviceLocatorFactoryBean;
    
         public void useService(String name) { // dosomething
         }
    }
    This is injected from the xml config:
    Code:
    <bean id="serviceLocatorFactoryBean"
    		class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean">
    		<property name="serviceLocatorInterface" value="uk.co.niktest.services.locators.ServiceLocator" />
    	</bean>
    I want to change this to java config... so I tried...
    Code:
    @Configuration
    public class AppConfig {
    	
    	@Bean
    	public  ServiceLocatorFactoryBean serviceLocatorFactoryBean(){		
    		ServiceLocatorFactoryBean factory = new ServiceLocatorFactoryBean();
    		factory.setServiceLocatorInterface(uk.co.niktest.services.locators.ServiceLocator.class);		
    		return factory;		
    	}
    }
    Spring finds and loads this bean, but fails to inject the proxy factory into the DefaultServiceLocator

    Can this be replicated as java config? I can't seem to find any examples .

    cheers

  • #2
    I was looking at exactly the same issue. The following is what worked for me

    Code:
    @Configuration
    public class MyFactoryConfig {
    
        @Bean
        public ServiceLocatorFactoryBean myFactoryServiceLocatorFactoryBean() {
            ServiceLocatorFactoryBean bean = new ServiceLocatorFactoryBean();
            bean.setServiceLocatorInterface(MyFactory.class);
            return bean;
        }
    
        @Bean
        public MyFactory myFactory() {
            return (MyFactory) myFactoryServiceLocatorFactoryBean().getObject();
        }
    }

    Comment

    Working...
    X