New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guice will not inject properties on JndiDataSourceProvider in PrivateModules #19

Merged
merged 3 commits into from Feb 25, 2014
Jump to file or symbol
Failed to load files and symbols.
+531 −10
Diff settings

Always

Just for now

View
@@ -20,7 +20,7 @@
<parent>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-parent</artifactId>
<version>19-SNAPSHOT</version>
<version>21</version>
</parent>
<artifactId>mybatis-guice</artifactId>
@@ -17,6 +17,7 @@
import org.apache.ibatis.datasource.jndi.JndiDataSourceFactory;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.naming.Context;
@@ -37,23 +38,24 @@
private final Properties properties = new Properties();
/**
* Creates a new JndiDataSourceProvider with the specified
* JNDI data source.
*
*
* @param initialContext
* @param dataSource the JNDI datasource name (fully qualified)
*/
@com.google.inject.Inject(optional = true)
public void setInitialContext(@Named("jndi.initialContext") final String initialContext) {
properties.setProperty(JndiDataSourceFactory.INITIAL_CONTEXT, initialContext);
@Inject
public JndiDataSourceProvider(@Named("jndi.dataSource") final String dataSource) {

This comment has been minimized.

@jeffgbutler

jeffgbutler Feb 21, 2014

Member

Adding a required injection to the constructor seems to "wake up" Guice and all the other properties will be injected as expected.

@jeffgbutler

jeffgbutler Feb 21, 2014

Member

Adding a required injection to the constructor seems to "wake up" Guice and all the other properties will be injected as expected.

properties.setProperty(JndiDataSourceFactory.DATA_SOURCE, dataSource);
}
/**
*
*
* @param dataSource
* @param initialContext
*/
@com.google.inject.Inject(optional = true)
public void setDataSource(@Named("jndi.dataSource") final String dataSource) {
properties.setProperty(JndiDataSourceFactory.DATA_SOURCE, dataSource);
public void setInitialContext(@Named("jndi.initialContext") final String initialContext) {
properties.setProperty(JndiDataSourceFactory.INITIAL_CONTEXT, initialContext);
}
/**
@@ -84,5 +86,4 @@ public DataSource get() {
factory.setProperties(properties);
return factory.getDataSource();
}
}
@@ -0,0 +1,24 @@
package org.mybatis.guice.multidstest;
import java.util.HashMap;
import java.util.Map;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class MockContext extends InitialContext {
private static Map<String, Object> bindings = new HashMap<String, Object>();
public MockContext(boolean lazy) throws NamingException {
super(lazy);
}
public Object lookup(String name) throws NamingException {
return bindings.get(name);
}
public void bind(String name, Object obj) throws NamingException {
bindings.put(name, obj);
}
}
@@ -0,0 +1,15 @@
package org.mybatis.guice.multidstest;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;
public class MockInitialContextFactory implements InitialContextFactory {
public Context getInitialContext(Hashtable<?, ?> environment)
throws NamingException {
return new MockContext(false);
}
}
@@ -0,0 +1,101 @@
package org.mybatis.guice.multidstest;
import static org.junit.Assert.assertEquals;
import java.util.Properties;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.Test;
import org.mybatis.guice.MyBatisModule;
import org.mybatis.guice.datasource.builtin.PooledDataSourceProvider;
import org.mybatis.guice.datasource.helper.JdbcHelper;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.PrivateModule;
import com.google.inject.name.Names;
public class MultiJDBCTest {
@Test
public void testMultiDSWithJDBC() {
Injector injector = setupInjector();
Schema1Service schema1Service = injector
.getInstance(Schema1Service.class);
schema1Service.createSchema1();
Integer int1 = schema1Service.getNextValueFromSchema1();
assertEquals(100, int1.intValue());
Schema2Service schema2Service = injector
.getInstance(Schema2Service.class);
schema2Service.createSchema2();
Integer int2 = schema2Service.getNextValueFromSchema2();
assertEquals(200, int2.intValue());
}
private Injector setupInjector() {
Injector injector = Guice.createInjector(new PrivateModule() {
@Override
protected void configure() {
install(new MyBatisModule() {
@Override
protected void initialize() {
bindDataSourceProviderType(PooledDataSourceProvider.class);
bindTransactionFactoryType(JdbcTransactionFactory.class);
install(JdbcHelper.HSQLDB_IN_MEMORY_NAMED);
Properties connectionProps = new Properties();
connectionProps.setProperty("mybatis.environment.id",
"jdbc");
connectionProps.setProperty("JDBC.username", "sa");
connectionProps.setProperty("JDBC.password", "");
connectionProps.setProperty("JDBC.schema", "schema1");
connectionProps.setProperty("JDBC.autoCommit", "false");
Names.bindProperties(binder(), connectionProps);
addMapperClass(Schema1Mapper.class);
bind(Schema1Service.class);
}
});
expose(Schema1Service.class);
}
}, new PrivateModule() {
@Override
protected void configure() {
install(new MyBatisModule() {
@Override
protected void initialize() {
bindDataSourceProviderType(PooledDataSourceProvider.class);
bindTransactionFactoryType(JdbcTransactionFactory.class);
install(JdbcHelper.HSQLDB_IN_MEMORY_NAMED);
Properties connectionProps = new Properties();
connectionProps.setProperty("mybatis.environment.id",
"jdbc");
connectionProps.setProperty("JDBC.username", "sa");
connectionProps.setProperty("JDBC.password", "");
connectionProps.setProperty("JDBC.schema", "schema2");
connectionProps.setProperty("JDBC.autoCommit", "false");
Names.bindProperties(binder(), connectionProps);
addMapperClass(Schema2Mapper.class);
bind(Schema2Service.class);
}
});
expose(Schema2Service.class);
}
});
return injector;
}
}
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.