0

My apache application sit inside a Spring context.

<bean id="ghChatServer" class="io.gamehammer.service.ghchat.server.GhChatServer"
        init-method="init" destroy-method="destroy">
        <property name="port" value="5222" />
    </bean>

And the handler also inside the container.

<bean id="eventHandler" class="io.gamehammer.service.ghchat.handler.EventHandler"></bean>

Where the sessionCreate, it will create a new worker and put it inside a HashMap. Remove it when it disconnect.

@Override
    public void sessionCreated(IoSession session) throws Exception {
        sessionWorker = new SessionWorker();
        sessionHandler.init(session);
        cpntext.put(session, sessionHandler);
        log.info("Current connected session : " + handlers.size());
    }

I can inject bean in to my handler. But I want to inject service into my Worker too. How can I dynamic create a bean when sessionCreated in my handler is call?

Is it possible to implement my own scope like HttpSession scope by default?

Jeff Lee
  • 35
  • 7

1 Answers1

0

I miss this before. Need some code to make this.

String beanName = "sessionHandler-" + session.toString();
AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(SessionHandler.class);
registry.registerBeanDefinition(beanName, beanDefinition);
factory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
SessionHandler sessionHandler = (SessionHandler) applicationContext.getBean(beanName);
sessionHandler.init(session);

After this, I can autowired the service inside Worker.

Jeff Lee
  • 35
  • 7