/** * Overrides properties configured on beans. */ @Bean() @Lazy(false) @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) public static BeanFactoryPostProcessor propertyOverrideConfigurer() { PropertyOverrideConfigurer theOverrideConfigurer = new PropertyOverrideConfigurer(); final Properties thePropertiesHolder = new Properties(); /* Task refresh interval. */ thePropertiesHolder.put("starterService.taskReschedulingCronExpression", "0/20 * * * * ?"); /* Transport service configuration refresh interval. */ thePropertiesHolder.put("starterService.transportServiceConfigurationRefreshCronExpression", "0/30 * * * * ?"); /* Task execution status reports cleanup interval. */ thePropertiesHolder.put("starterService.taskExecutionStatusCleanupCronExpression", "0/30 0/15 * * * ?"); theOverrideConfigurer.setProperties(thePropertiesHolder); theOverrideConfigurer.setIgnoreInvalidKeys(false); theOverrideConfigurer.setIgnoreResourceNotFound(false); theOverrideConfigurer.setOrder(0); return theOverrideConfigurer; }
/** * Overrides properties configured on beans. */ @Bean() @Lazy(false) @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) public static BeanFactoryPostProcessor propertyOverrideConfigurer() { PropertyOverrideConfigurer theOverrideConfigurer = new PropertyOverrideConfigurer(); final Properties thePropertiesHolder = new Properties(); /* Task refresh interval. */ thePropertiesHolder.put("starterService.taskReschedulingCronExpression", "* 4/30 * * * ?"); /* Transport service configuration refresh interval. */ thePropertiesHolder.put("starterService.transportServiceConfigurationRefreshCronExpression", "* 5/30 * * * ?"); /* Task execution status reports cleanup interval. */ thePropertiesHolder.put("starterService.taskExecutionStatusCleanupCronExpression", "0/5 * * * * ?"); theOverrideConfigurer.setProperties(thePropertiesHolder); theOverrideConfigurer.setIgnoreInvalidKeys(false); theOverrideConfigurer.setIgnoreResourceNotFound(false); theOverrideConfigurer.setOrder(0); return theOverrideConfigurer; }
@Test public void propertyOverride() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "contextNamespaceHandlerTests-override.xml", getClass()); Map<String, PropertyOverrideConfigurer> beans = applicationContext .getBeansOfType(PropertyOverrideConfigurer.class); assertFalse("No PropertyOverrideConfigurer found", beans.isEmpty()); Date date = (Date) applicationContext.getBean("date"); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); assertEquals(42, calendar.get(Calendar.MINUTE)); }
public static void iocStart() { // BeanFactory方式启动 ConfigurableListableBeanFactory beanFactory = new XmlBeanFactory( new ClassPathResource("spring.xml")); // 读取外部properties文件属性值填充xml中的bean,property PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer(); propertyPlaceholderConfigurer.setLocation(new ClassPathResource( "config.properties")); // 应用postProcessBeanFactory propertyPlaceholderConfigurer.postProcessBeanFactory(beanFactory); // 读取外部properties文件,覆盖xml中bean指定的property值 PropertyOverrideConfigurer propertyOverrideConfigurer = new PropertyOverrideConfigurer(); propertyOverrideConfigurer.setLocation(new ClassPathResource( "spring-adjustment.properties")); propertyOverrideConfigurer.postProcessBeanFactory(beanFactory); // CustomEditorConfigurer,对bean的property值,做类型转换支持 CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer(); Map customEditors = new HashMap(); customEditors.put(Date.class, new DatePropertyEditor()); customEditorConfigurer.setCustomEditors(customEditors); customEditorConfigurer.postProcessBeanFactory(beanFactory); FXNewsProvider newsProvider = (FXNewsProvider) beanFactory .getBean("newsProvider"); System.out.println(newsProvider.getClientId()); System.out.println(newsProvider.getBaseUrl()); System.out.println(newsProvider.getAddDate().toLocaleString()); }
@Test public void propertyOverrideConfigurer() { PropertyOverrideConfigurer bean = (PropertyOverrideConfigurer) applicationContext .getBean("propertyOverrideConfigurer"); System.out.println(bean); assertNotNull(bean); }
@Test public void propertyOverride() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "contextNamespaceHandlerTests-override.xml", getClass()); Map<String, PropertyOverrideConfigurer> beans = applicationContext .getBeansOfType(PropertyOverrideConfigurer.class); assertFalse("No PropertyOverrideConfigurer found", beans.isEmpty()); Date date = (Date) applicationContext.getBean("date"); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); assertEquals("No properties overriden", 42, calendar.get(Calendar.MINUTE)); }
@Override protected Class<?> getBeanClass(Element element) { return PropertyOverrideConfigurer.class; }
/** * create a component manager based on a list of component.xml * @param configPaths a ';' seperated list of xml bean config files * @throws IOException */ public TestComponentManagerContainer(String configPaths, Properties props) throws IOException { // we assume that all the jars are in the same classloader, so this will // not check for // incorrect bindings and will not fully replicate the tomcat // experience, but is an easier environment // to work within for kernel testing. // For a more complex structure we could use the kernel poms in the repo // to generate the dep list. if ( ComponentManager.m_componentManager != null ) { log.info("Closing existing Component Manager "); /* try { ComponentManager.m_componentManager.close(); } catch ( Throwable t ) { log.warn("Close Failed with message, safe to ignore "+t.getMessage()); } */ log.info("Closing Complete "); ComponentManager.m_componentManager = null; } log.info("Starting Component Manager with ["+configPaths+"]"); ComponentManager.setLateRefresh(true); componentManager = (SpringCompMgr) ComponentManager.getInstance(); ConfigurableApplicationContext ac = componentManager.getApplicationContext(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); // Supply any additional configuration. if (props != null) { PropertyOverrideConfigurer beanFactoryPostProcessor = new PropertyOverrideConfigurer(); beanFactoryPostProcessor.setBeanNameSeparator("@"); beanFactoryPostProcessor.setProperties(props); ac.addBeanFactoryPostProcessor(beanFactoryPostProcessor); } // we could take the kernel bootstrap from from the classpath in future // rather than from // the filesystem List<Resource> config = new ArrayList<Resource>(); String[] configPath = configPaths.split(";"); for ( String p : configPath) { File xml = new File(p); config.add(new FileSystemResource(xml.getCanonicalPath())); } loadComponent(ac, config, classLoader); ac.refresh(); // SAK-20908 - band-aid for TLM sync issues causing tests to fail // This sleep shouldn't be needed but it seems these tests are starting before ThreadLocalManager has finished its startup. try { Thread.sleep(500); // 1/2 second log.debug("Finished starting the component manager"); } catch (InterruptedException e) { log.error("Component manager startup interrupted..."); } }
@Override protected Class getBeanClass(Element element) { return PropertyOverrideConfigurer.class; }
@Before public void onSetUpInTransactionIfEnabled() throws Exception { m_fileAnticipator = new FileAnticipator(); String filename = m_fileAnticipator.expecting("remote-poller.configuration").getCanonicalPath(); filename = filename.replace("+", "%2B"); System.setProperty("opennms.poller.configuration.resource", "file://" + filename); m_populator.populateDatabase(); /** * We complete and end the transaction here so that the populated * database gets committed. If we don't do this, the poller back * end (setup with the contexts in getConfigLocations) won't see * the populated database because it's working in another * transaction. This will cause one of the asserts in testRegister * to fail because no services will be monitored by the remote * poller. */ /* setComplete(); endTransaction(); */ m_frontEndContext = new ClassPathXmlApplicationContext( new String[] { "classpath:/META-INF/opennms/applicationContext-remotePollerBackEnd-rmi.xml", "classpath:/META-INF/opennms/applicationContext-pollerFrontEnd.xml", }, false ); Properties props = new Properties(); props.setProperty("configCheckTrigger.repeatInterval", "1000"); PropertyOverrideConfigurer testPropertyConfigurer = new PropertyOverrideConfigurer(); testPropertyConfigurer.setProperties(props); m_frontEndContext.addBeanFactoryPostProcessor(testPropertyConfigurer); m_frontEndContext.refresh(); m_frontEnd = (PollerFrontEnd)m_frontEndContext.getBean("pollerFrontEnd"); m_settings = (PollerSettings)m_frontEndContext.getBean("pollerSettings"); }