Spring Docによると
Spring は、「ステレオタイプ化された」クラスを自動的に検出し、対応する BeanDefinitions を ApplicationContext に登録する機能を提供します。これらのクラスを自動検出し、対応する Bean を登録するには、XML に次の要素を含める必要があります。ここで、「basePackage」は 2 つのクラスの共通の親パッケージになります (または、それぞれの親パッケージを含むカンマ区切りのリストを指定できます)。クラス)。
したがって、アプリケーション コンテキストは次のようになります。
<?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 クラスに注釈を付ける必要があります。Like@Controllers
または@Service
etc. 詳細はこちらをご覧ください。