【问题标题】:MongoDB IntelliJ: How to configure custom jar project from my war projectMongoDB IntelliJ:如何从我的战争项目中配置自定义 jar 项目
【发布时间】:2014-08-26 22:35:58
【问题描述】:

我使用 IntelliJ、Spring、Maven、Tomcat7 和 MongoDB

我有 2 个项目:

  • JAR:这应该是一个身份验证服务
  • WAR:这有我的身份验证服务作为依赖项

现在我的战争项目中有以下 bean 配置:

    <!-- Factory bean that creates the Mongo instance -->
    <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
        <property name="host" value="localhost" />
    </bean>

    <!-- MongoTemplate for connecting and quering the documents in the database -->
    <bean id="jwt" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongo" ref="mongo" />
        <constructor-arg name="databaseName" value="ProjectDB" />
    </bean>

    <!-- Use this post processor to translate any MongoExceptions
         thrown in @Repository annotated classes -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />


    <!-- ########################################################################### -->
    <!-- Configuration for auth -->
    <!-- ########################################################################### -->

    <!-- MongoTemplate for connecting and quering the documents in the database -->
    <bean id="auth" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongo" ref="mongo" />
        <constructor-arg name="databaseName" value="ProjectDB-auth" />
    </bean>

我的 WAR 项目应该使用 ProjectDB 并为 ProjectDB-auth 提供身份验证服务

  • 在我的 WAR 项目中注入 -> @Autowired private MongoTemplate jwt;
  • 在我的 JAR 项目中注入 -> @Autowired private MongoTemplate auth;

这很好用,但是我的 JAR 项目中有另一个 mongoDB 配置 XML 文件,它将被完全忽略(永远不会在我的应用程序上下文中导入)。

    <!-- Factory bean that creates the Mongo instance -->
    <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
        <property name="host" value="localhost" />
    </bean>

    <!-- MongoTemplate for connecting and quering the documents in the database -->
    <bean id="test" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongo" ref="mongo" />
        <constructor-arg name="databaseName" value="AuthDBBase-notInUse" />
    </bean>

    <!-- Use this post processor to translate any MongoExceptions
         thrown in @Repository annotated classes -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

我需要删除此文件,但 IntelliJ 无法解析 @Autowired private MongoTemplate auth; 的依赖关系

我认为我的配置有问题,尽管这可以正常工作。但是如何正确配置我的两个项目呢?

【问题讨论】:

  • “一切都错了”是什么意思?也许“没有错”?
  • 对不起我的失败,我的意思是我的配置有问题:P

标签: spring mongodb maven intellij-idea tomcat7


【解决方案1】:

我认为您的设置一切正常。

您想创建一个可供任何客户端使用的 JAR,在本例中是您的 WAR。您依赖于数据库。您可以通过省略 JAR 中的任何 Spring 配置来保持此依赖项未声明。这不干净。此外,您的 IDE 会抱怨,因为它无法检查您的项目的有效性。

最好声明该依赖项,这与 C 或 Maven 依赖项中的头文件非常相似,范围为 provided。像您一样将该 Spring 配置添加到项目中。告诉您的库的用户将该配置包含到他们自己的配置中,或者将该配置用作文档以查看哪些 bean 只需要存在才能使您的工件工作。

【讨论】:

    猜你喜欢
    • 2013-03-08
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2013-11-17
    相关资源
    最近更新 更多