Guice数据库工厂

Guice database factory

我有一个 Java 项目,我需要实施 Guice。

我有一个模块化项目,所以我可以轻松地在数据库之间切换。为此,我有如下所示的 DBConnection 工厂:

    public abstract class DBConnectionFactory {
    // List of DAO types supported by the factory
    public static final int MYSQL = 1;
    public static final int ORACLE = 2;

    public abstract FilterDAO getFilterDAO();
    public abstract PhotoDAO getPhotoDAO();
    public abstract SetDAO getSetDAO();

    public static DBConnectionFactory getDBConnectionFactory(int whichFactory) {
        switch (whichFactory)   {
            case MYSQL:
                return new MySQLFactory();
            case ORACLE:
                return new OracleFactory();
            default:
                return null;
        }
    }
}

工厂 returns 一个连接,它像这样寻找 MySQL:

  public class MySQLFactory extends DBConnectionFactory {
    public static final String DRIVER= "com.mysql.jdbc.Driver";
    public static final String DBURL= "jdbcurl";
    private static final String PASSWORD = "password";
    private static final String USERNAME = "username";

    // method to create MySQL connection
    public static Connection createConnection() {
        try {
            // This will load the MySQL driver, each DB has its own driver
            Class.forName(DRIVER);
            Connection connect = DriverManager.getConnection(DBURL, USERNAME, PASSWORD);
            return connect;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    public FilterDAO getFilterDAO() { return new MySQLFilterDAO(); }
    public PhotoDAO getPhotoDAO() {
        return new MySQLPhotoDAO();
    }
    public SetDAO getSetDAO() {
        return new MySQLSetDAO();
    }
}

在我的 ServletController 中,我有这段代码来建立连接:

// Create a DAO
private DBConnectionFactory MySQLFactory = DBConnectionFactory.getDBConnectionFactory(DBConnectionFactory.MYSQL);

@Override
public void init(){
    photoDAO = MySQLFactory.getPhotoDAO();
}

我正试图摆脱工厂并使用 Guice,但我不知道从哪里开始。提前致谢!

扩展我的评论并提供一个更好的例子。这假设了游戏框架,但原理应该是完全一样的。

使用 Guice 时需要做的第一件事是将实现绑定到接口。例如我需要一个 FlagDAO.

public interface FlagDAO {
    public Flag getByName(String name) throws FlagNotFoundException;
}

现在我有几个它的实现,我不会展示完整的代码:

// This one is used in production/development
public class FlagDAOEbean implements FlagDAO {
    // Some code
}

// This one is used for testing
public class FlagDAOInMemory implements FlagDAO { {
    // Some code
}

在其他一些服务中使用FlagDAO。此 SomethingUsingFlagDAO class 也必须通过 Guice 构造,以便注入正确的值

public class SomethingUsingFlagDAO {
    @Inject
    public SomethingUsingFlagDAO(FlagDAO flagDAO) {
    }
}

现在播放框架使用模块在应用程序启动时绑定某些东西。我在测试中禁用了这个模块,但代码在开发和生产环境中是 运行。

public class DAOModule extends AbstractModule {
    public void configure() {
        bind(FlagDAO.class).to(FlagDAOEbean.class);
        bind(SomethingUsingFlagDAOInterface.class).to(SomethingUsingFlagDAO.class);
    }
}

我的测试用例中有这段代码。请注意,唯一实际不同的是 FlagDAO 实现绑定到 Guice:

public class FlagsTest extends WithApplication {

    private SomethingUsingFlagDAO something;

    @Before
    public void setUp() {
        Injector injector = this.provideApplication().injector();
        this.something = injector.instanceOf(SomethingUsingFlagDAO.class);
    }

    @Override
    protected Application provideApplication() {
        Application application = new GuiceApplicationBuilder()
                .in(new Environment(new File("./"), this.getClass().getClassLoader(), Mode.TEST))
                .bindings(bind(FlagDAO.class).to(FlagDAOInMemory.class))
                .bindings(bind(SomethingUsingFlagDAOInterface.class).to(SomethingUsingFlagDAO.class))
                .build();

        return application;
    }
}

因此,根据我们是否处于 development/production/testing 环境中,使用不同的 DAO 实现而不实际改变任何工作方式。没有 if (application.isTesting()) 检查他们不应该在的地方。


在您的情况下,您可以想象一个 FlagDAOOracleFlagDOAEbeanFlagDAOInMemory 一起绑定,如下所示:

public class DAOModule extends AbstractModule {
    public void configure() {
        if(weNeedOracle) {
           bind(FlagDAO.class).to(FlagDAOOracle.class);
        } else {
           bind(FlagDAO.class).to(FlagDAOMysql.class);
        }
    }
}