Refine search
public abstract class AbstractServlet extends HttpServlet { protected AutowireCapableBeanFactory ctx; @Override public void init() throws ServletException { super.init(); ctx = ((ApplicationContext) getServletContext().getAttribute( "applicationContext")).getAutowireCapableBeanFactory(); //The following line does the magic ctx.autowireBean(this); }
private void run() { final ApplicationContext parent = new ClassPathXmlApplicationContext(parentContextPath); parent.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); parent.getAutowireCapableBeanFactory().initializeBean(this, getClass().getSimpleName()); this.parentContext = parent; }
/** * Create a default strategy. * <p>The default implementation uses * {@link org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean}. * @param context the current WebApplicationContext * @param clazz the strategy implementation class to instantiate * @return the fully configured strategy instance * @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory() * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean */ protected Object createDefaultStrategy(ApplicationContext context, Class<?> clazz) { return context.getAutowireCapableBeanFactory().createBean(clazz); }
B bean = new B(); AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory(); factory.autowireBean( bean ); factory.initializeBean( bean, "bean" );
@Test public void testAspectAppliedForInitializeBeanWithEmptyName() { ITestBean bean = (ITestBean) this.context.getAutowireCapableBeanFactory().initializeBean(new TestBean(), ""); CountingAspectJAdvice advice = (CountingAspectJAdvice) this.context.getBean("countingAdvice"); assertEquals("Incorrect before count", 0, advice.getBeforeCount()); assertEquals("Incorrect after count", 0, advice.getAfterCount()); bean.setName("Sally"); assertEquals("Incorrect before count", 1, advice.getBeforeCount()); assertEquals("Incorrect after count", 1, advice.getAfterCount()); bean.getName(); assertEquals("Incorrect before count", 1, advice.getBeforeCount()); assertEquals("Incorrect after count", 1, advice.getAfterCount()); }
AgentCommand get(final String requestedCommandName) { final Class<? extends AgentCommand> commandClass = commandMap.get(requestedCommandName); final AgentCommand commandInstance; if (commandClass == null) { log.error("Command not found: {}", requestedCommandName); commandInstance = null; } else { commandInstance = applicationContext.getAutowireCapableBeanFactory().getBean(commandClass); } return commandInstance; }
/** * Apply the containing {@link ApplicationContext}'s lifecycle methods * to the given {@link View} instance, if such a context is available. * @param viewName the name of the view * @param view the freshly created View instance, pre-configured with * {@link AbstractUrlBasedView}'s properties * @return the {@link View} instance to use (either the original one * or a decorated variant) * @see #getApplicationContext() * @see ApplicationContext#getAutowireCapableBeanFactory() * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#initializeBean */ protected View applyLifecycleMethods(String viewName, AbstractUrlBasedView view) { ApplicationContext context = getApplicationContext(); if (context != null) { Object initialized = context.getAutowireCapableBeanFactory().initializeBean(view, viewName); if (initialized instanceof View) { return (View) initialized; } } return view; }
@SuppressWarnings("unchecked") @Override public void init(HttpSecurity http) throws Exception { // autowire this bean ApplicationContext context = http.getSharedObject(ApplicationContext.class); context.getAutowireCapableBeanFactory().autowireBean(this); http .authorizeRequests() .antMatchers(permitAllPattern).permitAll() .anyRequest().authenticated(); if (http.getConfigurer(FormLoginConfigurer.class) == null) { // only apply if formLogin() was not invoked by the user http .formLogin() .loginPage(loginPage); } }
@Override public void contextInitialized(ServletContextEvent event) { WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext()).getAutowireCapableBeanFactory().autowireBean(this); metricsServletContextListener.contextInitialized(event); healthCheckServletContextListener.contextInitialized(event); }
if (wac.containsBean(beanName)) { T endpoint = wac.getBean(beanName, endpointClass); if (logger.isTraceEnabled()) { logger.trace("Using @ServerEndpoint singleton " + endpoint); if (ann != null && wac.containsBean(ann.value())) { T endpoint = wac.getBean(ann.value(), endpointClass); if (logger.isTraceEnabled()) { logger.trace("Creating new @ServerEndpoint instance of type " + endpointClass); return wac.getAutowireCapableBeanFactory().createBean(endpointClass);
/** * Public constructor used by {@link BatchRuntime#getJobOperator()}. This will bootstrap a * singleton ApplicationContext if one has not already been created (and will utilize the existing * one if it has) to populate itself. */ public JsrJobOperator() { this.baseContext = BaseContextHolder.getInstance().getContext(); baseContext.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); if(taskExecutor == null) { taskExecutor = new SimpleAsyncTaskExecutor(); } }
@Override protected void initFrameworkServlet() throws ServletException, BeansException { context = getServletContext(); context.log("GSP servlet initialized"); context.setAttribute(SERVLET_INSTANCE, this); final WebApplicationContext webApplicationContext = getWebApplicationContext(); grailsAttributes = GrailsFactoriesLoader.loadFactoriesWithArguments(GrailsApplicationAttributes.class, getClass().getClassLoader(), new Object[]{context}).get(0); webApplicationContext.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); groovyPagesTemplateEngine = webApplicationContext.getBean(GroovyPagesTemplateEngine.BEAN_ID, GroovyPagesTemplateEngine.class); }
@Override public JsonSerializer<?> serializerInstance(SerializationConfig config, Annotated annotated, Class<?> implClass) { return (JsonSerializer<?>) this.beanFactory.createBean(implClass); }
final WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext(); // create the object yourself // and inject the dependenices you want manually final Object existingBean = initializeYourObjectHere(); AutowireCapableBeanFactory beanFactory = ctx.getAutowireCapableBeanFactory(); // now autowire it, injecting the remaining dependencies beanFactory.autowireBeanProperties( existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); // run post processors and register bean under this name beanFactory.initializeBean(existingBean, "newBeanName");
@Override protected ViewPreparer getPreparer(String name, WebApplicationContext context) throws TilesException { // Quick check on the concurrent map first, with minimal locking. ViewPreparer preparer = this.sharedPreparers.get(name); if (preparer == null) { synchronized (this.sharedPreparers) { preparer = this.sharedPreparers.get(name); if (preparer == null) { try { Class<?> beanClass = ClassUtils.forName(name, context.getClassLoader()); if (!ViewPreparer.class.isAssignableFrom(beanClass)) { throw new PreparerException( "Invalid preparer class [" + name + "]: does not implement ViewPreparer interface"); } preparer = (ViewPreparer) context.getAutowireCapableBeanFactory().createBean(beanClass); this.sharedPreparers.put(name, preparer); } catch (ClassNotFoundException ex) { throw new NoSuchPreparerException("Preparer class [" + name + "] not found", ex); } } } } return preparer; }
@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" ); }
@Override public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) { return getWebApplicationContext().getAutowireCapableBeanFactory().createBean(key); }
@Override public void injectResources(Object saga) { autowireCapableBeanFactory.autowireBeanProperties(saga, AutowireCapableBeanFactory.AUTOWIRE_NO, false); }
@Before @SuppressWarnings("resource") public void setup() { StaticWebApplicationContext wac = new StaticWebApplicationContext(); Properties props = new Properties(); props.setProperty("myOrigin", "http://example.com"); wac.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("ps", props)); wac.registerSingleton("ppc", PropertySourcesPlaceholderConfigurer.class); wac.refresh(); this.handlerMapping.setRemoveSemicolonContent(false); wac.getAutowireCapableBeanFactory().initializeBean(this.handlerMapping, "hm"); this.request.setMethod("GET"); this.request.addHeader(HttpHeaders.ORIGIN, "http://domain.com/"); }
void pollDirectories(File... directories) { for (File directory : directories) { StandardIntegrationFlow integrationFlow = IntegrationFlows .from(Files.inboundAdapter(directory) .scanner(new DefaultDirectoryScanner()), e -> e.poller(p -> p.fixedDelay(1000)) .id(directory.getName() + ".adapter")) .transform(Files.toStringTransformer(), e -> e.id(directory.getName() + ".transformer")) .channel(this.dynamicAdaptersResult) .get(); this.beanFactory.initializeBean(integrationFlow, directory.getName()); this.beanFactory.getBean(directory.getName() + ".transformer", Lifecycle.class).start(); this.beanFactory.getBean(directory.getName() + ".adapter", Lifecycle.class).start(); } }