【问题标题】:Spring context:component-scan not working with annotated beansSpring上下文:组件扫描不适用于带注释的bean
【发布时间】:2014-12-26 16:36:38
【问题描述】:

我正在尝试运行一个简单的 Spring + Hibernate 教程 => Maven Spring Hibernate annotation example

我的bean定义文件BeanLocations.xml是这样的:

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

        <!-- Database Configuration -->
        <import resource="../database/Datasource.xml"/>
        <import resource="../database/Hibernate.xml" />

        <context:component-scan base-package="com.sample.springhibernate"/>

</beans>

我的主要方法:

public static void main( String[] args )
    {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath*:BeanLocations.xml");
        StockBo stockBo = (StockBo)appContext.getBean("stockBo");
    }

我有一个带有接口的已定义服务:

public interface StockBo {

        public void save(Stock stock);
        public void update(Stock stock);
        public void delete(Stock stock);
        public Stock findByStockCode(String stockCode);
}

以及他的实现:

 @Service("stockBo")
public class StockBoImpl implements StockBo {

    @Autowired
    StockDao stockDao;

    public void setStockDao(StockDao stockDao){
        this.stockDao = stockDao;
    }

    @Override
    public void save(Stock stock) {
        stockDao.save(stock);
    }
........

这有任何问题,因为 Spring 在 StockBo)appContext.getBean("stockBo") 时抛出异常:

30-oct-2014 15:31:49 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@64dc11: display name [org.springframework.context.support.ClassPathXmlApplicationContext@64dc11]; startup date [Thu Oct 30 15:31:49 CET 2014]; root of context hierarchy
30-oct-2014 15:31:49 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@64dc11]: org.springframework.beans.factory.support.DefaultListableBeanFactory@a3d4cf
30-oct-2014 15:31:49 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@a3d4cf: defining beans []; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'stockBo' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)

Spring 没有找到我带注释的服务 StockBoo(使用 @Service("stockBo") )....

有什么问题?如何确保 Spring 通过组件扫描识别我的服务?

仅供参考:StockBo 在 com.sample.springhibernate.bo 中,StockBoImpl 在 com.sample.springhibernate.bo.impl 中

【问题讨论】:

  • 您的BeanLocations.xml 文件在哪里?
  • 看起来 spring 没有找到 BeanLocations.xml。你可以尝试直接在那里声明一个bean来确认。

标签: spring hibernate maven


【解决方案1】:

如果您将日志级别设置为 DEBUG,您很可能会找到类似的行

DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 0 bean definitions from location pattern [classpath*:BeanLocations.xml]

也就是说,您的ClassPathXmlApplicationContext 没有找到任何与classpath*:BeanLocations.xml 匹配的资源,因此没有加载任何上下文。

您需要提供一个资源位置String 值,以正确识别和定位上下文配置文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-17
    • 2012-09-03
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多