Spring引用来自外部项目的bean无法正常工作

时间:2014-09-19 19:34:08

标签: 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 exception is java.io.FileNotFoundException: class path resource [com/mycompany/projectA/ForeignClass.class] cannot be opened because it does not exist

我试图从项目B中删除有问题的Project A类引用,但是同样的错误与项目A中的任何其他类一起出现,表明Spring不是"看到"项目A bean(虽然Eclipse IDE在设计时没有表明任何java问题)。

我已经在项目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
    }
}

Project 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();
}

项目A的外部接口:

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);
}

项目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
    }

}

0 个答案:

没有答案