Issue
I am trying to inject annotated by @Repository class in @ServerEndpoint class. But when i am trying to call repository method it is return null. Another injected bean from this package is working fine.
@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());
}
.....
This is repository class:
@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>
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>
Error appears only on calling of @OnMessage method. All junit @Tests of repository class returns fine results. What is wrong in my code? Sorry for my english.
UPD:
It seems, that problem is in SessionFactory dependency because test method, that i add in @Repository:
public String testWS() {
return "Test is ok!";
}
returns fine result.
Solution
So, looks like i found the right way. I found it in Spring websocket documentation. There are some steps what i did:
I put in type-level annotation:
@ServerEndpoint(value = "/WebsocketHome/actions", configurator = SpringConfigurator.class)
I added @Service and @Controller to my websocket classes and "context:component-scan" path to my app-context.xml file so that Spring can find corresponding beans.
I added the "spring-websocket" dependency in my pom.xml (i use maven)
Maybe it is not the right way, but it is works fine for me.
Answered By - Andrey K
Answer Checked By - David Goodson (JavaFixing Volunteer)