在运行时动态创建Spring Bean

翻译是为“ Spring Framework开发人员”课程的未来学生特别准​​备的


五年来,关于动态bean创建的文章已成为我博客上最受欢迎的文章(超过9300次浏览)。现在该更新它了。我还在Github上添加了一个示例

Github上的Spring Bean动态
Spring Bean Github

: " Spring Bean , ". , . properties-.

1. , , :

package your.package;

@Retention(RetentionPolicy.RUNTIME)
public @interface InjectDynamicObject {
}

2. , :

@Service
public class CustomerServiceImpl {
    private Customer dynamicCustomerWithAspect;
    
    @InjectDynamicObject
    public Customer getDynamicCustomerWithAspect() {
        return this.dynamicCustomerWithAspect;
    }
}

3. Pointcut Advise, , 2:

@Component
@Aspect
public class DynamicObjectAspect {
    // This comes from the property file
    @Value("${dynamic.object.name}")
    private String object;
    @Autowired
    private ApplicationContext applicationContext;
    
    @Pointcut("execution(@com.lofi.springbean.dynamic.
        InjectDynamicObject * *(..))")
    public void beanAnnotatedWithInjectDynamicObject() {
    }
    @Around("beanAnnotatedWithInjectDynamicObject()")
    public Object adviceBeanAnnotatedWithInjectDynamicObject(
        ProceedingJoinPoint pjp) throws Throwable {   
        pjp.proceed();
        
        // Create the bean or object depends on the property file  
        Object createdObject = applicationContext.getBean(object);
        return createdObject;
    }
}

4. , @InjectDynamicObject. properties-. Customer: CustomerOneImpl CustomerTwoImpl:

@Component("customerOne")
public class CustomerOneImpl implements Customer {
    @Override
    public String getName() {
        return "Customer One";
    }
}

application.properties
dynamic.object.name=customerOne

5. :

@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerServiceImplTest {
    @Autowired
    private CustomerServiceImpl customerService;
    @Test
    public void testGetDynamicCustomerWithAspect() {
        // Dynamic object creation
        logger.info("Dynamic Customer with Aspect: " +
            customerService.getDynamicCustomerWithAspect()
            .getName());
}

, , . AspectJ, Spring. Map . eXTra Client. PluginsLocatorManager. Spring Map (String) .

"… Map , String. Map , — ".

. Spring.

@Service
public class CustomerServiceImpl {
    
    // We inject the customer implementations into a Map
    @Autowired
    private Map<String, Customer> dynamicCustomerWithMap;
    
    // This comes from the property file as a key for the Map
    @Value("${dynamic.object.name}")
    private String object;
    public Customer getDynamicCustomerWithMap() {
        return this.dynamicCustomerWithMap.get(object);
    }
}

" Spring Framework" .




All Articles