Example usage for org.springframework.beans.factory ObjectFactory ObjectFactory

List of usage examples for org.springframework.beans.factory ObjectFactory ObjectFactory

Introduction

In this page you can find the example usage for org.springframework.beans.factory ObjectFactory ObjectFactory.

Prototype

ObjectFactory

Source Link

Usage

From source file:org.spring.guice.annotation.ModuleRegistryConfiguration.java

@PostConstruct
public void init() {
    List<Module> modules = new ArrayList<Module>(this.modules);
    modules.add(new SpringModule(beanFactory));
    injector = Guice.createInjector(modules);
    for (Entry<Key<?>, Binding<?>> entry : injector.getBindings().entrySet()) {
        if (entry.getKey().getTypeLiteral().getRawType().equals(Injector.class)) {
            continue;
        }/*from w ww .j av  a2 s. c  o m*/
        final Provider<?> provider = entry.getValue().getProvider();
        beanFactory.registerResolvableDependency(entry.getKey().getTypeLiteral().getRawType(),
                new ObjectFactory<Object>() {
                    @Override
                    public Object getObject() throws BeansException {
                        return provider.get();
                    }
                });
    }
}

From source file:org.openregistry.core.service.DefaultPersonServiceTests.java

@Before
public void setUp() throws Exception {
    this.personRepository = new MockPersonRepository(new MockPerson());
    this.objectFactory = new ObjectFactory<Person>() {
        public Person getObject() {
            return new MockPerson();
        }/* w  ww .  j  a va 2 s.c  o m*/
    };

    this.personService = new DefaultPersonService(personRepository, new MockReferenceRepository(),
            new MockDisclosureRecalculationStrategyRepository(), new NoOpIdentifierGenerator(),
            new MockReconciler(ReconciliationType.NONE));
    this.personService.setIdCardGenerator(new MockIdCardGenerator());
    this.personService.setPersonObjectFactory(this.objectFactory);
    reconciliationCriteria = new MockReconciliationCriteria();
    setReconciliationCriteria(reconciliationCriteria);
    final SoRSpecificationThreadLocalAspect aspect = Aspects.aspectOf(SoRSpecificationThreadLocalAspect.class);
    aspect.setSystemOfRecordRepository(new MockSystemOfRecordRepository());
}

From source file:org.everrest.spring.SpringComponentsLoader.java

/**
 * Add binding for HttpHeaders, InitialProperties, Request, SecurityContext, UriInfo. All this types will be
 * supported for injection in constructor or fields of component of Spring IoC container.
 *
 * @param beanFactory/*from  w w  w  . j a v  a  2  s . c  om*/
 *         bean factory
 * @see org.springframework.beans.factory.annotation.Autowired
 */
protected void addAutowiredDependencies(ConfigurableListableBeanFactory beanFactory) {
    beanFactory.registerResolvableDependency(HttpHeaders.class, new ObjectFactory<HttpHeaders>() {
        @Override
        public HttpHeaders getObject() {
            ApplicationContext context = ApplicationContextImpl.getCurrent();
            if (context == null) {
                throw new IllegalStateException("EverRest ApplicationContext is not initialized.");
            }
            return context.getHttpHeaders();
        }
    });
    beanFactory.registerResolvableDependency(InitialProperties.class, new ObjectFactory<InitialProperties>() {
        @Override
        public InitialProperties getObject() {
            ApplicationContext context = ApplicationContextImpl.getCurrent();
            if (context == null) {
                throw new IllegalStateException("EverRest ApplicationContext is not initialized.");
            }
            return context.getInitialProperties();
        }
    });
    beanFactory.registerResolvableDependency(Request.class, new ObjectFactory<Request>() {
        @Override
        public Request getObject() {
            ApplicationContext context = ApplicationContextImpl.getCurrent();
            if (context == null) {
                throw new IllegalStateException("EverRest ApplicationContext is not initialized.");
            }
            return context.getRequest();
        }
    });
    beanFactory.registerResolvableDependency(SecurityContext.class, new ObjectFactory<SecurityContext>() {
        @Override
        public SecurityContext getObject() {
            ApplicationContext context = ApplicationContextImpl.getCurrent();
            if (context == null) {
                throw new IllegalStateException("EverRest ApplicationContext is not initialized.");
            }
            return context.getSecurityContext();
        }
    });
    beanFactory.registerResolvableDependency(UriInfo.class, new ObjectFactory<UriInfo>() {
        @Override
        public UriInfo getObject() {
            ApplicationContext context = ApplicationContextImpl.getCurrent();
            if (context == null) {
                throw new IllegalStateException("EverRest ApplicationContext is not initialized.");
            }
            return context.getUriInfo();
        }
    });
    beanFactory.registerResolvableDependency(javax.ws.rs.core.Application.class,
            new ObjectFactory<javax.ws.rs.core.Application>() {
                @Override
                public javax.ws.rs.core.Application getObject() {
                    ApplicationContext context = ApplicationContextImpl.getCurrent();
                    if (context == null) {
                        throw new IllegalStateException("EverRest ApplicationContext is not initialized.");
                    }
                    return context.getApplication();
                }
            });
}

