如何将服务自动装配为构造函数参数?
How to autowire service as constructor argument?
我有以下服务:
public interface LogoutService {
void logoutAllUsers();
void dropUserSession(SessionInformation session);
}
及以下实施:
@Service
public class LogoutServiceImpl implements LogoutService {
...
public class MyXmlBean extends ConcurrentSessionControlAuthenticationStrategy {
....
public MyXmlBean (MySessionRegistry sessionRegistry,
LogoutService logoutService) {
....
和以下 xml 配置:
<bean id="MyXmlBean" class="package.MyXmlBean">
<constructor-arg name="sessionRegistry" ref="mySessionRegistry" />
<constructor-arg name="logoutService" ref="logoutService"/>
<property name="maximumSessions" value="1" />
</bean>
当我启动应用程序时 - 我看到以下错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myXmlBean ' defined in class path resource [context-security.xml]: Cannot resolve reference to bean 'myXmlBean ' while setting constructor argument with key [1]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyXmlBean' defined in class path resource [context-security.xml]: Cannot resolve reference to bean 'logoutService' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'logoutService' is defined
如何解决?
据我所知
@Service
注释是从错误的包中导入的(非 spring)
我有以下服务:
public interface LogoutService {
void logoutAllUsers();
void dropUserSession(SessionInformation session);
}
及以下实施:
@Service
public class LogoutServiceImpl implements LogoutService {
...
public class MyXmlBean extends ConcurrentSessionControlAuthenticationStrategy {
....
public MyXmlBean (MySessionRegistry sessionRegistry,
LogoutService logoutService) {
....
和以下 xml 配置:
<bean id="MyXmlBean" class="package.MyXmlBean">
<constructor-arg name="sessionRegistry" ref="mySessionRegistry" />
<constructor-arg name="logoutService" ref="logoutService"/>
<property name="maximumSessions" value="1" />
</bean>
当我启动应用程序时 - 我看到以下错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myXmlBean ' defined in class path resource [context-security.xml]: Cannot resolve reference to bean 'myXmlBean ' while setting constructor argument with key [1]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyXmlBean' defined in class path resource [context-security.xml]: Cannot resolve reference to bean 'logoutService' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'logoutService' is defined
如何解决?
据我所知
@Service
注释是从错误的包中导入的(非 spring)