Java Code Examples for org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean()
The following examples show how to use
org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean() .
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
Source File: SpecialBeanInstantiationDemo.java From geekbang-lessons with Apache License 2.0 | 6 votes |
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 File: CommandHandlerUtils.java From cqrs-es-kafka with MIT License | 6 votes |
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 3
Source File: EventHandlerUtils.java From cqrs-es-kafka with MIT License | 6 votes |
public static Map<String, EventHandler> buildEventHandlersRegistry(final String basePackage, final ApplicationContext context) { final Map<String, EventHandler> registry = new HashMap<>(); final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false); final AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory(); scanner.addIncludeFilter(new AssignableTypeFilter(EventHandler.class)); EventHandler currentHandler = null; for (BeanDefinition bean : scanner.findCandidateComponents(basePackage)) { currentHandler = (EventHandler) beanFactory.createBean(ClassUtils.resolveClassName(bean.getBeanClassName(), context.getClassLoader()), AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); registry.put(currentHandler.getInterest(), currentHandler); } return registry; }
Example 4
Source File: JAXRSServerFactoryBeanDefinitionParser.java From cxf with Apache License 2.0 | 6 votes |
static List<Object> createBeansFromDiscoveredClasses(ApplicationContext context, Collection<Class<?>> classes, Class<? extends Annotation> serviceClassAnnotation) { AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory(); final List< Object > providers = new ArrayList<>(); for (final Class< ? > clazz: classes) { if (serviceClassAnnotation != null && clazz.getAnnotation(serviceClassAnnotation) == null) { continue; } Object bean = null; try { bean = beanFactory.createBean(clazz, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); } catch (Exception ex) { String stackTrace = ExceptionUtils.getStackTrace(ex); LOG.fine("Autowire failure for a " + clazz.getName() + " bean: " + stackTrace); bean = beanFactory.createBean(clazz); } providers.add(bean); } return providers; }
Example 5
Source File: AbstractSpringManagedBeanFactory.java From dolphin-platform with Apache License 2.0 | 5 votes |
@Override public <T> T createDependentInstance(Class<T> cls) { Assert.requireNonNull(cls, "cls"); ApplicationContext context = getContext(); AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory(); return beanFactory.createBean(cls); }
Example 6
Source File: AbstractSpringManagedBeanFactory.java From dolphin-platform with Apache License 2.0 | 5 votes |
@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 7
Source File: JAXRSClientFactoryBeanDefinitionParser.java From cxf with Apache License 2.0 | 5 votes |
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; }