From source file:org.craftercms.engine.view.freemarker.CrafterFreeMarkerView.java

@Override
@SuppressWarnings("deprecation")
protected SimpleHash buildTemplateModel(final Map<String, Object> model, final HttpServletRequest request,
        final HttpServletResponse response) {
    AllHttpScopesAndAppContextHashModel templateModel = new AllHttpScopesAndAppContextHashModel(
            getObjectWrapper(), applicationContextAccessor, getServletContext(), request);
    HttpSessionHashModel sessionModel = createSessionModel(request, response);
    HttpRequestHashModel requestModel = new HttpRequestHashModel(request, response, getObjectWrapper());
    HttpRequestParametersHashModel requestParamsModel = new HttpRequestParametersHashModel(request);
    Map<String, String> cookies = createCookieMap(request);

    templateModel.put(KEY_APPLICATION_CAP, servletContextHashModel);
    templateModel.put(KEY_APPLICATION, servletContextHashModel);
    templateModel.put(KEY_SESSION_CAP, sessionModel);
    templateModel.put(KEY_SESSION, sessionModel);
    templateModel.put(KEY_REQUEST_CAP, requestModel);
    templateModel.put(KEY_REQUEST, requestModel);
    templateModel.put(KEY_REQUEST_PARAMS_CAP, requestParamsModel);
    templateModel.put(KEY_REQUEST_PARAMS, requestParamsModel);
    templateModel.put(KEY_APP_CONTEXT_CAP, applicationContextAccessor);
    templateModel.put(KEY_APP_CONTEXT, applicationContextAccessor);
    templateModel.put(KEY_COOKIES_CAP, cookies);
    templateModel.put(KEY_COOKIES, cookies);

    Authentication auth = SecurityUtils.getAuthentication(request);
    if (auth != null) {
        templateModel.put(KEY_AUTH_CAP, auth);
        templateModel.put(KEY_AUTH, auth);
        templateModel.put(KEY_PROFILE_CAP, auth.getProfile());
        templateModel.put(KEY_PROFILE, auth.getProfile());
    }//  w w w  .j a  v  a2  s  . com

    SiteContext siteContext = SiteContext.getCurrent();
    Configuration siteConfig = siteContext.getConfig();
    Locale locale = LocaleContextHolder.getLocale();
    TemplateHashModel staticModels = BeansWrapper.getDefaultInstance().getStaticModels();
    TemplateHashModel enumModels = BeansWrapper.getDefaultInstance().getEnumModels();

    templateModel.put(KEY_STATICS_CAP, staticModels);
    templateModel.put(KEY_STATICS, staticModels);
    templateModel.put(KEY_ENUMS_CAP, enumModels);
    templateModel.put(KEY_ENUMS, enumModels);
    templateModel.put(KEY_SITE_CONTEXT_CAP, siteContext);
    templateModel.put(KEY_SITE_CONTEXT, siteContext);
    templateModel.put(KEY_LOCALE_CAP, locale);
    templateModel.put(KEY_LOCALE, locale);

    if (siteConfig != null) {
        templateModel.put(KEY_SITE_CONFIG, siteConfig);
        templateModel.put(KEY_SITE_CONFIG_CAP, siteConfig);
    }

    templateModel.putAll(model);

    ObjectFactory<SimpleHash> componentModelFactory = new ObjectFactory<SimpleHash>() {
        public SimpleHash getObject() {
            return buildTemplateModel(model, request, response);
        }
    };

    RenderComponentDirective renderComponentDirective = new RenderComponentDirective();
    renderComponentDirective.setSiteItemService(siteItemService);
    renderComponentDirective.setModelFactory(componentModelFactory);
    renderComponentDirective.setTemplateXPathQuery(componentTemplateXPathQuery);
    renderComponentDirective.setTemplateNamePrefix(componentTemplateNamePrefix);
    renderComponentDirective.setTemplateNameSuffix(componentTemplateNameSuffix);
    renderComponentDirective.setIncludeElementName(componentIncludeElementName);
    renderComponentDirective.setScriptResolver(componentScriptResolver);
    renderComponentDirective.setServletContext(getServletContext());

    ExecuteControllerDirective executeControllerDirective = new ExecuteControllerDirective();
    executeControllerDirective.setServletContext(getServletContext());

    templateModel.put(RENDER_COMPONENT_DIRECTIVE_NAME, renderComponentDirective);
    templateModel.put(EXECUTE_CONTROLLER_DIRECTIVE_NAME, executeControllerDirective);

    return templateModel;
}