<bean id="connection.url" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass"><value>com.vincenzodevivo.Config</value></property> <property name="targetMethod"><value>getProperty</value></property> <property name="arguments"> <list> <value>connection.url</value> </list> </property> </bean> <bean id="test" class="com.vincenzodevivo.Test"> <property name="url" ref="connection.url"/> </bean>
Access the Spring-ApplicationContext from everywhere in your Application
package context; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * This class provides an application-wide access to the * Spring ApplicationContext! The ApplicationContext is * injected in a static method of the class "AppContext". * * Use AppContext.getApplicationContext() to get access * to all Spring Beans. * */ public class ApplicationContextProvider implements ApplicationContextAware { public void setApplicationContext(ApplicationContext ctx) throws BeansException { // Wiring the ApplicationContext into a static method AppContext.setApplicationContext(ctx); } } // .EOF
<bean id="contextApplicationContextProvider" class="context.ApplicationContextProvider"></bean>
package context; import org.springframework.context.ApplicationContext; /** * This class provides application-wide access to the Spring ApplicationContext. * The ApplicationContext is injected by the class "ApplicationContextProvider". * */ public class AppContext { private static ApplicationContext ctx; /** * Injected from the class "ApplicationContextProvider" which is automatically * loaded during Spring-Initialization. */ public static void setApplicationContext(ApplicationContext applicationContext) { ctx = applicationContext; } /** * Get access to the Spring ApplicationContext from everywhere in your Application. * * @return */ public static ApplicationContext getApplicationContext() { return ctx; } } // .EOF
Ora puoi accedere da tutte le classi allo Spring-ApplicationContext della tua applicazione chiamando il metodo statico:
ApplicationContext ctx = AppContext.getApplicationContext(); Honeypotbean honey = (HoneyPotBean) ctx.getBean("honey");
String xml = IOUtils.toString(new PathMatchingResourcePatternResolver().findPathMatchingResources(folder+"*.xml")[0].getURI().toURL().openStream());