spring bean单例注入与用单例模式通过class.getinstance()区别?
1、action的某个方法中,用以下代码获得redis单例实例
RedisDelegate redisDelegate = RedisDelegate.getInstance();
redisDelegate.set(0, q1, result.toString());
2、Redisdelegate的单例模式如下
/** * 私有构造器 */ private RedisDelegate() { System.out.println("创建实例对象!"); JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(30); //2.5.2之前的版本是setMaxActive config.setMaxIdle(10); config.setMaxWaitMillis(3*1000); //2.5.2之前的版本是setMaxWait this.jedisPool = new JedisPool(config, REDIS_HOST,REDIS_PORT, 5000,"123"); } /** * @function: 创建redisDelegate单例 * 懒汉式 * @createTime 2014年11月4日 下午7:03:27 */ public synchronized static RedisDelegate getInstance() { if(instance == null) { System.out.println("懒汉式--单例模式,创建RedisDelegate实例!"); instance = new RedisDelegate(); } return instance; }
3、运行web应用程序,发布在tomcat下,action中方法被执行,但是
RedisDelegate redisDelegate = RedisDelegate.getInstance();
报错如下:
十一月 05, 2014 4:24:46 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for servlet [controller] in context with path [/adjustSolr] threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/JedisPoolConfig] with root cause java.lang.ClassNotFoundException: redis.clients.jedis.JedisPoolConfig at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571) at com.solr.adjust.controller.TestAction.testjedis(TestAction.java:13) at com.solr.adjust.controller.ConfigAction.configSearch(ConfigAction.java:61) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) at javax.servlet.http.HttpServlet.service(HttpServlet.java:620) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)
4、如果就走main方法或者实例化其他类,通过其他类的方法实例化redisDelegate则不报错误
public class TestAction { public static void main(String []args){ new TestAction().testjedis(); } public void testjedis(){ RedisDelegate redisDelegate = RedisDelegate.getInstance(); redisDelegate.set(0, "测试key111", "测试value1111"); System.out.println("测试开始!!!"); }在回到1步骤,添加以下代码执行是没有问题
TestAction test = new TestAction(); test.testjedis();
求问原因!!!!!!!!!!!!!!!!
是因为spring框架中,只能通过注入或者直接调用构造函数创建实例,而不能直接采用懒汉式单例模式创建实例?????????????
罪魁祸首JedisPoolConfig,当用spring的时候,必须结合spring来用,配置到配置文件中。
没有spring时候可以独立实例化JedisPoolConfig