下面列出了怎么用org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Loads the bean definitions via an GroovyBeanDefinitionReader.
* @see org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader
* @see #initBeanDefinitionReader
* @see #loadBeanDefinitions
*/
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
GroovyBeanDefinitionReader beanDefinitionReader = new GroovyBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's
// resource loading environment.
beanDefinitionReader.setEnvironment(getEnvironment());
beanDefinitionReader.setResourceLoader(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 GroovyBeanDefinitionReader.
* @see org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader
* @see #initBeanDefinitionReader
* @see #loadBeanDefinitions
*/
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
GroovyBeanDefinitionReader beanDefinitionReader = new GroovyBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's
// resource loading environment.
beanDefinitionReader.setEnvironment(getEnvironment());
beanDefinitionReader.setResourceLoader(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 GroovyBeanDefinitionReader.
* @see org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader
* @see #initBeanDefinitionReader
* @see #loadBeanDefinitions
*/
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
GroovyBeanDefinitionReader beanDefinitionReader = new GroovyBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's
// resource loading environment.
beanDefinitionReader.setEnvironment(getEnvironment());
beanDefinitionReader.setResourceLoader(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 GroovyBeanDefinitionReader.
* @see org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader
* @see #initBeanDefinitionReader
* @see #loadBeanDefinitions
*/
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
GroovyBeanDefinitionReader beanDefinitionReader = new GroovyBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's
// resource loading environment.
beanDefinitionReader.setEnvironment(getEnvironment());
beanDefinitionReader.setResourceLoader(this);
// Allow a subclass to provide custom initialization of the reader,
// then proceed with actually loading the bean definitions.
initBeanDefinitionReader(beanDefinitionReader);
loadBeanDefinitions(beanDefinitionReader);
}
private static void initGroovyConf(GenericApplicationContext context){
String classPath=ProcessInfo.getGroovyClassPath();
if(Help.isNotEmpty(classPath)){
GroovyBeanDefinitionReader reader=new GroovyBeanDefinitionReader(context);
reader.loadBeanDefinitions(new ClassPathResource(classPath));
}
}
private void loadBeanDefinitionsFromImportedResources(
Map<String, Class<? extends BeanDefinitionReader>> importedResources) {
Map<Class<?>, BeanDefinitionReader> readerInstanceCache = new HashMap<>();
importedResources.forEach((resource, readerClass) -> {
// Default reader selection necessary?
if (BeanDefinitionReader.class == readerClass) {
if (StringUtils.endsWithIgnoreCase(resource, ".groovy")) {
// When clearly asking for Groovy, that's what they'll get...
readerClass = GroovyBeanDefinitionReader.class;
}
else {
// Primarily ".xml" files but for any other extension as well
readerClass = XmlBeanDefinitionReader.class;
}
}
BeanDefinitionReader reader = readerInstanceCache.get(readerClass);
if (reader == null) {
try {
// Instantiate the specified BeanDefinitionReader
reader = readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry);
// Delegate the current ResourceLoader to it if possible
if (reader instanceof AbstractBeanDefinitionReader) {
AbstractBeanDefinitionReader abdr = ((AbstractBeanDefinitionReader) reader);
abdr.setResourceLoader(this.resourceLoader);
abdr.setEnvironment(this.environment);
}
readerInstanceCache.put(readerClass, reader);
}
catch (Throwable ex) {
throw new IllegalStateException(
"Could not instantiate BeanDefinitionReader class [" + readerClass.getName() + "]");
}
}
// TODO SPR-6310: qualify relative path locations as done in AbstractContextLoader.modifyLocations
reader.loadBeanDefinitions(resource);
});
}
private void loadBeanDefinitionsFromImportedResources(
Map<String, Class<? extends BeanDefinitionReader>> importedResources) {
Map<Class<?>, BeanDefinitionReader> readerInstanceCache = new HashMap<>();
importedResources.forEach((resource, readerClass) -> {
// Default reader selection necessary?
if (BeanDefinitionReader.class == readerClass) {
if (StringUtils.endsWithIgnoreCase(resource, ".groovy")) {
// When clearly asking for Groovy, that's what they'll get...
readerClass = GroovyBeanDefinitionReader.class;
}
else {
// Primarily ".xml" files but for any other extension as well
readerClass = XmlBeanDefinitionReader.class;
}
}
BeanDefinitionReader reader = readerInstanceCache.get(readerClass);
if (reader == null) {
try {
// Instantiate the specified BeanDefinitionReader
reader = readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry);
// Delegate the current ResourceLoader to it if possible
if (reader instanceof AbstractBeanDefinitionReader) {
AbstractBeanDefinitionReader abdr = ((AbstractBeanDefinitionReader) reader);
abdr.setResourceLoader(this.resourceLoader);
abdr.setEnvironment(this.environment);
}
readerInstanceCache.put(readerClass, reader);
}
catch (Throwable ex) {
throw new IllegalStateException(
"Could not instantiate BeanDefinitionReader class [" + readerClass.getName() + "]");
}
}
// TODO SPR-6310: qualify relative path locations as done in AbstractContextLoader.modifyLocations
reader.loadBeanDefinitions(resource);
});
}
private void loadBeanDefinitionsFromImportedResources(
Map<String, Class<? extends BeanDefinitionReader>> importedResources) {
Map<Class<?>, BeanDefinitionReader> readerInstanceCache = new HashMap<Class<?>, BeanDefinitionReader>();
for (Map.Entry<String, Class<? extends BeanDefinitionReader>> entry : importedResources.entrySet()) {
String resource = entry.getKey();
Class<? extends BeanDefinitionReader> readerClass = entry.getValue();
// Default reader selection necessary?
if (BeanDefinitionReader.class == readerClass) {
if (StringUtils.endsWithIgnoreCase(resource, ".groovy")) {
// When clearly asking for Groovy, that's what they'll get...
readerClass = GroovyBeanDefinitionReader.class;
}
else {
// Primarily ".xml" files but for any other extension as well
readerClass = XmlBeanDefinitionReader.class;
}
}
BeanDefinitionReader reader = readerInstanceCache.get(readerClass);
if (reader == null) {
try {
// Instantiate the specified BeanDefinitionReader
reader = readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry);
// Delegate the current ResourceLoader to it if possible
if (reader instanceof AbstractBeanDefinitionReader) {
AbstractBeanDefinitionReader abdr = ((AbstractBeanDefinitionReader) reader);
abdr.setResourceLoader(this.resourceLoader);
abdr.setEnvironment(this.environment);
}
readerInstanceCache.put(readerClass, reader);
}
catch (Throwable ex) {
throw new IllegalStateException(
"Could not instantiate BeanDefinitionReader class [" + readerClass.getName() + "]");
}
}
// TODO SPR-6310: qualify relative path locations as done in AbstractContextLoader.modifyLocations
reader.loadBeanDefinitions(resource);
}
}
private void loadBeanDefinitionsFromImportedResources(
Map<String, Class<? extends BeanDefinitionReader>> importedResources) {
Map<Class<?>, BeanDefinitionReader> readerInstanceCache = new HashMap<Class<?>, BeanDefinitionReader>();
for (Map.Entry<String, Class<? extends BeanDefinitionReader>> entry : importedResources.entrySet()) {
String resource = entry.getKey();
Class<? extends BeanDefinitionReader> readerClass = entry.getValue();
// Default reader selection necessary?
if (BeanDefinitionReader.class == readerClass) {
if (StringUtils.endsWithIgnoreCase(resource, ".groovy")) {
// When clearly asking for Groovy, that's what they'll get...
readerClass = GroovyBeanDefinitionReader.class;
}
else {
// Primarily ".xml" files but for any other extension as well
readerClass = XmlBeanDefinitionReader.class;
}
}
BeanDefinitionReader reader = readerInstanceCache.get(readerClass);
if (reader == null) {
try {
// Instantiate the specified BeanDefinitionReader
reader = readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry);
// Delegate the current ResourceLoader to it if possible
if (reader instanceof AbstractBeanDefinitionReader) {
AbstractBeanDefinitionReader abdr = ((AbstractBeanDefinitionReader) reader);
abdr.setResourceLoader(this.resourceLoader);
abdr.setEnvironment(this.environment);
}
readerInstanceCache.put(readerClass, reader);
}
catch (Exception ex) {
throw new IllegalStateException(
"Could not instantiate BeanDefinitionReader class [" + readerClass.getName() + "]");
}
}
// TODO SPR-6310: qualify relative path locations as done in AbstractContextLoader.modifyLocations
reader.loadBeanDefinitions(resource);
}
}
@Override
protected BeanDefinitionReader createBeanDefinitionReader(
GenericApplicationContext context) {
return new GroovyBeanDefinitionReader(context);
}
@Override
protected BeanDefinitionReader createBeanDefinitionReader(
GenericApplicationContext context) {
return new GroovyBeanDefinitionReader(context);
}
/**
* Load the bean definitions with the given GroovyBeanDefinitionReader.
* <p>The lifecycle of the bean factory is handled by the refreshBeanFactory method;
* therefore this method is just supposed to load and/or register bean definitions.
* <p>Delegates to a ResourcePatternResolver for resolving location patterns
* into Resource instances.
* @throws IOException if the required Groovy script or XML file isn't found
* @see #refreshBeanFactory
* @see #getConfigLocations
* @see #getResources
* @see #getResourcePatternResolver
*/
protected void loadBeanDefinitions(GroovyBeanDefinitionReader reader) throws IOException {
String[] configLocations = getConfigLocations();
if (configLocations != null) {
for (String configLocation : configLocations) {
reader.loadBeanDefinitions(configLocation);
}
}
}
/**
* Load the bean definitions with the given GroovyBeanDefinitionReader.
* <p>The lifecycle of the bean factory is handled by the refreshBeanFactory method;
* therefore this method is just supposed to load and/or register bean definitions.
* <p>Delegates to a ResourcePatternResolver for resolving location patterns
* into Resource instances.
* @throws IOException if the required Groovy script or XML file isn't found
* @see #refreshBeanFactory
* @see #getConfigLocations
* @see #getResources
* @see #getResourcePatternResolver
*/
protected void loadBeanDefinitions(GroovyBeanDefinitionReader reader) throws IOException {
String[] configLocations = getConfigLocations();
if (configLocations != null) {
for (String configLocation : configLocations) {
reader.loadBeanDefinitions(configLocation);
}
}
}
/**
* Load the bean definitions with the given GroovyBeanDefinitionReader.
* <p>The lifecycle of the bean factory is handled by the refreshBeanFactory method;
* therefore this method is just supposed to load and/or register bean definitions.
* <p>Delegates to a ResourcePatternResolver for resolving location patterns
* into Resource instances.
* @throws IOException if the required Groovy script or XML file isn't found
* @see #refreshBeanFactory
* @see #getConfigLocations
* @see #getResources
* @see #getResourcePatternResolver
*/
protected void loadBeanDefinitions(GroovyBeanDefinitionReader reader) throws IOException {
String[] configLocations = getConfigLocations();
if (configLocations != null) {
for (String configLocation : configLocations) {
reader.loadBeanDefinitions(configLocation);
}
}
}
/**
* Load the bean definitions with the given GroovyBeanDefinitionReader.
* <p>The lifecycle of the bean factory is handled by the refreshBeanFactory method;
* therefore this method is just supposed to load and/or register bean definitions.
* <p>Delegates to a ResourcePatternResolver for resolving location patterns
* into Resource instances.
* @throws IOException if the required Groovy script or XML file isn't found
* @see #refreshBeanFactory
* @see #getConfigLocations
* @see #getResources
* @see #getResourcePatternResolver
*/
protected void loadBeanDefinitions(GroovyBeanDefinitionReader reader) throws IOException {
String[] configLocations = getConfigLocations();
if (configLocations != null) {
for (String configLocation : configLocations) {
reader.loadBeanDefinitions(configLocation);
}
}
}
/**
* Exposes the underlying {@link GroovyBeanDefinitionReader} for convenient access
* to the {@code loadBeanDefinition} methods on it as well as the ability
* to specify an inline Groovy bean definition closure.
* @see GroovyBeanDefinitionReader#loadBeanDefinitions(org.springframework.core.io.Resource...)
* @see GroovyBeanDefinitionReader#loadBeanDefinitions(String...)
*/
public final GroovyBeanDefinitionReader getReader() {
return this.reader;
}
/**
* Initialize the bean definition reader used for loading the bean
* definitions of this context. Default implementation is empty.
* <p>Can be overridden in subclasses.
* @param beanDefinitionReader the bean definition reader used by this context
*/
protected void initBeanDefinitionReader(GroovyBeanDefinitionReader beanDefinitionReader) {
}
/**
* Load bean definitions into the supplied {@link GenericWebApplicationContext context}
* from the locations in the supplied {@code WebMergedContextConfiguration} using a
* {@link GroovyBeanDefinitionReader}.
* @param context the context into which the bean definitions should be loaded
* @param webMergedConfig the merged context configuration
* @see AbstractGenericWebContextLoader#loadBeanDefinitions
*/
@Override
protected void loadBeanDefinitions(GenericWebApplicationContext context,
WebMergedContextConfiguration webMergedConfig) {
new GroovyBeanDefinitionReader(context).loadBeanDefinitions(webMergedConfig.getLocations());
}
/**
* Load bean definitions into the supplied {@link GenericApplicationContext context}
* from the locations in the supplied {@code MergedContextConfiguration} using a
* {@link GroovyBeanDefinitionReader}.
* @param context the context into which the bean definitions should be loaded
* @param mergedConfig the merged context configuration
* @see org.springframework.test.context.support.AbstractGenericContextLoader#loadBeanDefinitions
*/
@Override
protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) {
new GroovyBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations());
}
/**
* Exposes the underlying {@link GroovyBeanDefinitionReader} for convenient access
* to the {@code loadBeanDefinition} methods on it as well as the ability
* to specify an inline Groovy bean definition closure.
* @see GroovyBeanDefinitionReader#loadBeanDefinitions(org.springframework.core.io.Resource...)
* @see GroovyBeanDefinitionReader#loadBeanDefinitions(String...)
*/
public final GroovyBeanDefinitionReader getReader() {
return this.reader;
}
/**
* Initialize the bean definition reader used for loading the bean
* definitions of this context. Default implementation is empty.
* <p>Can be overridden in subclasses.
* @param beanDefinitionReader the bean definition reader used by this context
*/
protected void initBeanDefinitionReader(GroovyBeanDefinitionReader beanDefinitionReader) {
}
/**
* Load bean definitions into the supplied {@link GenericWebApplicationContext context}
* from the locations in the supplied {@code WebMergedContextConfiguration} using a
* {@link GroovyBeanDefinitionReader}.
* @param context the context into which the bean definitions should be loaded
* @param webMergedConfig the merged context configuration
* @see AbstractGenericWebContextLoader#loadBeanDefinitions
*/
@Override
protected void loadBeanDefinitions(GenericWebApplicationContext context,
WebMergedContextConfiguration webMergedConfig) {
new GroovyBeanDefinitionReader(context).loadBeanDefinitions(webMergedConfig.getLocations());
}
/**
* Load bean definitions into the supplied {@link GenericApplicationContext context}
* from the locations in the supplied {@code MergedContextConfiguration} using a
* {@link GroovyBeanDefinitionReader}.
* @param context the context into which the bean definitions should be loaded
* @param mergedConfig the merged context configuration
* @see org.springframework.test.context.support.AbstractGenericContextLoader#loadBeanDefinitions
*/
@Override
protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) {
new GroovyBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations());
}
/**
* Initialize the bean definition reader used for loading the bean
* definitions of this context. Default implementation is empty.
* <p>Can be overridden in subclasses.
* @param beanDefinitionReader the bean definition reader used by this context
*/
protected void initBeanDefinitionReader(GroovyBeanDefinitionReader beanDefinitionReader) {
}
/**
* Exposes the underlying {@link GroovyBeanDefinitionReader} for convenient access
* to the {@code loadBeanDefinition} methods on it as well as the ability
* to specify an inline Groovy bean definition closure.
* @see GroovyBeanDefinitionReader#loadBeanDefinitions(org.springframework.core.io.Resource...)
* @see GroovyBeanDefinitionReader#loadBeanDefinitions(String...)
*/
public final GroovyBeanDefinitionReader getReader() {
return this.reader;
}
/**
* Exposes the underlying {@link GroovyBeanDefinitionReader} for convenient access
* to the {@code loadBeanDefinition} methods on it as well as the ability
* to specify an inline Groovy bean definition closure.
* @see GroovyBeanDefinitionReader#loadBeanDefinitions(org.springframework.core.io.Resource...)
* @see GroovyBeanDefinitionReader#loadBeanDefinitions(String...)
*/
public final GroovyBeanDefinitionReader getReader() {
return this.reader;
}
/**
* Initialize the bean definition reader used for loading the bean
* definitions of this context. Default implementation is empty.
* <p>Can be overridden in subclasses.
* @param beanDefinitionReader the bean definition reader used by this context
*/
protected void initBeanDefinitionReader(GroovyBeanDefinitionReader beanDefinitionReader) {
}
/**
* Load bean definitions into the supplied {@link GenericWebApplicationContext context}
* from the locations in the supplied {@code WebMergedContextConfiguration} using a
* {@link GroovyBeanDefinitionReader}.
*
* @param context the context into which the bean definitions should be loaded
* @param webMergedConfig the merged context configuration
* @see AbstractGenericWebContextLoader#loadBeanDefinitions
*/
@Override
protected void loadBeanDefinitions(GenericWebApplicationContext context,
WebMergedContextConfiguration webMergedConfig) {
new GroovyBeanDefinitionReader(context).loadBeanDefinitions(webMergedConfig.getLocations());
}
/**
* Load bean definitions into the supplied {@link GenericApplicationContext context}
* from the locations in the supplied {@code MergedContextConfiguration} using a
* {@link GroovyBeanDefinitionReader}.
*
* @param context the context into which the bean definitions should be loaded
* @param mergedConfig the merged context configuration
* @see org.springframework.test.context.support.AbstractGenericContextLoader#loadBeanDefinitions
*/
@Override
protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) {
new GroovyBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations());
}