Java Code Examples for org.springframework.beans.factory.config.AutowireCapableBeanFactory#initializeBean()

The following examples show how to use org.springframework.beans.factory.config.AutowireCapableBeanFactory#initializeBean() . 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: MtasUimaParser.java    From inception with Apache License 2.0 6 votes vote down vote up
public MtasUimaParser(MtasConfiguration config)
{
    super(config);
    
    // Perform dependency injection
    AutowireCapableBeanFactory factory = ApplicationContextProvider.getApplicationContext()
            .getAutowireCapableBeanFactory();
    factory.autowireBean(this);
    factory.initializeBean(this, "transientParser");
    
    JSONObject jsonParserConfiguration = new JSONObject(
            config.attributes.get(ARGUMENT_PARSER_ARGS));
    MtasDocumentIndex index = MtasDocumentIndex
            .getIndex(jsonParserConfiguration.getLong(PARAM_PROJECT_ID));

    // Initialize and populate the hash maps for the layers and features
    for (AnnotationFeature feature : index.getFeaturesToIndex()) {
        layers.put(feature.getLayer().getName(), feature.getLayer());
        layerFeatures
                .computeIfAbsent(feature.getLayer().getName(), key -> new ArrayList<>())
                .add(feature);
    }        
}
 
Example 2
Source File: SchedulingService.java    From inception with Apache License 2.0 6 votes vote down vote up
public synchronized void enqueue(Task aTask)
{
    if (getScheduledTasks().contains(aTask)) {
        log.debug("Task already in queue, requeueing at the end: {}", aTask);
        executor.remove(aTask);
    }

    log.debug("Enqueuing task [{}]", aTask);

    // This autowires the task fields manually.
    AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
    factory.autowireBean(aTask);
    factory.initializeBean(aTask, "transientTask");

    executor.execute(aTask);
}
 
Example 3
Source File: ResourceInitializationUtil.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
/**
 * Handle Spring-initialization of resoures produced by the UIMA framework.
 */
public static Resource initResource(Resource aResource, ApplicationContext aApplicationContext) {
  AutowireCapableBeanFactory beanFactory = aApplicationContext.getAutowireCapableBeanFactory();

  if (aResource instanceof PrimitiveAnalysisEngine_impl) {
    PropertyAccessor pa = PropertyAccessorFactory.forDirectFieldAccess(aResource);

    // Access the actual AnalysisComponent and initialize it
    AnalysisComponent analysisComponent = (AnalysisComponent) pa
            .getPropertyValue("mAnalysisComponent");
    initializeBean(beanFactory, analysisComponent, aResource.getMetaData().getName());
    pa.setPropertyValue("mAnalysisComponent", analysisComponent);

    return aResource;
  } else {
    return (Resource) beanFactory.initializeBean(aResource, aResource.getMetaData().getName());
  }
}
 
Example 4
Source File: TaskConsumer.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public void run()
{
    try {
        while (!Thread.interrupted()) {
            log.debug("Waiting for new indexing task...");

            activeTask = queue.take();

            try {
                AutowireCapableBeanFactory factory = applicationContext
                        .getAutowireCapableBeanFactory();
                factory.autowireBean(activeTask);
                factory.initializeBean(activeTask, "transientTask");

                log.debug("Indexing task started: {}", activeTask);
                activeTask.run();
                log.debug("Indexing task completed: {}", activeTask);
            }
            // Catching Throwable is intentional here as we want to continue the execution even
            // if a particular recommender fails.
            catch (Throwable e) {
                log.error("Indexing task failed: {}", activeTask, e);
            }
            finally {
                activeTask = null;
            }
        }
    }
    catch (InterruptedException ie) {
        log.info("Thread interrupted: ", ie);
    }
}
 
Example 5
Source File: ProjectExportServiceImpl.java    From webanno with Apache License 2.0 5 votes vote down vote up
private ProjectExportTaskHandle startTask(ProjectExportTask_ImplBase aTask)
{
    ProjectExportTaskHandle handle = aTask.getHandle();
    
    // This autowires the task fields manually.
    AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
    factory.autowireBean(aTask);
    factory.initializeBean(aTask, "transientTask");

    tasks.put(handle, new TaskInfo(taskExecutorService.submit(aTask), aTask));
    
    return handle;
}
 
Example 6
Source File: ResourceInitializationUtil.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize an existing object as a Spring bean.
 */
public static <T> T initializeBean(AutowireCapableBeanFactory aBeanFactory, T aBean, String aName) {
  @SuppressWarnings("unchecked")
  T wrappedBean = (T) aBeanFactory.initializeBean(aBean, aName);
  aBeanFactory.autowireBean(aBean);
  return wrappedBean;
}
 
Example 7
Source File: WebSocketServer.java    From usergrid with Apache License 2.0 5 votes vote down vote up
public void startSpring() {

        String[] locations = getApplicationContextLocations();
        ApplicationContext ac = new ClassPathXmlApplicationContext( locations );

        AutowireCapableBeanFactory acbf = ac.getAutowireCapableBeanFactory();
        acbf.autowireBeanProperties( this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false );
        acbf.initializeBean( this, "webSocketServer" );

        assertNotNull( emf );
        assertTrue( "EntityManagerFactory is instance of EntityManagerFactoryImpl",
                emf instanceof EntityManagerFactoryImpl );
    }
 
