/** * Delegates the given environment to underlying {@link XmlBeanDefinitionReader}. * Should be called before any call to {@code #load}. */ @Override public void setEnvironment(ConfigurableEnvironment environment) { super.setEnvironment(environment); this.reader.setEnvironment(getEnvironment()); }
/** * Create a new {@link XmlServletWebServerApplicationContext} that needs to be * {@linkplain #load loaded} and then manually {@link #refresh refreshed}. */ public XmlServletWebServerApplicationContext() { this.reader.setEnvironment(this.getEnvironment()); }
/** * Delegates the given environment to underlying {@link XmlBeanDefinitionReader}. * Should be called before any call to {@code #load}. */ @Override public void setEnvironment(ConfigurableEnvironment environment) { super.setEnvironment(environment); this.reader.setEnvironment(getEnvironment()); }
/** * <p> * Create a new context instance with no job parameters. * </p> */ public JsrXmlApplicationContext() { reader.setDocumentReaderClass(JsrBeanDefinitionDocumentReader.class); reader.setEnvironment(this.getEnvironment()); }
/** * Set the environment to be used by the underlying readers and scanner. * @param environment the environment */ public void setEnvironment(ConfigurableEnvironment environment) { this.annotatedReader.setEnvironment(environment); this.xmlReader.setEnvironment(environment); this.scanner.setEnvironment(environment); }
/** * {@inheritDoc} * <p> * Delegates the given environment to underlying {@link XmlBeanDefinitionReader}. * Should be called before any call to {@link #load}. */ @Override public void setEnvironment(ConfigurableEnvironment environment) { super.setEnvironment(environment); this.reader.setEnvironment(this.getEnvironment()); }
/** * <p> * Create a new context instance using the provided {@link Properties} representing job * parameters when pre-processing the job definition document. * </p> * * @param jobParameters the {@link Properties} representing job parameters */ public JsrXmlApplicationContext(Properties jobParameters) { reader.setDocumentReaderClass(JsrBeanDefinitionDocumentReader.class); reader.setEnvironment(this.getEnvironment()); storeJobParameters(jobParameters); }
private BeanDefinitionRegistry beanFactoryFor(String xmlName, String... activeProfiles) { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); StandardEnvironment env = new StandardEnvironment(); env.setActiveProfiles(activeProfiles); reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(xmlName, getClass())); return beanFactory; }
/** * Loads the bean definitions via an XmlBeanDefinitionReader. * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader * @see #initBeanDefinitionReader * @see #loadBeanDefinitions */ @Override protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { // Create a new XmlBeanDefinitionReader for the given BeanFactory. XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); // Configure the bean definition reader with this context's // resource loading environment. beanDefinitionReader.setEnvironment(this.getEnvironment()); beanDefinitionReader.setResourceLoader(this); beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); // Allow a subclass to provide custom initialization of the reader, // then proceed with actually loading the bean definitions. initBeanDefinitionReader(beanDefinitionReader); loadBeanDefinitions(beanDefinitionReader); }
/** * Loads the bean definitions via an XmlBeanDefinitionReader. * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader * @see #initBeanDefinitionReader * @see #loadBeanDefinitions */ @Override protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { // Create a new XmlBeanDefinitionReader for the given BeanFactory. XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); // Configure the bean definition reader with this context's // resource loading environment. beanDefinitionReader.setEnvironment(getEnvironment()); beanDefinitionReader.setResourceLoader(this); beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); // Allow a subclass to provide custom initialization of the reader, // then proceed with actually loading the bean definitions. initBeanDefinitionReader(beanDefinitionReader); loadBeanDefinitions(beanDefinitionReader); }
@Test public void testDefaultProfile() { { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); ConfigurableEnvironment env = new StandardEnvironment(); env.setDefaultProfiles("custom-default"); reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_ELIGIBLE_XML, getClass())); assertThat(beanFactory, not(containsTargetBean())); } { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); ConfigurableEnvironment env = new StandardEnvironment(); env.setDefaultProfiles("custom-default"); reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(CUSTOM_DEFAULT_ELIGIBLE_XML, getClass())); assertThat(beanFactory, containsTargetBean()); } }
/** * Loads the bean definitions via an XmlBeanDefinitionReader. * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader * @see #initBeanDefinitionReader * @see #loadBeanDefinitions */ @Override protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { // Create a new XmlBeanDefinitionReader for the given BeanFactory. XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); // Configure the bean definition reader with this context's // resource loading environment. beanDefinitionReader.setEnvironment(this.getEnvironment()); beanDefinitionReader.setResourceLoader(this); beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); // Allow a subclass to provide custom initialization of the reader, // then proceed with actually loading the bean definitions. initBeanDefinitionReader(beanDefinitionReader); loadBeanDefinitions(beanDefinitionReader); }
/** * Initialize the view bean factory from the XML file. * Synchronized because of access by parallel threads. * @throws BeansException in case of initialization errors */ protected synchronized BeanFactory initFactory() throws BeansException { if (this.cachedFactory != null) { return this.cachedFactory; } ApplicationContext applicationContext = obtainApplicationContext(); Resource actualLocation = this.location; if (actualLocation == null) { actualLocation = applicationContext.getResource(DEFAULT_LOCATION); } // Create child ApplicationContext for views. GenericWebApplicationContext factory = new GenericWebApplicationContext(); factory.setParent(applicationContext); factory.setServletContext(getServletContext()); // Load XML resource with context-aware entity resolver. XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); reader.setEnvironment(applicationContext.getEnvironment()); reader.setEntityResolver(new ResourceEntityResolver(applicationContext)); reader.loadBeanDefinitions(actualLocation); factory.refresh(); if (isCache()) { this.cachedFactory = factory; } return factory; }
/** * Loads the bean definitions via an XmlBeanDefinitionReader. * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader * @see #initBeanDefinitionReader * @see #loadBeanDefinitions */ @Override protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { // Create a new XmlBeanDefinitionReader for the given BeanFactory. XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); // Configure the bean definition reader with this context's // resource loading environment. beanDefinitionReader.setEnvironment(getEnvironment()); beanDefinitionReader.setResourceLoader(this); beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); // Allow a subclass to provide custom initialization of the reader, // then proceed with actually loading the bean definitions. initBeanDefinitionReader(beanDefinitionReader); loadBeanDefinitions(beanDefinitionReader); }
@Test public void getBean_withActiveProfile() { ConfigurableEnvironment env = new StandardEnvironment(); env.setActiveProfiles("dev"); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf); reader.setEnvironment(env); reader.loadBeanDefinitions(XML); bf.getBean("devOnlyBean"); // should not throw NSBDE Object foo = bf.getBean("foo"); assertThat(foo, instanceOf(Integer.class)); bf.getBean("devOnlyBean"); }
env.setActiveProfiles(DEV_ACTIVE); env.setDefaultProfiles("default"); reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass())); assertThat(beanFactory, containsTargetBean()); reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass())); assertThat(beanFactory, containsTargetBean()); reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass())); assertThat(beanFactory, containsTargetBean());
/** * Initialize the view bean factory from the XML file. * Synchronized because of access by parallel threads. * @throws BeansException in case of initialization errors */ protected synchronized BeanFactory initFactory() throws BeansException { if (this.cachedFactory != null) { return this.cachedFactory; } ApplicationContext applicationContext = obtainApplicationContext(); Resource actualLocation = this.location; if (actualLocation == null) { actualLocation = applicationContext.getResource(DEFAULT_LOCATION); } // Create child ApplicationContext for views. GenericWebApplicationContext factory = new GenericWebApplicationContext(); factory.setParent(applicationContext); factory.setServletContext(getServletContext()); // Load XML resource with context-aware entity resolver. XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); reader.setEnvironment(applicationContext.getEnvironment()); reader.setEntityResolver(new ResourceEntityResolver(applicationContext)); reader.loadBeanDefinitions(actualLocation); factory.refresh(); if (isCache()) { this.cachedFactory = factory; } return factory; }
@Override protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException { XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); // Configure the bean definition reader with this context's // resource loading environment. beanDefinitionReader.setEnvironment(this.getEnvironment()); beanDefinitionReader.setResourceLoader(this); beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); if (configLocations != null) { for (String configLocation : configLocations) { beanDefinitionReader.loadBeanDefinitions(configLocation); } } }
/** * <p> * Create a new context instance with no job parameters. * </p> */ public JsrXmlApplicationContext() { reader.setDocumentReaderClass(JsrBeanDefinitionDocumentReader.class); reader.setEnvironment(this.getEnvironment()); }
public class XmlBeanDefinitionReaderPostProcessor implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) beanFactory); ResourceLoader resourceLoader = new DefaultResourceLoader(); reader.setResourceLoader(new DefaultResourceLoader()); reader.setEntityResolver(new ResourceEntityResolver(resourceLoader)); reader.setEnvironment(new StandardEnvironment()); LocationService locationService = (LocationService) beanFactory.getBean("locationService"); reader.loadBeanDefinitions(locationService.getLocations()); } }