doenst work Autowired

时间:2018-05-24 11:11:42

标签: spring spring-boot

@Autowired (socket_mesajService)我的SocketHandler.class工作NullPointerException我得到了@Autowired (socket_mesajService)@Component public class SocketHandler extends TextWebSocketHandler { @Autowired private Socket_mesajService socket_mesajService; List<Socket_session_model> sessions = new ArrayList<>(); @Override public void handleTextMessage(WebSocketSession session, TextMessage message) throws InterruptedException, IOException { for(Socket_session_model socket_session__model : sessions) { socket_mesajService.Socket_mesaj_templateAll(socket_session__model.getUserid()); } } } 正在为其他班级工作。

这是我的SocketHandler类,并在此类中调用socket_mesajService

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(new SocketHandler(), "/name");

 }

这是我的WebSocketConfig类

@Configuration
@EnableAutoConfiguration
@SpringBootApplication   (scanBasePackages={"com.polipool.*"})
@EntityScan( basePackages = {"com.polipool.model"})
public class ApplicationRun extends SpringBootServletInitializer {

  public static void main(String[] args) throws Exception {
        SpringApplication.run(ApplicationRun.class, args);
    }

  @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        FacesServlet servlet = new FacesServlet();
        return new ServletRegistrationBean(servlet, "*.xhtml");
    }

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder 
  builder) {
      return builder.sources(ApplicationRun.class);
   }

} 

这是我的ApplicationRun课程//////////////////////

@Service
public class Socket_mesajServiceImpl implements Socket_mesajService {
@Autowired
private Socket_mesajDao socket_mesajDao;
@Override
public List<Socket_mesaj_template> Socket_mesaj_templateAll(String 
messageid) {
    return socket_mesajDao.Socket_mesaj_templateAll(messageid);
}

}

Socket_mesajServiceImpl类

public interface Socket_mesajService {
public void Socket_mesaj_tamplateadd (Socket_mesaj_template mesaj);
public void Socket_mesaj_templateupdate (Socket_mesaj_template user);
public void Socket_mesaj_templatedelete (String messageid);
public List<Socket_mesaj_template> Socket_mesaj_templateAll (String userid);
}

Socket_mesajService

$('.city-input').val('İstanbul').trigger('input');

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

registry.addHandler(new SocketHandler(), "/name");

您自己创建了SocketHandler的实例,因此其中的属性将为null

相反,@Autowired SocketHandlerWebSocketConfig配置类,并将该实例添加到“registry”将解决您的问题