Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jakarta ee 获取Vaadin项目中的daoFactory实例_Jakarta Ee_Instance_Vaadin_Dao - Fatal编程技术网

Jakarta ee 获取Vaadin项目中的daoFactory实例

Jakarta ee 获取Vaadin项目中的daoFactory实例,jakarta-ee,instance,vaadin,dao,Jakarta Ee,Instance,Vaadin,Dao,我第一次使用Vaadinproject时遇到了一些问题。 我用这个做了一些数据分层,但现在的问题是我想在应用程序开始时调用DAOFTORY实例一次,但我不知道如何做,因为Vaadin体系结构与普通的J2ee非常不同,所以我不能使用servletContextListener。如果有人有主意,那就太好了 使用的工厂: public abstract class DAOFactory { // Constants --------------------------------------

我第一次使用
Vaadin
project时遇到了一些问题。 我用这个做了一些
数据分层
,但现在的问题是我想在应用程序开始时调用
DAOFTORY
实例一次,但我不知道如何做,因为
Vaadin
体系结构与普通的
J2ee
非常不同,所以我不能使用
servletContextListener
。如果有人有主意,那就太好了

使用的工厂:

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.

}

使用Vaadin时,可以通过以下方式初始化工厂:

(在主应用程序类中)

主UI实例的init方法在每个应用程序实例上运行一次,可能是您想要的。 vaadin应用程序、实例、会话等的生命周期。
如果需要更多帮助,请返回。

很抱歉,复制/粘贴中缺少一部分。我现在已经更正了代码。它位于vaadin应用程序的主UI类中。我不想使用JPA容器,但我已经使用过了。我用代码编辑了我的问题。对不起,我现在更改了普通Dao工厂的示例,而不是JPA(但它在同一个位置),我在那里创建了我的实例。我如何从其他类访问它?它应该是静态的吗?
public MyVaadinUI() extends UI 
{
    @Override
    protected void init(VaadinRequest request) 
    {
        // create your Dao instance here
    }
}