在Grails 3.0.9中向src / groovy注入服务

时间:2015-11-20 08:46:03

标签: grails groovy grails-3.0.9

我正在尝试创建一个EndPoint文件。所以我创建了一个名为ServerEndPointDemo.groovy

的文件
package com.akiong

import grails.util.Environment
import grails.util.Holders

import javax.servlet.ServletContext
import javax.servlet.ServletContextEvent
import javax.servlet.ServletContextListener
import javax.servlet.annotation.WebListener
import javax.websocket.EndpointConfig
import javax.websocket.OnClose
import javax.websocket.OnError
import javax.websocket.OnMessage
import javax.websocket.OnOpen
import javax.websocket.Session
import javax.websocket.server.PathParam
import javax.websocket.server.ServerContainer
import javax.websocket.server.ServerEndpoint
import javax.websocket.EncodeException

import javax.servlet.annotation.WebListener
import java.util.concurrent.CopyOnWriteArraySet

@WebListener
@ServerEndpoint(value="/chat/test/{username}")

public class ServerEndPointDemo implements ServletContextListener {
    private static HashMap<String, String> usersMap = new HashMap<String, String>();
    private static final Set<ServerEndPointDemo> connections = new CopyOnWriteArraySet<>();
    private String username
    private Session session

    @OnOpen
    public void  handleOpen(Session session,@PathParam("username") String user){
        System.out.println("-------------------------------------");

        System.out.println(session.getId() + " has opened a connection");
        println "user = "+user

        connections.add(this);
        this.username   = user
        this.session    = session

        addUserToMap(username,session.getId())

        try{
            def ctx = Holders.applicationContext
            chatService = ctx.chatService
        }
        catch (Exception e){
            println "e = "+e
        }

    }

    @OnClose
    public void  handleClose(Session session){  
        System.out.println("Session " +session.getId()+" has ended");  
    }

    @OnMessage
    public String handleMessage(String message,Session session){
        chatService.saveSessionAdminJava(session.getId())    
    }

    @OnError
    public void  handleError(Throwable t){
        println "error ~"
    }  

    private void sendMessageToAll(String message){
        println "session.size() = "+sessions.size()
        for(Session s : sessions){
            try {
                s.getBasicRemote().sendText(message);
                println "sent to session = "+s.getId()
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
`

我尝试使用此代码从服务调用方法:

try {
    def ctx = Holders.applicationContext
    chatService = ctx.chatService
} catch (Exception e) {
    println "e = " + e
}

这是我的服务:

package com.akiong.services

import com.akiong.maintenance.Chat

class ChatService {

    def serviceMethod() {

    }

    def saveSessionAdminJava(def sessionJava) {
        println "jaln!!!"
        def chatInstance = Chat.findByIsAdmin("1")
        chatInstance.sessionJava = sessionJava
        chatInstance.save(failOnError: true)
    }
}

但在我尝试运行此代码后,我收到此错误:

Error : groovy.lang.MissingPropertyException: No such property: chatService for
class: com.akiong.ServerEndPointDemo

任何人都知道如何在src/groovy

中调用从服务到文件的方法

2 个答案:

答案 0 :(得分:1)

所以这就是问题所在。您编写的代码对于注入服务是正确的:

try {
    def ctx = Holders.applicationContext
    chatService = ctx.chatService
} catch (Exception e) {
    println "e = "+e
}

chatService未在您班级的任何地方定义。因此,即使调用了handleOpen方法,也必须抛出MissingPropertyException,因为您已经处理了顶级Exception类(永远不应该鼓励) <{1}}方法中的异常被抑制。

现在,稍后在您的handleOpen方法中,同样的问题再次发生handleMessage未定义,因此您获得了在问题中发布的异常。

所以,您现在知道答案:-)只需在chatService课程中定义chatService

<强>更新

ServerEndPointDemo

答案 1 :(得分:-1)

import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.grails.commons.ApplicationHolder as AH

class MyClass{
    private static final log = LogFactory.getLog(this)
    def ctx = AH.application.mainContext
    def authService=ctx.authService

    def method(){
        log.debug "into MyClass method"
    }
}