我想运行一个简单的Spring + Hibernate的教程=>Maven Spring Hibernate annotation exampleSpring上下文:组件扫描不带注解的bean工作
我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);
}
........
有任何这方面的问题,因为春天抛出一个异常时StockBo)appContext.getBean( “stockBo”):不是foud
30-oct-2014 15:31:49 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org[email protected]64dc11: display name [org[email protected]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[email protected]64dc11]: org.s[email protected]a3d4cf
30-oct-2014 15:31:49 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.s[email protected]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)
春天我的注释服务StockBoo(带@Service(“stockBo”))....
什么问题?我如何确保Spring使用组件扫描来识别我的服务?
FYI:StockBo是在com.sample.springhibernate.bo.impl
您的'BeanLocations.xml'文件位于何处? – 2014-10-30 15:16:47
看起来像春天没有找到'BeanLocations.xml'。你可以尝试直接在那里声明一个bean来确认。 – 2014-10-30 15:19:24