Example 8
Source File: HazelcastTest.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
    // assertNotNull(client);

    String maven_opts = System.getenv( "MAVEN_OPTS" );
    logger.info( "Maven options: " + maven_opts );

    String[] locations = { "usergrid-test-context.xml" };
    ac = new ClassPathXmlApplicationContext( locations );

    AutowireCapableBeanFactory acbf = ac.getAutowireCapableBeanFactory();
    acbf.autowireBeanProperties( this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false );
    acbf.initializeBean( this, "testClient" );
}
 
Example 9
Source File: MongoServer.java    From usergrid with Apache License 2.0 5 votes vote down vote up
public void startSpring() {

        String[] locations = getApplicationContextLocations();
        ApplicationContext ac = new ClassPathXmlApplicationContext( locations );

        AutowireCapableBeanFactory acbf = ac.getAutowireCapableBeanFactory();
        acbf.autowireBeanProperties( this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false );
        acbf.initializeBean( this, "mongoServer" );

        assertNotNull( emf );
        assertTrue( "EntityManagerFactory is instance of EntityManagerFactoryImpl",
                emf instanceof EntityManagerFactoryImpl );
    }
 
Example 10
Source File: SpringUtils.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public static <T> void initializeBean(AutowireCapableBeanFactory acb, T bean) {
	String beanName = StringUtils.uncapitalize(bean.getClass().getSimpleName());
	acb.initializeBean(bean, beanName);
}
 
Example 11
Source File: ToolBase.java    From usergrid with Apache License 2.0 4 votes vote down vote up
public void startSpring() {

        String[] locations = { "toolsApplicationContext.xml" };
        ApplicationContext ac = new ClassPathXmlApplicationContext( locations );

        AutowireCapableBeanFactory acbf = ac.getAutowireCapableBeanFactory();
        acbf.autowireBeanProperties( this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false );
        acbf.initializeBean( this, "testClient" );

        assertNotNull( emf );
        assertTrue( "EntityManagerFactory is instance of EntityManagerFactory",
                emf instanceof EntityManagerFactory );

        injector = ac.getBean( Injector.class );


    }
 
Example 12
Source File: DependencyInjectionTestExecutionListener.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Performs dependency injection and bean initialization for the supplied
 * {@link TestContext} as described in
 * {@link #prepareTestInstance(TestContext) prepareTestInstance()}.
 * <p>The {@link #REINJECT_DEPENDENCIES_ATTRIBUTE} will be subsequently removed
 * from the test context, regardless of its value.
 * @param testContext the test context for which dependency injection should
 * be performed (never {@code null})
 * @throws Exception allows any exception to propagate
 * @see #prepareTestInstance(TestContext)
 * @see #beforeTestMethod(TestContext)
 */
protected void injectDependencies(TestContext testContext) throws Exception {
	Object bean = testContext.getTestInstance();
	Class<?> clazz = testContext.getTestClass();
	AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
	beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
	beanFactory.initializeBean(bean, clazz.getName() + AutowireCapableBeanFactory.ORIGINAL_INSTANCE_SUFFIX);
	testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
}
 
Example 13
Source File: DependencyInjectionTestExecutionListener.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Performs dependency injection and bean initialization for the supplied
 * {@link TestContext} as described in
 * {@link #prepareTestInstance(TestContext) prepareTestInstance()}.
 * <p>The {@link #REINJECT_DEPENDENCIES_ATTRIBUTE} will be subsequently removed
 * from the test context, regardless of its value.
 * @param testContext the test context for which dependency injection should
 * be performed (never {@code null})
 * @throws Exception allows any exception to propagate
 * @see #prepareTestInstance(TestContext)
 * @see #beforeTestMethod(TestContext)
 */
protected void injectDependencies(TestContext testContext) throws Exception {
	Object bean = testContext.getTestInstance();
	Class<?> clazz = testContext.getTestClass();
	AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
	beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
	beanFactory.initializeBean(bean, clazz.getName() + AutowireCapableBeanFactory.ORIGINAL_INSTANCE_SUFFIX);
	testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
}
 
Example 14
Source File: DependencyInjectionTestExecutionListener.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Performs dependency injection and bean initialization for the supplied
 * {@link TestContext} as described in
 * {@link #prepareTestInstance(TestContext) prepareTestInstance()}.
 * <p>The {@link #REINJECT_DEPENDENCIES_ATTRIBUTE} will be subsequently removed
 * from the test context, regardless of its value.
 * @param testContext the test context for which dependency injection should
 * be performed (never {@code null})
 * @throws Exception allows any exception to propagate
 * @see #prepareTestInstance(TestContext)
 * @see #beforeTestMethod(TestContext)
 */
protected void injectDependencies(final TestContext testContext) throws Exception {
	Object bean = testContext.getTestInstance();
	AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
	beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
	beanFactory.initializeBean(bean, testContext.getTestClass().getName());
	testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
}