List of usage examples for org.springframework.beans BeanUtils instantiateClass
public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException
From source file:org.springframework.web.method.annotation.support.ModelAttributeMethodProcessor.java
/** * Extension point to create the model attribute if not found in the model. * The default implementation uses the default constructor. * @param attributeName the name of the attribute, never {@code null} * @param parameter the method parameter * @param binderFactory for creating WebDataBinder instance * @param request the current request//from www . j a v a2 s . c om * @return the created model attribute, never {@code null} */ protected Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { return BeanUtils.instantiateClass(parameter.getParameterType()); }
From source file:org.springframework.web.servlet.mvc.multiaction.MultiActionController.java
/** * Create a new command object of the given class. * <p>This implementation uses {@code BeanUtils.instantiateClass}, * so commands need to have public no-arg constructors. * Subclasses can override this implementation if desired. * @throws Exception if the command object could not be instantiated * @see org.springframework.beans.BeanUtils#instantiateClass(Class) *///from w ww . j a v a 2 s. co m protected Object newCommandObject(Class<?> clazz) throws Exception { if (logger.isDebugEnabled()) { logger.debug("Creating new command of class [" + clazz.getName() + "]"); } return BeanUtils.instantiateClass(clazz); }
From source file:org.springframework.web.struts.ContextLoaderPlugIn.java
/** * Instantiate the WebApplicationContext for the ActionServlet, either a default * XmlWebApplicationContext or a custom context class if set. This implementation * expects custom contexts to implement ConfigurableWebApplicationContext. * Can be overridden in subclasses.//from www . j a v a2s. c o m * @throws org.springframework.beans.BeansException if the context couldn't be initialized * @see #setContextClass * @see org.springframework.web.context.support.XmlWebApplicationContext */ protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) throws BeansException { if (logger.isDebugEnabled()) { logger.debug("ContextLoaderPlugIn for Struts ActionServlet '" + getServletName() + "', module '" + getModulePrefix() + "' will try to create custom WebApplicationContext " + "context of class '" + getContextClass().getName() + "', using parent context [" + parent + "]"); } if (!ConfigurableWebApplicationContext.class.isAssignableFrom(getContextClass())) { throw new ApplicationContextException( "Fatal initialization error in ContextLoaderPlugIn for Struts ActionServlet '" + getServletName() + "', module '" + getModulePrefix() + "': custom WebApplicationContext class [" + getContextClass().getName() + "] is not of type ConfigurableWebApplicationContext"); } ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils .instantiateClass(getContextClass()); wac.setParent(parent); wac.setServletContext(getServletContext()); wac.setNamespace(getNamespace()); if (getContextConfigLocation() != null) { wac.setConfigLocations(StringUtils.tokenizeToStringArray(getContextConfigLocation(), ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS)); } wac.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { beanFactory.addBeanPostProcessor(new ActionServletAwareProcessor(getActionServlet())); } }); wac.refresh(); return wac; }
From source file:org.springframework.ws.test.support.MockStrategiesHelper.java
/** * Returns a single strategy found in the given application context, or instantiates a default strategy if no * applicable strategy was found./*from w w w. j a va2 s. co m*/ * * @param type the type of bean to be found in the application context * @param defaultType the type to instantiate and return when no bean of the specified type could be found * @return the bean found in the application context, or the default type if no bean of the given type can be found * @throws BeanInitializationException if there is more than 1 beans of the given type */ public <T, D extends T> T getStrategy(Class<T> type, Class<D> defaultType) { Assert.notNull(defaultType, "'defaultType' must not be null"); T t = getStrategy(type); if (t != null) { return t; } else { if (logger.isDebugEnabled()) { logger.debug("No " + ClassUtils.getShortName(type) + " found, using default " + ClassUtils.getShortName(defaultType)); } T defaultStrategy = BeanUtils.instantiateClass(defaultType); if (defaultStrategy instanceof ApplicationContextAware) { ApplicationContextAware applicationContextAware = (ApplicationContextAware) defaultStrategy; applicationContextAware.setApplicationContext(applicationContext); } if (defaultStrategy instanceof InitializingBean) { InitializingBean initializingBean = (InitializingBean) defaultStrategy; try { initializingBean.afterPropertiesSet(); } catch (Exception ex) { throw new BeanCreationException("Invocation of init method failed", ex); } } return defaultStrategy; } }
From source file:org.tinygroup.beanwrapper.TypeConverterDelegate.java
/** * Find a default editor for the given type. * @param requiredType the type to find an editor for * @param descriptor the JavaBeans descriptor for the property * @return the corresponding editor, or <code>null</code> if none */// ww w.j av a2s .c om protected PropertyEditor findDefaultEditor(Class requiredType, PropertyDescriptor descriptor) { PropertyEditor editor = null; if (descriptor != null) { if (JdkVersion.isAtLeastJava15()) { editor = descriptor.createPropertyEditor(this.targetObject); } else { Class editorClass = descriptor.getPropertyEditorClass(); if (editorClass != null) { editor = (PropertyEditor) BeanUtils.instantiateClass(editorClass); } } } if (editor == null && requiredType != null) { // No custom editor -> check BeanWrapperImpl's default editors. editor = (PropertyEditor) this.propertyEditorRegistry.getDefaultEditor(requiredType); if (editor == null && !String.class.equals(requiredType)) { // No BeanWrapper default editor -> check standard JavaBean editor. editor = BeanUtils.findEditorByConvention(requiredType); if (editor == null && !unknownEditorTypes.containsKey(requiredType)) { // Deprecated global PropertyEditorManager fallback... editor = PropertyEditorManager.findEditor(requiredType); if (editor == null) { // Regular case as of Spring 2.5 unknownEditorTypes.put(requiredType, Boolean.TRUE); } else { logger.warn("PropertyEditor [" + editor.getClass().getName() + "] found through deprecated global PropertyEditorManager fallback - " + "consider using a more isolated form of registration, e.g. on the BeanWrapper/BeanFactory!"); } } } } return editor; }
From source file:org.tinygroup.weblayer.webcontext.parser.util.BeanWrapperImpl.java
/** * Create new BeanWrapperImpl, wrapping a new instance of the specified class. * @param clazz class to instantiate and wrap *///from w ww.jav a 2 s . c o m public BeanWrapperImpl(Class clazz) { registerDefaultEditors(); setWrappedInstance(BeanUtils.instantiateClass(clazz)); }
From source file:org.wise.portal.spring.impl.CustomContextLoaderListener.java
/** * The behaviour of this method is the same as the superclass except for * setting of the config locations./* w ww. j av a2 s . co m*/ * * @throws ClassNotFoundException * * @see org.springframework.web.context.ContextLoader#createWebApplicationContext(javax.servlet.ServletContext, * org.springframework.context.ApplicationContext) */ @Override protected WebApplicationContext createWebApplicationContext(ServletContext servletContext) throws BeansException { Class<?> contextClass = determineContextClass(servletContext); if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type ConfigurableWebApplicationContext"); } ConfigurableWebApplicationContext webApplicationContext = (ConfigurableWebApplicationContext) BeanUtils .instantiateClass(contextClass); webApplicationContext.setServletContext(servletContext); String configClass = servletContext.getInitParameter(CONFIG_CLASS_PARAM); if (configClass != null) { try { SpringConfiguration springConfig = (SpringConfiguration) BeanUtils .instantiateClass(Class.forName(configClass)); webApplicationContext.setConfigLocations(springConfig.getRootApplicationContextConfigLocations()); } catch (ClassNotFoundException e) { if (LOGGER.isErrorEnabled()) { LOGGER.error(CONFIG_CLASS_PARAM + " <" + configClass + "> not found.", e); } throw new InvalidParameterException("ClassNotFoundException: " + configClass); } } else { throw new InvalidParameterException("No value defined for the required: " + CONFIG_CLASS_PARAM); } webApplicationContext.refresh(); return webApplicationContext; }
From source file:org.wise.WISE.java
/** * @param springConfigClassname/*w w w.ja va 2 s . c o m*/ * @throws ClassNotFoundException * @throws IOException */ public static void resetDB(String springConfigClassname) throws ClassNotFoundException, IOException { ConfigurableApplicationContext applicationContext = null; try { File wisePropertiesFile = new File("target/classes/wise.properties"); String wisePropertiesString = FileUtils.readFileToString(wisePropertiesFile); wisePropertiesString = wisePropertiesString.replaceAll("#hibernate.hbm2ddl.auto=create", ""); wisePropertiesString = wisePropertiesString.replaceAll("hibernate.hbm2ddl.auto=create", ""); wisePropertiesString = "hibernate.hbm2ddl.auto=create\n\n" + wisePropertiesString; FileUtils.writeStringToFile(wisePropertiesFile, wisePropertiesString); try { Thread.sleep(5000); // give it time to save the file } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } SpringConfiguration springConfig = (SpringConfiguration) BeanUtils .instantiateClass(Class.forName(springConfigClassname)); applicationContext = new ClassPathXmlApplicationContext( springConfig.getRootApplicationContextConfigLocations()); } finally { if (applicationContext != null) { applicationContext.close(); } } }