Grails 在HttpSessionListener中使用service

在grails中使用HttpSessionListener
需要先执行
grails install-templates 

然后 在 \src\templates\war\web.xml 中增加 listener

	<listener>
		<listener-class>a1.MyListener</listener-class>
	</listener>

\grails-app\services\a1\MyService.groovy

package a1

class MyService {

    def serviceMethod() {
		log.error " in ${this.class}"
    }
}

\src\java\a1\MyListener.java

package a1;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.codehaus.groovy.grails.commons.ApplicationHolder;

@SuppressWarnings("deprecation")
public class MyListener implements HttpSessionListener {

	MyService myService = null;
	Object oo = new Book();

	public void sessionCreated(HttpSessionEvent arg0) {
		init();
		System.err.println("sessionCreated " + arg0.getSession().getId());
		myService.serviceMethod();
	}

	public void sessionDestroyed(HttpSessionEvent arg0) {
		init();
		System.err.println("sessionDestroyed " + arg0.getSession().getId());
		myService.serviceMethod();
	}

	synchronized public void init() {
		if (myService == null) {
			myService = (MyService) ApplicationHolder.getApplication()
					.getMainContext().getBean("myService");
		}
	}

}



你可能感兴趣的:(Grails 在HttpSessionListener中使用service)