Java Code Examples for org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory()

The following examples show how to use org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public static void main(String[] args) {
        // 配置 XML 配置文件
        // 启动 Spring 应用上下文
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/META-INF/special-bean-instantiation-context.xml");
        // 通过 ApplicationContext 获取 AutowireCapableBeanFactory
        AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();

        ServiceLoader<UserFactory> serviceLoader = beanFactory.getBean("userFactoryServiceLoader", ServiceLoader.class);

        displayServiceLoader(serviceLoader);

//        demoServiceLoader();

        // 创建 UserFactory 对象,通过 AutowireCapableBeanFactory
        UserFactory userFactory = beanFactory.createBean(DefaultUserFactory.class);
        System.out.println(userFactory.createUser());

    }
 
Example 2
Source Project: cuba   File: AppUI.java    License: Apache License 2.0 6 votes vote down vote up
protected void autowireContext(Object instance, ApplicationContext applicationContext) {
    AutowireCapableBeanFactory autowireBeanFactory = applicationContext.getAutowireCapableBeanFactory();
    autowireBeanFactory.autowireBean(instance);

    if (instance instanceof ApplicationContextAware) {
        ((ApplicationContextAware) instance).setApplicationContext(applicationContext);
    }

    if (instance instanceof InitializingBean) {
        try {
            ((InitializingBean) instance).afterPropertiesSet();
        } catch (Exception e) {
            throw new RuntimeException(
                    "Unable to initialize UI Component - calling afterPropertiesSet for " +
                            instance.getClass(), e);
        }
    }
}
 
Example 3
Source Project: cqrs-es-kafka   File: CommandHandlerUtils.java    License: MIT License 6 votes vote down vote up
public static Map<String, CommandHandler> buildCommandHandlersRegistry(final String basePackage,
                                                                       final ApplicationContext context) {

    final Map<String, CommandHandler> registry = new HashMap<>();
    final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
    final AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
    scanner.addIncludeFilter(new AssignableTypeFilter(CommandHandler.class));

    CommandHandler currentHandler = null;

    for (BeanDefinition bean : scanner.findCandidateComponents(basePackage)) {
        currentHandler = (CommandHandler) beanFactory.createBean(ClassUtils.resolveClassName(bean.getBeanClassName(), context.getClassLoader()),
                AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
        registry.put(currentHandler.getInterest(), currentHandler);
    }

    return registry;
}
 
Example 4
Source Project: cloudstack   File: ModuleBasedFilter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void init(FilterConfig filterConfig) throws ServletException {
    String module = filterConfig.getInitParameter("module");
    CloudStackSpringContext context = (CloudStackSpringContext)filterConfig.getServletContext().getAttribute(CloudStackSpringContext.CLOUDSTACK_CONTEXT_SERVLET_KEY);

    if (context == null)
        return;

    ApplicationContext applicationContext = context.getApplicationContextForWeb(module);
    if (applicationContext != null) {
        AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
        if (factory != null) {
            factory.autowireBean(this);
            enabled = true;
        }
    }
}
 
Example 5
Source Project: sdn-rx   File: Neo4jMappingContext.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
	super.setApplicationContext(applicationContext);

	this.beanFactory = applicationContext.getAutowireCapableBeanFactory();
	Driver driver = this.beanFactory.getBean(Driver.class);
	((DefaultNeo4jConverter) this.converter).setTypeSystem(driver.defaultTypeSystem());
}
 
Example 6
private AdminUserDAO getAdminUserDAO( )
{
    AdminUserDAO adminUserDAO = new AdminUserDAO( );
    ApplicationContext context = SpringContextService.getContext( );
    AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory( );
    beanFactory.autowireBean( adminUserDAO );
    return adminUserDAO;
}
 
Example 7
private AdminUserDAO getAdminUserDAO( )
{
    AdminUserDAO adminUserDAO = new AdminUserDAO( );
    ApplicationContext context = SpringContextService.getContext( );
    AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory( );
    beanFactory.autowireBean( adminUserDAO );
    return adminUserDAO;
}
 
