2016-10-28 6 views
1

@ServerEndpointクラスに@Repositoryクラスで注釈を挿入しようとしています。しかし、私はリポジトリメソッドを呼び出そうとしているときにはnullを返します。このパッケージのもう1つの注入されたBeanは正常に動作しています。@ServerEndpointとSpring MVC注入

@ApplicationScoped 
@ServerEndpoint("/WebsocketHome/actions") 
public class WebSocketServer { 
private static final Logger LOG = Logger.getLogger(WebSocketServer.class); 

@Inject 
private SessionHandler sessionHandler; 

@Inject 
private PlaceRepository placeRepository; 

@OnMessage 
public void handleMessage(String message, Session session) { 

    JSONParser jsonParser = new JSONParser(); 

    try { 
     JSONObject jsonObject = (JSONObject) jsonParser.parse(message); 

     if ("getRooms".equals(jsonObject.get("action"))) { 
      List<Place> places = this.placeRepository.getAllPlaces(); //error is here    
     } 
     } catch (ParseException e) { 
      LOG.error(e.getMessage()); 
     } 
..... 

これは、リポジトリクラスです:

@Repository 
@Transactional 
public class PlaceRepository { 

    @Autowired 
    private SessionFactory sessionFactory; 

    @SuppressWarnings("unchecked") 
    public List<Place> getAllPlaces() { 
     return this.sessionFactory.getCurrentSession().createQuery("from Place place").list(); 
    } 
} 

のweb.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://java.sun.com/xml/ns/javaee" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
     version="3.0"> 
    <display-name>Archetype Created Web Application</display-name> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/application-context.xml</param-value> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

</web-app> 

アプリ-のcontext.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:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:property-placeholder location="classpath:application.properties" system-properties-mode="ENVIRONMENT"/> 
    <context:component-scan base-package="com.hms.repository"/> 
    <context:component-scan base-package="com.hms.utils"/> 
    <tx:annotation-driven transaction-manager="txManager"/> 

    <import resource="security-context.xml"/> 

    <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${db.driverClassName}" /> 
     <property name="url" value="${db.url}" /> 
     <property name="username" value="${db.username}" /> 
     <property name="password" value="${db.password}" /> 
     <!-- 
     <property name="initialSize" value="5" /> 
     <property name="maxActive" value="10" /> 
     <property name="maxIdle" value="5" /> 
     <property name="minIdle" value="2" /> 
     --> 
    </bean> 

    <bean id="sessionFactory" 
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="configLocation" value="classpath:hibernate.cfg.xml"/> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${db.dialect}</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hbm2ddl.auto">${db.hbm2ddl.auto}</prop> 
      </props> 
     </property> 
    </bean> 
</beans> 

エラーのみの呼び出しに表示されます@OnMessageメソッド。 すべてjunit @リポジトリクラスのテスト結果がきれいです。 私のコードで何が間違っていますか? 私の英語を残念に思っています。

UPD:

それはそう、その問題は私が@Repositoryに追加することを、テストメソッドので、SessionFactoryの依存関係にあります。

public String testWS() { 
     return "Test is ok!"; 
    } 

戻り罰金結果。

+0

あなたの設定も提供できますか?最初の考えは、あなたのクラスでTransactional注釈を使用することに問題があることです。 @Transactionalスプリングを使用するとプロキシが提供され、CGLIBを使用していないと仮定すると、JDKのダイナミックプロキシはコンクリートPlaceRepositoryと互換性がありません –

+0

リクエストしたとおり、私は設定ファイルをインクルードします。 –

答えて

1

私は正しい方法を見つけたようです。私はSpring websocket documentationでそれを見つけました。

  1. 私はタイプレベルの注釈に入れる:

    @ServerEndpoint

  2. (値、コンフィギュレータ= SpringConfigurator.class "/ WebsocketHome /アクション" =) 私がやったいくつかのステップがあります。

    WebServiceクラスに@Serviceと@Controllerを追加し、app-context.xmlファイルの "context:component-scan"パスを使用して、Springが対応するBeanを見つけることができるようにしました。

  3. 私は私のpom.xmlに「春-のWebSocket」依存関係を追加しました(私はMavenを使用)

多分それは正しい方法ではありませんが、それは私のために正常に動作しています。

関連する問題