@Override public void afterPropertiesSet() throws Exception { BeanWrapper bw = new BeanWrapperImpl(ThreadPoolTaskExecutor.class); determinePoolSizeRange(bw); if (this.queueCapacity != null) { bw.setPropertyValue("queueCapacity", this.queueCapacity); } if (this.keepAliveSeconds != null) { bw.setPropertyValue("keepAliveSeconds", this.keepAliveSeconds); } if (this.rejectedExecutionHandler != null) { bw.setPropertyValue("rejectedExecutionHandler", this.rejectedExecutionHandler); } if (this.beanName != null) { bw.setPropertyValue("threadNamePrefix", this.beanName + "-"); } this.target = (TaskExecutor) bw.getWrappedInstance(); if (this.target instanceof InitializingBean) { ((InitializingBean) this.target).afterPropertiesSet(); } }
@Override protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { if (handlerExceptionResolvers == null) { return; } for (HandlerExceptionResolver resolver : handlerExceptionResolvers) { if (resolver instanceof ApplicationContextAware) { ((ApplicationContextAware) resolver).setApplicationContext(getApplicationContext()); } if (resolver instanceof InitializingBean) { try { ((InitializingBean) resolver).afterPropertiesSet(); } catch (Exception ex) { throw new IllegalStateException("Failure from afterPropertiesSet", ex); } } exceptionResolvers.add(resolver); } }
/** * Create and start a new container using the specified factory. */ protected MessageListenerContainer createListenerContainer(JmsListenerEndpoint endpoint, JmsListenerContainerFactory<?> factory) { MessageListenerContainer listenerContainer = factory.createListenerContainer(endpoint); if (listenerContainer instanceof InitializingBean) { try { ((InitializingBean) listenerContainer).afterPropertiesSet(); } catch (Exception ex) { throw new BeanInitializationException("Failed to initialize message listener container", ex); } } int containerPhase = listenerContainer.getPhase(); if (containerPhase < Integer.MAX_VALUE) { // a custom phase value if (this.phase < Integer.MAX_VALUE && this.phase != containerPhase) { throw new IllegalStateException("Encountered phase mismatch between container factory definitions: " + this.phase + " vs " + containerPhase); } this.phase = listenerContainer.getPhase(); } return listenerContainer; }
public static <T> T parseTag(Class<T> clazz, Spec spec) { T object = Reflect.on(clazz).create().get(); if (object instanceof SpecAppliable) ((SpecAppliable) object).applySpec(spec); if (object instanceof InitializingBean) { try { ((InitializingBean) object).afterPropertiesSet(); } catch (Exception e) { throw new RuntimeException(e); } } return object; }
public static <T> T createBean(Class<T> clazz, Settings settings, ApplicationContext applicationContext) throws Exception { if (clazz == null || clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) { return null; } T bean = clazz.newInstance(); if (bean instanceof SettingsAware) { ((SettingsAware) bean).setSettings(settings); } if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(applicationContext); } if (bean instanceof InitializingBean) { ((InitializingBean) bean).afterPropertiesSet(); } return bean; }
@Bean InitializingBean init( final IdentityService identityService) { return new InitializingBean() { @Override public void afterPropertiesSet() throws Exception { // install groups & users Group userGroup = group("user"); Group adminGroup = group("admin"); User joram = user("jbarrez", "Joram", "Barrez"); identityService.createMembership(joram.getId(), userGroup.getId()); identityService.createMembership(joram.getId(), adminGroup.getId()); User josh = user("jlong", "Josh", "Long"); identityService.createMembership(josh.getId(), userGroup.getId()); } }; }
@Override protected FunctionExclusionGroups createFunctionExclusionGroups() { // TODO: The exclusion groups could be exposed over the network (sending the function identifier) and cached final String exclusionGroups = getCommandLine().getOptionValue(EXCLUSION_GROUPS_OPTION); if (exclusionGroups != null) { try { final Class<?> exclusionGroupsClass = ClassUtils.loadClass(exclusionGroups); Object groupsObject = exclusionGroupsClass.newInstance(); if (groupsObject instanceof InitializingBean) { ((InitializingBean) groupsObject).afterPropertiesSet(); } if (groupsObject instanceof FactoryBean) { groupsObject = ((FactoryBean<?>) groupsObject).getObject(); } if (groupsObject instanceof FunctionExclusionGroups) { return (FunctionExclusionGroups) groupsObject; } throw new IllegalArgumentException("Couldn't set exclusion groups to " + exclusionGroups + " (got " + groupsObject + ")"); } catch (Exception e) { throw new OpenGammaRuntimeException("Error loading exclusion groups", e); } } return null; }
protected <T> Map<String, T> getCurrencyInfo(final Function<CurrencyInfo, T> filter) { final Map<String, T> result = new HashMap<String, T>(); for (final Map.Entry<String, CurrencyInfo> e : getPerCurrencyInfo().entrySet()) { final T entry = filter.apply(e.getValue()); if (entry instanceof InitializingBean) { try { ((InitializingBean) entry).afterPropertiesSet(); } catch (final Exception ex) { s_logger.debug("Skipping {}", e.getKey()); s_logger.trace("Caught exception", e); continue; } } if (entry != null) { result.put(e.getKey(), entry); } } return result; }
protected <T> Map<Pair<String, String>, T> getCurrencyPairInfo(final Function<CurrencyPairInfo, T> filter) { final Map<Pair<String, String>, T> result = new HashMap<Pair<String, String>, T>(); for (final Map.Entry<Pair<String, String>, CurrencyPairInfo> e : getPerCurrencyPairInfo().entrySet()) { final T entry = filter.apply(e.getValue()); if (entry instanceof InitializingBean) { try { ((InitializingBean) entry).afterPropertiesSet(); } catch (final Exception ex) { s_logger.debug("Skipping {}", e.getKey()); s_logger.trace("Caught exception", e); continue; } } if (entry != null) { result.put(e.getKey(), entry); } } return result; }
@Bean public InitializingBean clientsCorrelationInitializer(final RequestCorrelationProperties properties) { return new InitializingBean() { @Override public void afterPropertiesSet() throws Exception { if(clients != null) { for(InterceptingHttpAccessor client : clients) { final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(client.getInterceptors()); interceptors.add(new ClientHttpRequestCorrelationInterceptor(properties)); client.setInterceptors(interceptors); } } } }; }
private TimestampPair createAdapter(String ip, int port, String user, String password) { CobarAdapter adapter = null; try { DataSource ds = dsFactory.createDataSource(ip, port, user, password); adapter = new CobarAdapter(); adapter.setDataSource(ds); ((InitializingBean) adapter).afterPropertiesSet(); return new TimestampPair(adapter); } catch (Exception exception) { logger.error("ip=" + ip + ", port=" + port, exception); try { adapter.destroy(); } catch (Exception e) { } throw new RuntimeException(exception); } }
private void autowireAndInitialize(ServletRequest req, Map<String, Object> contexts) { ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(req.getServletContext()); AutowireCapableBeanFactory autowirer = appContext.getAutowireCapableBeanFactory(); for (Map.Entry<String,Object> entry : contexts.entrySet()) { Object context = entry.getValue(); autowirer.autowireBean(context); try { if (context instanceof InitializingBean) { ((InitializingBean) context).afterPropertiesSet(); } } catch (Exception e) { logger.warn("Invocation of init method failed of context: " + entry.getKey(), e); } } }
@Bean InitializingBean populateData(final UserService userService) { return () -> { userService.deleteAll(); userService.persist(User.builder().login("malysz").name("adam").salary(new BigDecimal(306)).build()); log.info(" {}", userService.persist(User.builder().login("borowiec").name("przodownik").salary(new BigDecimal(100)).build())); log.info(" {}", userService.persist(User.builder().login("borowiec").name("aga").salary(new BigDecimal(10)).build())); log.info(" {}", userService.persist(User.builder().login("borowiec").name("kalina").salary(new BigDecimal(30)).build())); log.info(" {}", userService.persist(User.builder().login("tyson").name("iron mike").salary(new BigDecimal(3234)).build())); log.info(" {}", userService.persist(User.builder().login("rossi").name("the doctor").salary(new BigDecimal(2000)).build())); log.info(" {}", userService.persist(new User("kazimierczak", "juz", new BigDecimal(100)))); log.info(" {}", userService.persist(new User("aleksandrowicz", "dawid", new BigDecimal(10)))); log.info(" {}", userService.persist(new User("barszcz", "mariusz", new BigDecimal(30)))); log.info(" {}", userService.persist(new User("bogadanski", "pawel", new BigDecimal(3000)))); log.info(" {}", userService.persist(new User("chudzikowska", "sylwia", new BigDecimal(2000)))); log.info(" {}", userService.persist(new User("swietojanski", "przemyslaw", new BigDecimal(100)))); log.info(" {}", userService.persist(new User("zurek", "marcin", new BigDecimal(10)))); log.info(" {}", userService.persist(new User("grabowski", "michal", new BigDecimal(30)))); log.info(" {}", userService.persist(new User("gilewski", "piotr", new BigDecimal(3000)))); log.info(" {}", userService.persist(new User("ostroski", "krzych", new BigDecimal(2000)))); }; }
public void afterPropertiesSet() throws Exception { Class<?> executorClass = (shouldUseBackport() ? ClassUtils.forName("org.springframework.scheduling.backportconcurrent.ThreadPoolTaskExecutor", getClass().getClassLoader()) : ThreadPoolTaskExecutor.class); BeanWrapper bw = new BeanWrapperImpl(executorClass); determinePoolSizeRange(bw); if (this.queueCapacity != null) { bw.setPropertyValue("queueCapacity", this.queueCapacity); } if (this.keepAliveSeconds != null) { bw.setPropertyValue("keepAliveSeconds", this.keepAliveSeconds); } if (this.rejectedExecutionHandler != null) { bw.setPropertyValue("rejectedExecutionHandler", this.rejectedExecutionHandler); } if (this.beanName != null) { bw.setPropertyValue("threadNamePrefix", this.beanName + "-"); } this.target = (TaskExecutor) bw.getWrappedInstance(); if (this.target instanceof InitializingBean) { ((InitializingBean) this.target).afterPropertiesSet(); } }
protected View createView() { Assert.state(viewClass != null, "View class to instantiate is not set"); Object o = BeanUtils.instantiateClass(viewClass); Assert.isTrue((o instanceof View), "View class '" + viewClass + "' was instantiated, but instance is not a View!"); View view = (View) o; view.setDescriptor(this); if (viewProperties != null) { BeanWrapper wrapper = new BeanWrapperImpl(view); wrapper.setPropertyValues(viewProperties); } if (view instanceof InitializingBean) { try { ((InitializingBean) view).afterPropertiesSet(); } catch (Exception e) { throw new BeanInitializationException("Problem running on " + view, e); } } return view; }
private Editor createEditor(){ Object o = BeanUtils.instantiateClass(editorClass); Assert.isTrue((o instanceof Editor), "Editor class '"+ editorClass + "' was instantiated but is not an Editor"); Editor editor = (Editor)o; editor.setDescriptor(this); ApplicationEventMulticaster multicaster = getApplicationEventMulticaster(); if(editor instanceof ApplicationListener && multicaster != null){ multicaster.addApplicationListener((ApplicationListener)editor); } if(editorProperties != null){ BeanWrapper wrapper = new BeanWrapperImpl(editor); wrapper.setPropertyValues(editorProperties); } if(editor instanceof InitializingBean){ try { ((InitializingBean)editor).afterPropertiesSet(); } catch (Exception e) { throw new BeanInitializationException("Problem running on " + editor, e); } } return editor; }
@Bean public InitializingBean demo(DemoService demoService) { return new InitializingBean() { @Override @Transactional public void afterPropertiesSet() throws Exception { if (demoProperties.isEnabled()) { demoService.createDemoDataFromProperties(demoProperties); } } }; }
@Bean public InitializingBean issuerInitializer(IssuerRepository issuerRepository) { return new InitializingBean() { @Override @Transactional public void afterPropertiesSet() throws Exception { initializeIssuerFromPropertiesIfNecessary(issuerRepository, issuer()); } }; }
@Override public void afterPropertiesSet() throws Exception { if (executor instanceof InitializingBean) { InitializingBean bean = (InitializingBean) executor; bean.afterPropertiesSet(); } }
public void testProxy() throws Exception { // publish service bundleContext.registerService(new String[] { DataSource.class.getName(), Comparator.class.getName(), InitializingBean.class.getName(), Constants.class.getName() }, new Service(), new Hashtable()); ConfigurableApplicationContext ctx = getNestedContext(); assertNotNull(ctx); Object proxy = ctx.getBean("service"); assertNotNull(proxy); assertTrue(proxy instanceof DataSource); assertTrue(proxy instanceof Comparator); assertTrue(proxy instanceof Constants); assertTrue(proxy instanceof InitializingBean); ctx.close(); }
/** * Give a bean a chance to react now all its properties are set, * and a chance to know about its owning bean factory (this object). * This means checking whether the bean implements InitializingBean or defines * a custom init method, and invoking the necessary callback(s) if it does. * @param beanName the bean name in the factory (for debugging purposes) * @param bean the new bean instance we may need to initialize * @param mbd the merged bean definition that the bean was created with * (can also be {@code null}, if given an existing bean instance) * @throws Throwable if thrown by init methods or by the invocation process * @see #invokeCustomInitMethod */ protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable { boolean isInitializingBean = (bean instanceof InitializingBean); if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) { if (logger.isDebugEnabled()) { logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'"); } if (System.getSecurityManager() != null) { try { AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { @Override public Object run() throws Exception { ((InitializingBean) bean).afterPropertiesSet(); return null; } }, getAccessControlContext()); } catch (PrivilegedActionException pae) { throw pae.getException(); } } else { ((InitializingBean) bean).afterPropertiesSet(); } } if (mbd != null) { String initMethodName = mbd.getInitMethodName(); if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) { invokeCustomInitMethod(beanName, bean, mbd); } } }
@Bean public InitializingBean seedDatabase(TrackingRepository repository) { System.out.println("initial DB"); return () -> { repository.save(new Tracking("EMS123456777", "Packing", 1L)); repository.save(new Tracking("KEERY9999", "Packing", 2L)); }; }
@Bean public InitializingBean seedDatabase(ProductRepository repository) { return () -> { repository.save(new Product("Intel i7", "CPU", "https://i.ytimg.com/vi/mmHY8Kj4vCk/maxresdefault.jpg", "i7 7700k", 7000)); repository.save(new Product("AMD Raizen 1700X", "CPU", "http://cdn.wccftech.com/wp-content/uploads/2017/02/AMD-Ryzen-CPU.png", "AMD 1700X", 3500)); repository.save(new Product("Galax HOF GTX1080", "GPU", "https://www.custompcreview.com/wp-content/uploads/2016/05/29140448720l.jpg", "GTX 1080", 2350)); repository.save(new Product("Raiden Plasma", "RAM", "https://i.ytimg.com/vi/SApyTXm_jQE/maxresdefault.jpg", "DDR4 32GB", 8872)); }; }
@Override public void afterPropertiesSet() throws Exception { for (ViewResolver viewResolver : this.viewResolvers) { if (viewResolver instanceof InitializingBean) { ((InitializingBean) viewResolver).afterPropertiesSet(); } } }
@Before public final void createFactory() throws Exception { this.factory = createRequestFactory(); if (this.factory instanceof InitializingBean) { ((InitializingBean) this.factory).afterPropertiesSet(); } }
@Before public final void createFactory() throws Exception { factory = createRequestFactory(); if (factory instanceof InitializingBean) { ((InitializingBean) factory).afterPropertiesSet(); } }