Example 8
private BeanDefinitionRegistry getBeanDefinitionRegistry(ApplicationContext applicationContext) {
    AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
    if (beanFactory instanceof BeanDefinitionRegistry) {
        return (BeanDefinitionRegistry) beanFactory;
    }
    throw new IllegalStateException("");
}
 
Example 9
public GroovyObject execPlugIn(String name, GroovyObject groovyObject,
		GroovyObject proxyObject) throws Exception {

	ApplicationContext context=MicroContextHolder.getContext();
	AutowireCapableBeanFactory autowireCapableBeanFactory=context.getAutowireCapableBeanFactory();
	autowireCapableBeanFactory.autowireBeanProperties(groovyObject, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
	return proxyObject;
}
 
Example 10
@Override
public <T> T createDependentInstance(Class<T> cls) {
    Assert.requireNonNull(cls, "cls");
    ApplicationContext context = getContext();
    AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
    return beanFactory.createBean(cls);
}
 
Example 11
Source Project: OpERP   File: AbstractEntityService.java    License: MIT License 5 votes vote down vote up
@Override
public void registerClient(String clientAddress) {
	System.out.println("Client from " + clientAddress);

	ApplicationContext context = ServerApp.getApplicationContext();
	AutowireCapableBeanFactory factory = context
			.getAutowireCapableBeanFactory();
	BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
	GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
	beanDefinition.setBeanClass(RmiProxyFactoryBean.class);
	beanDefinition.setAutowireCandidate(true);

	Class<EM> entityModelInterfaceClass = getEntityModelInterfaceClass();

	MutablePropertyValues propertyValues = new MutablePropertyValues();
	propertyValues.addPropertyValue("serviceInterface",
			entityModelInterfaceClass);
	propertyValues.addPropertyValue("serviceUrl", "rmi://" + clientAddress
			+ ":1099/" + entityModelInterfaceClass.getCanonicalName());
	beanDefinition.setPropertyValues(propertyValues);

	registry.registerBeanDefinition(
			entityModelInterfaceClass.getCanonicalName(), beanDefinition);
	EM entityModel = context.getBean(entityModelInterfaceClass);
	registerEntityModel(entityModel);
	System.out.println(entityModel);

}
 
Example 12
@Override
public <T> T createDependentInstance(Class<T> cls, PostConstructInterceptor<T> interceptor) {
    Assert.requireNonNull(cls, "cls");
    Assert.requireNonNull(interceptor, "interceptor");
    ApplicationContext context = getContext();
    AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
    SpringPreInjector.getInstance().prepare(cls, interceptor);
    return beanFactory.createBean(cls);
}
 
Example 13
static List<Object> getProviders(ApplicationContext context, Collection<Class<?>> providerClasses) {
    List<Object> providers = new LinkedList<>();
    AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
    for (final Class< ? > providerClass: providerClasses) {
        Object bean = null;
        try {
            bean = beanFactory.createBean(providerClass,
                                   AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
        } catch (Exception ex) {
            bean = beanFactory.createBean(providerClass);
        }
        providers.add(bean);
    }
    return providers;
}
 
Example 14
public BasicBeanProcessor(ApplicationContext applicationContext){
    Objects.requireNonNull(applicationContext);
    this.springBeanRegister = new SpringBeanRegister(applicationContext);
    this.helper = new BeanFactoryAdvisorRetrievalHelper(
            (ConfigurableListableBeanFactory)applicationContext.getAutowireCapableBeanFactory());
}
 
Example 15
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
	beanFactory = applicationContext.getAutowireCapableBeanFactory();
}
 
Example 16
@Override
public void setApplicationContext(ApplicationContext context) {
    beanFactory = context.getAutowireCapableBeanFactory();
}
 
Example 17
@Override
public void setApplicationContext(final ApplicationContext context) {
  beanFactory = context.getAutowireCapableBeanFactory();
}
 
Example 18
@Override
public void setApplicationContext(final ApplicationContext context)
{
    beanFactory = context.getAutowireCapableBeanFactory();
}
 
Example 19
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        beanFactory = applicationContext.getAutowireCapableBeanFactory();
    }
 
Example 20
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        beanFactory = applicationContext.getAutowireCapableBeanFactory();
    }