zyro23 / grails-spring-websocket

93 stars 28 forks source link

brokerMessagingTemplate is null when used in /src/main/groovy/ #28

Closed watta90 closed 8 years ago

watta90 commented 8 years ago

Hi,

I would like to use brokerMessagingTemplate in a class that I have in src/main/groovy, i.e. not in grails-app, but I get that the template is null.

This is how I'm trying to use it:

@Singleton
class TestManager {

    @Autowired
    private SimpMessagingTemplate brokerMessagingTemplate;

    void init(){
        brokerMessagingTemplate.convertAndSend "/topic/test", "hello"
    }
}

Any idea of what is wrong? Thanks in advance

zyro23 commented 8 years ago

is that TestManager defined as a spring bean? if not, no autowiring will take place.

watta90 commented 8 years ago

Thanks for the really quick reply!

No I haven't declared it in grails-app/conf/spring/resources.groovy but will it not be considered as a spring object when I use the annotation @Singleton?

I did put this in resources.groovy

beans = {
    myBean TestManager
}

But it fails in run time with this message Failed to instantiate [test.TestManager]: Constructor threw exception; nested exception is java.lang.RuntimeException: Can't instantiate singleton test.TestManager. Use test.TestManager.instance

zyro23 commented 8 years ago

this seems unrelated to the websocket plugin but is rather essential spring. just remove the @Singleton which prevents spring from instatiating that class. (defining a spring bean will make it a singleton in the application context by default and if you then access that bean by autowiring it into other beans you will always get the same instance)

watta90 commented 8 years ago

Thanks man! You are right, it became unrelated to your plugin but thanks for clarifying how spring works! Have a nice day/evening/night!