package com.ghca.config;
import *
/**
* Created by JialeLi on 6/9/2014.
* Spring的配置
*/
@Configuration
@ComponentScan("com.ghca")
@EnableWebMvc
@PropertySource("classpath:config.properties")
public class SpringConfig {
@Value("${selfService.xmlEncoding}")
private String xmlCharSet;
@Value("${selfService.port}")
private int selfServicePort;
@Value("${selfService.idleTime}")
private int selfServiceIdleTime;
/**
* 配合@PropertySource 注解 处理properties文件
*/
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
@Bean
@Autowired
@Qualifier("SelfServiceHandler")
public IoAcceptor minaInitial(IoHandler handler) throws IOException {
IoAcceptor acceptor = new NioSocketAcceptor();
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
chain.addLast("logger", new LoggingFilter());
chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName(xmlCharSet))));
acceptor.setHandler(handler);
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, selfServiceIdleTime);
acceptor.bind(new InetSocketAddress(selfServicePort));
return acceptor;
}
}
在tomcat关闭时会抛出如下警告:
^C19-May-2015 13:57:28.036 INFO [Thread-5] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-80"]
19-May-2015 13:57:28.091 INFO [Thread-5] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"]
19-May-2015 13:57:28.142 INFO [Thread-5] org.apache.catalina.core.StandardService.stopInternal Stopping service Catalina
13:57:28 [localhost-startStop-1][INFO ] org.springframework.web.context.support.AnnotationConfigWebApplicationContext doClose 873 - Closing WebApplicationContext for namespace 'dispatcherServlet-servlet': startup date [Tue May 19 13:57:21 CST 2015]; root of context hierarchy
19-May-2015 13:57:28.188 WARNING [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoader.clearReferencesThreads The web application [/authServer] appears to have started a thread named [NioSocketAcceptor-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
sun.nio.ch.SelectorImpl.select(SelectorImpl.java:102)
org.apache.mina.transport.socket.nio.NioSocketAcceptor.select(NioSocketAcceptor.java:234)
org.apache.mina.core.polling.AbstractPollingIoAcceptor$Acceptor.run(AbstractPollingIoAcceptor.java:417)
org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:64)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:745)
19-May-2015 13:57:28.218 INFO [Thread-5] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-80"]
表示mina线程没有正确的关闭, 可能会造成内存泄漏.
解决方法是在Spring配置中指定资源清理方法. 即在@Bean注解中配置destroyMethod属性
@Bean(destroyMethod = "dispose")
@Autowired
@Qualifier("SelfServiceHandler")
public IoAcceptor minaInitial(IoHandler handler) throws IOException {
IoAcceptor acceptor = new NioSocketAcceptor();
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
chain.addLast("logger", new LoggingFilter());
chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName(xmlCharSet))));
acceptor.setHandler(handler);
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, selfServiceIdleTime);
acceptor.bind(new InetSocketAddress(selfServicePort));
return acceptor;
}
参考 http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02s02.html