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

The following examples show how to use org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBean() . 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: AppUI.java    From cuba with 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 2
Source File: ActionsImpl.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected void autowireContext(Action instance) {
    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 File: ModuleBasedFilter.java    From cloudstack with 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 4
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 5
Source File: DesktopComponentsFactory.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected void autowireContext(Component instance) {
    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 Component - calling afterPropertiesSet for " +
                            instance.getClass());
        }
    }
}
 
Example 6
Source File: AdminUserDAOTest.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private AdminUserDAO getAdminUserDAO( )
{
    AdminUserDAO adminUserDAO = new AdminUserDAO( );
    ApplicationContext context = SpringContextService.getContext( );
    AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory( );
    beanFactory.autowireBean( adminUserDAO );
    return adminUserDAO;
}
 
Example 7
Source File: LuteceDefaultAdminAuthenticationTest.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private LuteceDefaultAdminAuthentication getLuteceDefaultAdminAuthentication( )
{
    LuteceDefaultAdminAuthentication adminAuth = new LuteceDefaultAdminAuthentication( );
    LuteceDefaultAdminUserDAO dao = new LuteceDefaultAdminUserDAO( );
    AutowireCapableBeanFactory beanFactory = SpringContextService.getContext( ).getAutowireCapableBeanFactory( );
    beanFactory.autowireBean( dao );
    adminAuth.setDao( dao );
    beanFactory.autowireBean( adminAuth );
    return adminAuth;
}
 
Example 8
Source File: AdminUserHomeTest.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private AdminUserDAO getAdminUserDAO( )
{
    AdminUserDAO adminUserDAO = new AdminUserDAO( );
    ApplicationContext context = SpringContextService.getContext( );
    AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory( );
    beanFactory.autowireBean( adminUserDAO );
    return adminUserDAO;
}
 
Example 9
Source File: AdminLoginJspBeanTest.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private AdminUserDAO getAdminUserDAO( )
{
    AdminUserDAO adminUserDAO = new AdminUserDAO( );
    ApplicationContext context = SpringContextService.getContext( );
    AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory( );
    beanFactory.autowireBean( adminUserDAO );
    return adminUserDAO;
}
 
Example 10
Source File: AdminMenuJspBeanTest.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private AdminUserDAO getAdminUserDAO( )
{
    AdminUserDAO adminUserDAO = new AdminUserDAO( );
    ApplicationContext context = SpringContextService.getContext( );
    AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory( );
    beanFactory.autowireBean( adminUserDAO );
    return adminUserDAO;
}
 
Example 11
Source File: ApplicationContextHolder.java    From app-engine with Apache License 2.0 5 votes vote down vote up
/**
 * 将该对象中的带有Autowired annotation的属性自动注入
 */
public static void autowireBean(Object obj) {
    if (_context != null) {
        AutowireCapableBeanFactory factory = _context.getAutowireCapableBeanFactory();
        factory.autowireBean(obj);
    }
}
 
Example 12
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 13
Source File: LuteceDefaultAdminAuthenticationTest.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private AdminUserDAO getAdminUserDAO( )
{
    AdminUserDAO adminUserDAO = new AdminUserDAO( );
    ApplicationContext context = SpringContextService.getContext( );
    AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory( );
    beanFactory.autowireBean( adminUserDAO );
    return adminUserDAO;
}
 
Example 14
Source File: SpringEntityListener.java    From example-ddd-with-spring-data-jpa with Apache License 2.0 5 votes vote down vote up
@PostLoad
@PostPersist
public void inject(Object object) {
    AutowireCapableBeanFactory beanFactory = get().getBeanFactory();
    if(beanFactory == null) {
        LOG.warn("Bean Factory not set! Depdendencies will not be injected into: '{}'", object);
        return;
    }
    LOG.debug("Injecting dependencies into entity: '{}'.", object);
    beanFactory.autowireBean(object);
}
 
Example 15
Source File: SyndesisCommand.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Override
public final ExitStatus call() {
    prepare();

    try (AbstractApplicationContext context = createContext()) {
        final AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();

        beanFactory.autowireBean(this);

        perform();
    }

    return ExitStatus.OK;
}
 
Example 16
Source File: AbstractCrudTestCase.java    From cerberus-source with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void init(final ServletConfig config) throws ServletException {
    super.init(config);
    springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
    final AutowireCapableBeanFactory beanFactory = springContext.getAutowireCapableBeanFactory();
    beanFactory.autowireBean(this);
}
 
