關(guān)于org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘studenta’ available的解決辦法
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'studenta' available
這個(gè)報(bào)錯(cuò)可能是因?yàn)椋?/p>
1. spring的xml配置文件Bean中的id和getBean的id不一致
spring的配置文件中:
而程序中
applicationContext.getBean(“studenta”, Student.class)中的是studenta而spring配置文件的id是student,不一致。
2. 是否是忘記加注解了
@Service
public class XxxServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
}
@Resource或@Autowired都可以(@Resource是jdk自帶的)
3.如果添加了注解但是還是報(bào)這個(gè)錯(cuò)
例如:
配置文件:
啟動(dòng):
這樣寫會報(bào)錯(cuò):
Exception in thread “main” org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘strudentImp’ available
這是因?yàn)闆]有掃描要注入的StrudentImp類所在的包(com.cn.domain和com.cn.service)。所以在spring配置文件應(yīng)該做以下修改
也就是掃描要注入的類的包。
4. 包結(jié)構(gòu)如下
文章來源:http://www.zghlxwxcb.cn/news/detail-638282.html
為什么會報(bào)錯(cuò)org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘studenta’ available
beanDefinitionMap是一個(gè)map儲存的是spring配置文件中聲明的對象,對象可以通過Bean的id作為key來獲取。如果獲取的為空就拋出異常。
spring默認(rèn)是單例模式,創(chuàng)建的對象儲存在:
通過spring.xml配置寫的id中寫的作為key或 如果是通過注解注入,那么key就是類名首字母小寫作為key
下面這個(gè)圖片就是通過注解注入,key就是類名首字母小寫。從而獲取到的內(nèi)容。文章來源地址http://www.zghlxwxcb.cn/news/detail-638282.html
到了這里,關(guān)于關(guān)于org.springframework.beans.factory.NoSuchBeanDefinitionException的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!