Java Spring引用来自外部项目的bean不起作用

Java Spring引用来自外部项目的bean不起作用,java,spring,spring-mvc,Java,Spring,Spring Mvc,我有两个项目(以A和B为例),其中B使用A中的类。尽管Maven对项目B的配置将项目A作为依赖项引入其中,但Spring在服务器启动期间抛出了一个异常: Severe: Context initialization failed org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.mycompany.projectB.NativeClass; nested e

我有两个项目(以A和B为例),其中B使用A中的类。尽管Maven对项目B的配置将项目A作为依赖项引入其中,但Spring在服务器启动期间抛出了一个异常:

Severe: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.mycompany.projectB.NativeClass; nested exception is java.io.FileNotFoundException: class path resource [com/mycompany/projectA/ForeignClass.class] cannot be opened because it does not exist
我曾试图从项目B中删除有问题的项目A类引用,但与项目A中的任何其他类都会出现相同的错误,这表明Spring并没有“看到”项目A bean(尽管Eclipse IDE在设计时没有指出任何java问题)

我已经在Project B的mvc-config.xml中输入了以下语句,但没有成功:

<import resource="classpath*:/WEB-INF/mvc-config.xml" />

此外,项目A正确地放置在项目B中Maven的依赖项下。 我做错了什么

编辑:mvc-config.xml

    <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--    "Nisto está o amor, não em que nós tenhamos amado a Deus, 
             mas em que ele nos amou a nós, 
             e enviou seu Filho para propiciação pelos nossos pecados". 1 João 4:10 -->

    <context:component-scan base-package="com.mycompany.myunit.myprojectb"/>

    <mvc:annotation-driven />
    <context:annotation-config/>
    <mvc:default-servlet-handler/>
    <!-- I'm just trying to import everything that could be important for beans definition in Spring -->
    <import resource="classpath*:/WEB-INF/mvc-config.xml" />
    <import resource="classpath*:/WEB-INF/spring-config.xml" />
    <import resource="classpath*:/WEB-INF/service-context.xml" />
    <import resource="classpath*:spring/application-config.xml" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp"/>
    </bean>

    <bean id="messageSource" class=
      "org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages/messages" />
    </bean>

</beans>

编辑2:添加类代码:

项目B的本机类:

package com.mycompany.myunit.myproject.dao.impl;

import java.util.List;

import javax.persistence.PersistenceException;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.mycompany.myunit.myproject.dao.LocalBranchDao;
import com.mycompany.dao.impl.BranchDaoImpl;
import com.mycompany.myunit.myproject.model.Branch;

@Repository
public class LocalBranchDaoImpl extends BranchDaoImpl implements LocalBranchDao {

    @Autowired
    SessionFactory sessionFactory;

    public LocalBranchDaoImpl(){
    }

    @Transactional(readOnly=true)
    @Deprecated
    public List<Branch> getAllBranches() {
      //... Data Access code
    }
}
package com.mycompany.myunit.myproject.dao.impl;
导入java.util.List;
导入javax.persistence.PersistenceException;
导入org.hibernate.SessionFactory;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Repository;
导入org.springframework.transaction.annotation.Transactional;
导入com.mycompany.myunit.myproject.dao.LocalBranchDao;
导入com.mycompany.dao.impl.BranchDaoImpl;
导入com.mycompany.myunit.myproject.model.Branch;
@存储库
公共类LocalBranchDaoImpl扩展BranchDaoImpl实现LocalBranchDao{
@自动连线
会话工厂会话工厂;
公共LocalBranchDaoImpl(){
}
@事务(只读=真)
@不赞成
公共列表getAllBranchs(){
//…数据访问代码
}
}
项目B的本机接口:

package com.mycompany.myunit.myproject.dao;

import java.util.List;
import com.mycompany.dao.BranchDao;
import com.mycompany.model.Branch;

public interface LocalBranchDao extends BranchDao {
    public List<Branch> getAllBranches();
}
package com.mycompany.dao;

import java.util.Collection;

import com.mycompany.model.Branch;

public interface BranchDao {
    public Collection<Branch> findAll();
    public Branch findById(long id);
    public void delete(Branch branch);
    public void create(Branch branch);
    public Branch update(Branch branch);
}
package com.mycompany.myunit.myproject.dao;
导入java.util.List;
导入com.mycompany.dao.BranchDao;
导入com.mycompany.model.Branch;
公共接口LocalBranchDao扩展了BranchDao{
公共列表getAllBranchs();
}
项目A的外部接口:

package com.mycompany.myunit.myproject.dao;

import java.util.List;
import com.mycompany.dao.BranchDao;
import com.mycompany.model.Branch;

public interface LocalBranchDao extends BranchDao {
    public List<Branch> getAllBranches();
}
package com.mycompany.dao;

import java.util.Collection;

import com.mycompany.model.Branch;

public interface BranchDao {
    public Collection<Branch> findAll();
    public Branch findById(long id);
    public void delete(Branch branch);
    public void create(Branch branch);
    public Branch update(Branch branch);
}
package com.mycompany.dao;
导入java.util.Collection;
导入com.mycompany.model.Branch;
公共界面部{
公共集合findAll();
公共分支findById(长id);
公共作废删除(分行);
公共作废创建(分行);
公共分行更新(分行);
}
项目A的外语课:

package com.mycompany.dao.impl;

import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.mycompany.dao.BranchDao;
import com.mycompany.model.Branch;

@Repository
public class BranchDaoImpl implements BranchDao {

    private static final Log log = LogFactory.getLog(BranchDaoImpl.class);

    @Autowired
    SessionFactory sessionFactory;

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Transactional(readOnly=true)
    public Collection<Branch> findAll() {
        //...Data Acess
    }

    @Transactional(readOnly=true)
    public Branch findById(long id) {
       //... Data Access
    }

    @Transactional
    public void delete(Branch branch) {
      //... Data Access
    }

    @Transactional
    public void create(Branch branch) {
     //... Data Access
    }

    @Transactional
    public Branch update(Branch branch) {
     //... Data Access
    }

}
package com.mycompany.dao.impl;
导入java.util.Collection;
导入java.util.List;
导入org.apache.commons.logging.Log;
导入org.apache.commons.logging.LogFactory;
导入org.hibernate.SessionFactory;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Repository;
导入org.springframework.transaction.annotation.Transactional;
导入com.mycompany.dao.BranchDao;
导入com.mycompany.model.Branch;
@存储库
公共类BranchDaoImpl实现BranchDao{
私有静态最终日志Log=LogFactory.getLog(branchdaimpl.class);
@自动连线
会话工厂会话工厂;
@SuppressWarnings({“unchecked”,“rawtypes”})
@事务(只读=真)
公共集合findAll(){
//…数据访问
}
@事务(只读=真)
公共分支findById(长id){
//…数据访问
}
@交易的
公共作废删除(分行){
//…数据访问
}
@交易的
公共作废创建(分支){
//…数据访问
}
@交易的
公共分行更新(分行){
//…数据访问
}
}

您是否控制了maven在项目B的类路径中正确复制项目A的jar(
/WEB-INF/libs
用于war或
/libs
用于jar)?显示尝试访问该类的配置。异常类型是奇数的。这是在eclipse中发生的吗?我在Web服务器启动时有时会遇到这种情况。一个完整的maven构建(干净的安装)通常可以解决这个问题。@MartinFrey,我在两个项目(A和B)中都进行了干净的安装,但没有成功。@SergeBallesta,在Tomcat的webapps文件夹中,项目B正在运行与/WEB-INF/lib中的项目A相关的jar文件。我认为这是对的,不是吗?