添加此作爲在每個Spring文件
Spring提供自動檢測「定型」類和註冊用在ApplicationContext對應的BeanDefinitions的能力。要自動檢測這些類並註冊相應的bean,需要在XML中包含以下元素,其中'basePackage'將是這兩個類的公共父包(或者也可以指定包含每個父包的逗號分隔列表類)。
因此您的應用程序上下文應該是這個樣子:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.example"/>
</beans>
,使你的包獲得通過掃描你component-scan
應該相應註釋註釋Java類中按要求。像@Controllers
或@Service
等 查看詳細信息here。
分享您的應用程序的context.xml –
只是檢查是否已正確提到的控制器或服務的完整包路徑。 – agpt
問題可能是你已經配置了註解驅動的上下文配置,但沒有指定任何'component-scan'路徑 –