【问题标题】:Where to put the activejdbc.properties file for referencing the database.properties file in ActiveJDBC?在 ActiveJDBC 中引用 database.properties 文件的 activejdbc.properties 文件放在哪里?
【发布时间】:2018-02-08 17:13:49
【问题描述】:

我从 NetBeans 上的 Maven 原型 jersey-quickstart-webapp 创建了一个 Jersey 项目。我正在使用 ActiveJDBC 进行数据持久性。在其文档中,可以参考数据库连接属性的外部文件:http://javalite.io/database_connection_management#location-of-property-file

我已按照说明进行操作,并且我有这个 activejdbc.properties(位于 project-ws/src/main/)文件内容:

env.connections.file=C:\dev\database.properties

还有database.properties 文件:

development.driver=com.mysql.jdbc.Driver
development.username=user_db
development.password=*****
development.url=jdbc:mysql://192.168.0.19/customersdb

test.driver=com.mysql.jdbc.Driver
test.username=user_db
test.password=*****
test.url=jdbc:mysql://192.168.0.19/customersdb

production.jndi=java:comp/env/jdbc/acme

另外,我有这个网络服务:

@Path("telephone")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class TelephoneResource {
    @GET
    public Response get() {
        try {
            Base.open();

            LazyList<Telephone> tels= Telephone.findAll();

            String telsJson = tels.toJson(true);

            Base.close();

            return Response.ok().entity(telsJson).build();
        } catch (Exception ex) {
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\": \"" + ex.getMessage() + "\"}").build();
        }
    }
}

但是当我尝试执行该 GET 资源时,我收到以下错误消息:

{
    "error": "Could not find configuration in a property file for environment: development. Are you sure you have a database.properties file configured?"
}

我也尝试过其他配置:

通过使用maven-surefire-plugin 将它们添加到pom.xml 文件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.13</version>
    <configuration>
        <systemPropertyVariables>
            <activejdbc.url>jdbc:mysql://192.168.0.19/customersdb</activejdbc.url>
            <activejdbc.user>user_db</activejdbc.user>
            <activejdbc.password>*****</activejdbc.password>
            <activejdbc.driver>com.mysql.jdbc.Driver</activejdbc.driver>
        </systemPropertyVariables>
        <environmentVariables>
            <ACTIVEJDBC.URL>jdbc:mysql://192.168.0.19/customersdb</ACTIVEJDBC.URL>
            <ACTIVEJDBC.USER>user_db</ACTIVEJDBC.USER>
            <ACTIVEJDBC.PASSWORD>*****</ACTIVEJDBC.PASSWORD>
            <ACTIVEJDBC.DRIVER>com.mysql.jdbc.Driver</ACTIVEJDBC.DRIVER>
        </environmentVariables>
    </configuration>
</plugin>

但他们也没有工作。

【问题讨论】:

    标签: java maven activejdbc


    【解决方案1】:

    本页: http://javalite.io/database_connection_management#location-of-property-file 状态:

    将文件activejdbc.properties添加到您的项目的类路径根目录并在其中配置一个属性:

    这意味着文件需要位于类路径的根目录。 由于您使用 Maven,这意味着:

    src/main/resources/database.properties
    

    看看这个应用程序,完成测试:https://github.com/javalite/simple-example

    此外,在控制器内部打开连接是一种非常糟糕的方法。由于您没有使用ActiveWeb 哪个manages connections automatically,您可以编写一个类似于:ActiveJdbcFilter 的简单Servlet 过滤器,并从您的资源中获取Base.open()/Base.close() 代码:

    public class TelephoneResource {
        @GET
        public Response get() {
            try {
                return Response.ok().entity(Telephone.findAll().toJson(true)).build();
            } catch (Exception ex) {
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\": \"" + ex.getMessage() + "\"}").build();
            }
        }
    }
    

    如果您想探索 ActiveWeb,这里有一个简单的 Rest Webapp 示例:https://github.com/javalite/activeweb-rest

    【讨论】:

    • 嗨@ipolevoy。我已将database.propertiesactivejdbc.properties 文件都放在src/main/resources/ 中,最后一个包含此内容的文件:env.connections.file=D:\proyect-ws\src\main\resources\activejdbc.properties 在您的示例中(github.com/javalite/simple-example)我找不到activejdbc.properties 文件。
    • @InfZero,该示例没有文件activejdbc.properties,您的项目中通常没有该文件。您需要的文件是database.properties
    • @InfZero,你不需要弄乱env.connections.file 或环境变量。只需将文件 database.properties 放在你的类路径中,这就是你所需要的。
    • @InfZero,最近添加了新功能:javalite.io/… 自 2.1-SNAPSHOT
    猜你喜欢
    • 1970-01-01
    • 2014-02-10
    • 2018-06-05
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 2016-05-28
    • 1970-01-01
    相关资源
    最近更新 更多