我想運行一個簡單的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