我第一次使用Vaadin
项目而且我遇到了麻烦。
我使用此link做了一些data layering
但现在我想在应用程序开始时调用daoFactory
实例一次,但我不知道该怎么做由于Vaadin
架构与普通版J2ee
完全不同,因此我无法使用servletContextListener
。如果有人有想法,那就太棒了。
使用DAOFactory:
public abstract class DAOFactory {
// Constants ----------------------------------------------------------------------------------
private static final String PROPERTY_URL = "url";
private static final String PROPERTY_DRIVER = "driver";
private static final String PROPERTY_USERNAME = "username";
private static final String PROPERTY_PASSWORD = "password";
// Actions ------------------------------------------------------------------------------------
/**
* Returns a new DAOFactory instance for the given database name.
* @param name The database name to return a new DAOFactory instance for.
* @return A new DAOFactory instance for the given database name.
* @throws DAOConfigurationException If the database name is null, or if the properties file is
* missing in the classpath or cannot be loaded, or if a required property is missing in the
* properties file, or if either the driver cannot be loaded or the datasource cannot be found.
*/
public static DAOFactory getInstance(String name) throws DAOConfigurationException {
if (name == null) {
throw new DAOConfigurationException("Database name is null.");
}
DAOProperties properties = new DAOProperties(name);
String url = properties.getProperty(PROPERTY_URL, true);
String driverClassName = properties.getProperty(PROPERTY_DRIVER, false);
String password = properties.getProperty(PROPERTY_PASSWORD, false);
String username = properties.getProperty(PROPERTY_USERNAME, password != null);
DAOFactory instance;
// If driver is specified, then load it to let it register itself with DriverManager.
if (driverClassName != null) {
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
throw new DAOConfigurationException(
"Driver class '" + driverClassName + "' is missing in classpath.", e);
}
instance = new DriverManagerDAOFactory(url, username, password);
}
// Else assume URL as DataSource URL and lookup it in the JNDI.
else {
DataSource dataSource;
try {
dataSource = (DataSource) new InitialContext().lookup(url);
} catch (NamingException e) {
throw new DAOConfigurationException(
"DataSource '" + url + "' is missing in JNDI.", e);
}
if (username != null) {
instance = new DataSourceWithLoginDAOFactory(dataSource, username, password);
} else {
instance = new DataSourceDAOFactory(dataSource);
}
}
return instance;
}
/**
* Returns a connection to the database. Package private so that it can be used inside the DAO
* package only.
* @return A connection to the database.
* @throws SQLException If acquiring the connection fails.
*/
abstract Connection getConnection() throws SQLException;
// DAO implementation getters -----------------------------------------------------------------
/**
* Returns the User DAO associated with the current DAOFactory.
* @return The User DAO associated with the current DAOFactory.
*/
public UserDAO getUserDAO() {
return new UserDAOJDBC(this);
}
// You can add more DAO implementation getters here.
}
答案 0 :(得分:0)
使用Vaadin时,您可以通过以下方式初始化工厂:
(它在主要的应用程序类中)
public MyVaadinUI() extends UI
{
@Override
protected void init(VaadinRequest request)
{
// create your Dao instance here
}
}
主UI实例的init方法每个应用程序实例运行一次,可能就是你想要的。 vaadin应用程序,实例,会话等的生命周期is described here。 如果你需要更多的帮助,请回来。