public class AutowireThisDriver { private MySpringBean mySpringBean; public static void main(String[] args) { AutowireThisDriver atd = new AutowireThisDriver(); //get instance ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "/WEB-INF/applicationContext.xml"); //get Spring context //the magic: auto-wire the instance with all its dependencies: ctx.getAutowireCapableBeanFactory().autowireBeanProperties(atd, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); // code that uses mySpringBean ... mySpringBean.doStuff() // no need to instantiate - thanks to Spring } public void setMySpringBean(MySpringBean bean) { this.mySpringBean = bean; } }
@Before public void setup() throws Exception { // assertNotNull(client); String maven_opts = System.getenv( "MAVEN_OPTS" ); logger.info( "Maven options: " + maven_opts ); String[] locations = { "usergrid-test-context.xml" }; ac = new ClassPathXmlApplicationContext( locations ); AutowireCapableBeanFactory acbf = ac.getAutowireCapableBeanFactory(); acbf.autowireBeanProperties( this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false ); acbf.initializeBean( this, "testClient" ); }
private ClassPathXmlApplicationContext createContext(String config, String channelName) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config, getClass()); context.getAutowireCapableBeanFactory() .autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); channel = context.getBean(channelName, PollableChannel.class); return context; }
private ClassPathXmlApplicationContext createContext(String config, String channelName) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config, getClass()); context.getAutowireCapableBeanFactory() .autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); channel = context.getBean(channelName, MessageChannel.class); return context; }
private ClassPathXmlApplicationContext createContext(String config, String channelName) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config, getClass()); context.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); channel = context.getBean(channelName, MessageChannel.class); return context; }
/** * 根据当前spring容器的bean定义,解析对应的object并完成注入 */ public static void autowire(Object obj) { // 重新注入一下对象 context.getAutowireCapableBeanFactory().autowireBeanProperties(obj, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false); }
final ClassPathXmlApplicationContext beanFactory = new ClassPathXmlApplicationContext( "applicationContext.xml" ); AutowireCapableBeanFactory factory = beanFactory.getAutowireCapableBeanFactory(); factory.autowireBeanProperties( this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE , true ); ProductDao dao = context.getBean( "productDao" );
generator.getClass(), generator.getDisplayName(), generator.getId())); cax.getAutowireCapableBeanFactory() .autowireBeanProperties(generator, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
private void installGenerator(final URI pluginId, final Version version, final PluginVO plugin, final ClassPathXmlApplicationContext cax, final Generator<GeneratorConfiguration, GeneratorStatistics> generator) { cax.getAutowireCapableBeanFactory() .autowireBeanProperties(generator, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false); if (generator.getConfiguration() == null) { GeneratorConfiguration generatorConfiguration = generator.newConfiguration(); generator.setConfiguration(generatorConfiguration); } if (LifecyclePhase.NOT_INSTALLED.toString().equals(plugin.getState())) generator.install(true); else generator.install(false); pluginDAO.updatePluginState(pluginId, version, LifecyclePhase.INSTALLED.toString()); generator.initialize(); generators.put(generator.getId(), generator); contexts.put(generator.getId(), cax); logger.info("registered plugin " + generator.getSourceType()); pluginDAO.updatePluginState(pluginId, version, LifecyclePhase.INITIALIZED.toString()); }