如何使用Hessian Web服务获得@Autowire注释? (Setter autowire工作正常)

时间:2015-09-12 07:57:57

标签: java spring hessian

我有春季项目使用Hessian Web服务(userMgr)。在我的客户端(UserProfileHelperImpl)中,spring配置使用setter自动装配,但不能使用@Autowire注释。我怎么能用@Autowire得到同样的东西?感谢。

的web.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_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_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);
}

1 个答案:

答案 0 :(得分:0)

你不应该用AttributeMgr来代替UserMgr吗?

@Autowired
private AttributeMgr userMgr;