Как заставить аннотацию @Autowire работать с веб-службой Hessian? (Setter autowire работает нормально)

У меня есть весенний проект, использующий веб-сервис Hessian (userMgr). В моем клиенте (UserProfileHelperImpl) конфигурация spring работает с автопроводкой сеттера, но не с аннотацией @Autowire. Как я могу получить то же самое с @Autowire? Спасибо.

веб.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app
<?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" 
xmlns:task="http://www.springframework.org/schema/task"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:aop="http://www.springframework.org/schema/aop"
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
  http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
  http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"    
default-autowire="byName">
    <context:annotation-config /> 
    <context:spring-configured />
    <task:annotation-driven />
    <import resource="applicationContext-service.xml" />
    <import resource="applicationContext-business.xml" />
</beans>
5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
  default-autowire="byName">  
  <bean id="userMgr"
class="org.springframework.remoting.caucho.HessianProxyFactoryBean" scope="prototype">
    <property name="hessian2Request" value="true" />
    <property name="hessian2Reply" value="true" />
    <property name="serviceUrl" value='#{config.getFullUrl("userMgr")}' />
    <property name="serviceInterface" value="com.core.business.UserMgr" />
  </bean>
</beans>
0.xsd" id="WebApp_ID" version="3.0"> <display-name>web</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> </listener> </web-app>

applicationContext.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" 
xmlns:task="http://www.springframework.org/schema/task"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:aop="http://www.springframework.org/schema/aop"
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
  http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
  http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"    
default-autowire="byName">
    <context:annotation-config /> 
    <context:spring-configured />
    <task:annotation-driven />
    <import resource="applicationContext-service.xml" />
    <import resource="applicationContext-business.xml" />
</beans>

applicationContext-service.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
  default-autowire="byName">  
  <bean id="userMgr"
class="org.springframework.remoting.caucho.HessianProxyFactoryBean" scope="prototype">
    <property name="hessian2Request" value="true" />
    <property name="hessian2Reply" value="true" />
    <property name="serviceUrl" value='#{config.getFullUrl("userMgr")}' />
    <property name="serviceInterface" value="com.core.business.UserMgr" />
  </bean>
</beans>

applicationContext-business.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
  default-autowire="byName">
  <bean id="userProfileHelper" class="com.web.business.common.UserProfileHelperImpl" scope="prototype">
<!--      <property name="userMgr" ref="userMgr"/> -->
  </bean>
</beans>

UserProfileHelper.java

package com.web.business.common;

@Component
public interface UserProfileHelper {
  Profile getUserProfile(String userName);
}

UserProfileHelperImpl.java

package com.web.business.common;

public class UserProfileHelperImpl implements UserProfileHelper {
  @Autowired // this does not work
  private UserMgr userMgr;

  // this work
  /*public void setUserMgr(UserMgr userMgr) {
    this.userMgr = userMgr;
  }*/
}

UserMgr.java

package com.core.business;

public interface UserMgr {

  Profile getUserProfile(String userName);
}

person drakid    schedule 12.09.2015    source источник


Ответы (1)


Разве вы не должны писать AttributeMgr вместо UserMgr?

@Autowired
private AttributeMgr userMgr;
person user2004685    schedule 12.09.2015
comment
Кроме того, в <property name="serviceInterface" value="com.business.UserMgr" /> не должно быть com.core.business.UserMgr вместо com.business.UserMgr? Я не уверен, как Сеттер работает на вас. - person user2004685; 13.09.2015
comment
Я еще раз все пересмотрел и кое-где поправил. Поскольку каждый файл длинный и все одинаковые, я удаляю избыточный код. - person drakid; 13.09.2015
comment
Вы даете default-autowire="byName", а @Autowired используете "byType". "byName" всегда вводите зависимость с помощью установщика. Если вы хотите использовать аннотацию, не указывайте default-autowire="byName" или, возможно, используйте @Qualifier в дополнение к @Autowired. - person user2004685; 13.09.2015
comment
В applicationContext-service.xml и applicationContext-business.xml я удалил default-autowire="byName" и добавил явное autowire="byType" к каждому определению компонента. И я получаю эту ошибку: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.core.business.UserMgr] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}. Я также пытаюсь @Qualifier("userMgr"), но не работает, та же ошибка. - person drakid; 14.09.2015
comment
Если я не ошибаюсь, вы пробовали @Qualifier с default-autowire="byName", верно? Если нет, то вы должны сделать это в дополнение к default-autowire="byName". Кроме того, нет необходимости добавлять явный autowire="byType". Вы можете просто удалить default-autowire="byName", и это должно работать. - person user2004685; 14.09.2015
comment
Я сделал следующее: удалил сеттер, затем объединил некоторые случаи (1: сохранить @Autowire, не использовать @Qualifier, удалить default-autowire; 2: сохранить @Autowire и @Qualifier, просто удалить default-autowire), но все это не работает (ошибка, как указано выше). Дополнительная информация, я создаю объект следующим образом: ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); userProfileHelper = (UserProfileHelper) context.getBean("userProfileHelper");. Не знаю, почему сеттер работает, но @Autowire :)) - person drakid; 15.09.2015
comment
Вы пробовали это? Удалить сеттер, оставить @Autowired, оставить @Qualifier("usrMgr"), НЕ удалять default-autowire="byName"? - person user2004685; 15.09.2015
comment
Я только что попробовал. Но это тоже не работа (NoSuchBeanDefinitionException) - person drakid; 15.09.2015
comment
Я думаю, что это должна быть другая ошибка, потому что есть этот bean (userMgr) при использовании сеттера. - person drakid; 15.09.2015