@Test public void testLazyLookupWithoutProxyInterface() throws NamingException { JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); jof.setJndiName("foo"); jof.setLookupOnStartup(false); try { jof.afterPropertiesSet(); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // expected } }
@Test public void testNotCacheWithoutProxyInterface() throws NamingException { JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); jof.setJndiName("foo"); jof.setCache(false); jof.setLookupOnStartup(false); try { jof.afterPropertiesSet(); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // expected } }
@Test public void testLookupWithProxyInterfaceAndLazyLookup() throws Exception { JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); final TestBean tb = new TestBean(); jof.setJndiTemplate(new JndiTemplate() { @Override public Object lookup(String name) { if ("foo".equals(name)) { tb.setName("tb"); return tb; } return null; } }); jof.setJndiName("foo"); jof.setProxyInterface(ITestBean.class); jof.setLookupOnStartup(false); jof.afterPropertiesSet(); assertTrue(jof.getObject() instanceof ITestBean); ITestBean proxy = (ITestBean) jof.getObject(); assertNull(tb.getName()); assertEquals(0, tb.getAge()); proxy.setAge(99); assertEquals("tb", tb.getName()); assertEquals(99, tb.getAge()); }
jof.setJndiName("foo"); jof.setProxyInterface(ITestBean.class); jof.setLookupOnStartup(false); jof.setCache(false); jof.afterPropertiesSet();
@Bean(destroyMethod="") public DataSource jndiDataSource() throws IllegalArgumentException, NamingException { JndiObjectFactoryBean bean = new JndiObjectFactoryBean(); bean.setJndiName("java:comp/env/jdbc/myDataSource"); bean.setProxyInterface(DataSource.class); bean.setLookupOnStartup(false); bean.afterPropertiesSet(); return (DataSource)bean.getObject(); } }
@Override public void initialize() throws Exception { super.initialize(); String jndiInitialContextFactoryClass = mqPropertyEntity.getMQEntity().getJndiInitialContextFactoryClass(); String url = mqPropertyEntity.getString(ThunderConstant.MQ_URL_ATTRIBUTE_NAME); String userName = mqPropertyEntity.getString(ThunderConstant.MQ_USER_NAME_ATTRIBUTE_NAME); String password = mqPropertyEntity.getString(ThunderConstant.MQ_PASSWORD_ATTRIBUTE_NAME); String jndiName = mqPropertyEntity.getString(ThunderConstant.MQ_JNDI_NAME_ATTRIBUTE_NAME); Properties environment = new Properties(); environment.put("java.naming.factory.initial", jndiInitialContextFactoryClass); environment.put("java.naming.provider.url", url); environment.put("java.naming.security.principal", userName); environment.put("java.naming.security.credentials", password); JndiTemplate jndiTemplate = new JndiTemplate(); jndiTemplate.setEnvironment(environment); JndiObjectFactoryBean targetConnectionFactory = new JndiObjectFactoryBean(); targetConnectionFactory.setJndiTemplate(jndiTemplate); targetConnectionFactory.setJndiName(jndiName); targetConnectionFactory.setLookupOnStartup(true); targetConnectionFactory.afterPropertiesSet(); setTargetConnectionFactory((ConnectionFactory) targetConnectionFactory.getObject()); afterPropertiesSet(); } }