Example 17
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 18
Source File: TestUiEnvironment.java    From cuba with Apache License 2.0 4 votes vote down vote up
protected void setupVaadinUi() {
    AutowireCapableBeanFactory injector = getInjector();

    app = new DefaultApp();
    setThemeConstants(app, new ThemeConstants(new HashMap<>()));
    setCookies(app, new AppCookies());

    Connection connection = new ConnectionImpl();
    injector.autowireBean(connection);

    setConnection(app, connection);

    VaadinSession vaadinSession = new TestVaadinSession(new WebBrowser(), getLocale());

    vaadinSession.setAttribute(App.class, app);
    vaadinSession.setAttribute(App.NAME, app);
    vaadinSession.setAttribute(Connection.class, connection);
    vaadinSession.setAttribute(Connection.NAME, connection);
    vaadinSession.setAttribute(UserSession.class, sessionSource.getSession());

    VaadinSession.setCurrent(vaadinSession);

    injector.autowireBean(app);

    ui = new AppUI();
    injector.autowireBean(ui);

    // setup UI

    ConnectorTracker connectorTracker = new TestConnectorTracker(ui);

    try {
        getDeclaredField(UI.class, "connectorTracker", true)
                .set(ui, connectorTracker);
        getDeclaredField(UI.class, "session", true)
                .set(ui, vaadinSession);
    } catch (Exception e) {
        throw new RuntimeException("Unable to init Vaadin UI state", e);
    }

    UI.setCurrent(ui);

    VaadinRequest vaadinRequest = new TestVaadinRequest();
    ui.getPage().init(vaadinRequest);

    initUi(ui, vaadinRequest);
}
 
Example 19
Source File: ComponentContext.java    From cosmic with Apache License 2.0 4 votes vote down vote up
public static <T> T inject(final Object instance) {
    // autowire dynamically loaded object
    final AutowireCapableBeanFactory beanFactory = getApplicationContext(instance).getAutowireCapableBeanFactory();
    beanFactory.autowireBean(instance);
    return (T) instance;
}
 
Example 20
Source File: JpaRestfulServerR4.java    From careconnect-reference-implementation with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected void initialize() throws ServletException {
    super.initialize();
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

    // Get the spring context from the web container (it's declared in web.xml)
    FhirVersionEnum fhirVersion = FhirVersionEnum.R4;
    setFhirContext(new FhirContext(fhirVersion));
    String serverBase = HapiProperties.getServerAddress();
    if (serverBase != null && !serverBase.isEmpty()) {
        setServerAddressStrategy(new HardcodedServerAddressStrategy(serverBase));
    }

    if (applicationContext == null ) log.info("Context is null");

    AutowireCapableBeanFactory autowireCapableBeanFactory = applicationContext.getAutowireCapableBeanFactory();
    autowireCapableBeanFactory.autowireBean(this);

    List<IResourceProvider> permissionlist = new ArrayList<>();
    Class<?> classType = null;
    try {
        classType = Class.forName("uk.nhs.careconnect.ccri.fhirserver.r4.provider.ObservationDefinitionProvider");
        log.info("class methods " + classType.getMethods()[4].getName() );
    } catch (ClassNotFoundException  e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception ex) {
        log.error(ex.getMessage());
    }

    permissionlist.add((IResourceProvider) applicationContext.getBean(classType));
    setResourceProviders(permissionlist);

    // Replace built in conformance provider (CapabilityStatement)
    setServerConformanceProvider(new CareConnectServerConformanceR4Provider());

    setServerName(HapiProperties.getServerName());
    setServerVersion(HapiProperties.getSoftwareVersion());
    setImplementationDescription(HapiProperties.getSoftwareImplementationDesc());


    ServerInterceptor loggingInterceptor = new ServerInterceptor(log);
    registerInterceptor(loggingInterceptor);




    CorsConfiguration config = new CorsConfiguration();
    config.addAllowedHeader("x-fhir-starter");
    config.addAllowedHeader("Origin");
    config.addAllowedHeader("Accept");
    config.addAllowedHeader("X-Requested-With");
    config.addAllowedHeader("Content-Type");

    config.addAllowedOrigin("*");

    config.addExposedHeader("Location");
    config.addExposedHeader("Content-Location");
    config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"));

    // Create the interceptor and register it
    CorsInterceptor interceptor = new CorsInterceptor(config);
    registerInterceptor(interceptor);

    ServerInterceptor gatewayInterceptor = new ServerInterceptor(log);
    if (HapiProperties.getSecurityOauth()) {
        registerInterceptor(new OAuth2Interceptor());  // Add OAuth2 Security Filter
    }
    registerInterceptor(gatewayInterceptor);

    FifoMemoryPagingProvider pp = new FifoMemoryPagingProvider(10);
    pp.setDefaultPageSize(10);
    pp.setMaximumPageSize(100);
    setPagingProvider(pp);

    setDefaultPrettyPrint(true);
    setDefaultResponseEncoding(EncodingEnum.JSON);

    ctx = getFhirContext();


    registerInterceptor( new ResponseHighlighterInterceptor());

    // Remove as believe due to issues on docker ctx.setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator());
}