Spring MVCを使用して、私はこのControllerクラスを持っています:
@Controller
public class LoginController {
@Autowired
private LoginService loginService;
// more code and a setter to loginService
}
LoginService
はインターフェースであり、その唯一の実装はLoginServiceImpl
次のとおりです。
public class LoginServiceImpl implements LoginService {
//
}
私のproject-name-servlet.xml
ファイルはこれです:
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="loginService" class="my.example.service.impl.LoginServiceImpl" />
<bean class="my.example.controller.LoginController" />
</beans>
問題は、loginService
コントローラーに注入されていないことです。以下のようにXMLファイルでインジェクションを指定すると、次のように機能します。
<bean id="loginService"
class="may.example.service.impl.LoginServiceImpl" />
<bean class="my.example.controller.LoginController">
<property name="loginService" ref="loginService"></property>
</bean>
なぜこれが起こるのですか?インターフェイスタイプのインジェクションポイントにインジェクションされるのは、実装されているBeanだけではありませんか?