springmvc框架如何把通过socket传过来的数据保存到数据库

lfiefjw09 2017-12-20 02:34:53
通过socket数据传过来了,但是进行保存数据库时,jdbcTemplate报空指针,jdbcTemplate是用注解注入进来的
...全文
827 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wqyld9527 2017-12-25
  • 打赏
  • 举报
回复
请教一下,你这个SOCKET接收数据已经可以了吗?
什么都不能 2017-12-21
  • 打赏
  • 举报
回复
引用 10 楼 ygr_0801 的回复:
[quote=引用 9 楼 hanpoyangtitan 的回复:] 你的socket服务是什么时候实例化的,需要在springcontext完成加载后才能实例化,建议放到spring监听器里
socket是在web.xml中添加listener进行实例化的,是不是如果放到spring监听器里,通过socket这边进行增删改查时就可以用注解[/quote] 你要用Spring注解就需要让Spring接管你的实例,你就不能自己new了,能不能起线程的时候通过构造参数把service实例注入到线程中呢?这个要你自己试试看
lfiefjw09 2017-12-21
  • 打赏
  • 举报
回复
引用 11 楼 maradona1984 的回复:
把你的socket server服务的代码让spring管理就行了,然后直接注入
是把初始化socket server服务的类交给spring去管理吗 <bean id="serverListener" class="com.kw.socket.ServerListener"></bean>
maradona1984 2017-12-21
  • 打赏
  • 举报
回复
把你的socket server服务的代码让spring管理就行了,然后直接注入
lfiefjw09 2017-12-21
  • 打赏
  • 举报
回复
引用 9 楼 hanpoyangtitan 的回复:
你的socket服务是什么时候实例化的,需要在springcontext完成加载后才能实例化,建议放到spring监听器里
socket是在web.xml中添加listener进行实例化的,是不是如果放到spring监听器里,通过socket这边进行增删改查时就可以用注解
lfiefjw09 2017-12-21
  • 打赏
  • 举报
回复
弄好了,谢谢了
什么都不能 2017-12-20
  • 打赏
  • 举报
回复
你的socket服务是什么时候实例化的,需要在springcontext完成加载后才能实例化,建议放到spring监听器里
dong_19890208 2017-12-20
  • 打赏
  • 举报
回复 1
ICartService cartService =(ICartService) AppContextUtil.getBean("cartService"); 这句应该放到run方法里面,这里有个时序问题,你的线程启动的时候spring的初始化有可能还没完成,就会报空指针,你的线程最好通过下面的代码启动,控制加载顺序 @Companant public class Initor{ // 控制加载顺序 @AutoWire private CartService cartService @PostConstructor public void init(){ // 在这里启动线程 } } 另 JdbcTemplate jdbcTemp =(JdbcTemplate) AppContextUtil.getBean("jdbcTemp"); service里的这一句不需要
lfiefjw09 2017-12-20
  • 打赏
  • 举报
回复
引用 1 楼 dong_19890208 的回复:
能不能把代码贴出来?
按照你那样做了项目启动时又报错了 public class ServerSocketThread extends Thread{ ICartService cartService =(ICartService) AppContextUtil.getBean("cartService"); Socket socket = null; public ServerSocketThread(Socket s) { this.socket = s; } public void run() { try { InputStream ips = socket.getInputStream(); OutputStream ops = socket.getOutputStream(); while (true) { byte[] bt = readStream(ips); String str = new String(bt); // System.out.println("主机收到信息:" + str); String cartCode=str.substring(8, 18); boolean flag=cartService.saveOrUpdateCartIp(cartCode, socket); if(flag){ String restr="cmd1received"; ops.write(restr.getBytes()); ops.flush(); } String restr = "你好,主机已经收到信息!"; ops.write(restr.getBytes()); ops.flush(); } } catch (Exception e) { e.printStackTrace(); } } public static byte[] readStream(InputStream inStream) throws Exception { int count = 0; while (count == 0) { count = inStream.available(); } byte[] b = new byte[count]; inStream.read(b); return b; } } @Service("cartService") public class CartServiceImpl implements ICartService { @Autowired private JdbcTemplate jdbcTemplate; JdbcTemplate jdbcTemp =(JdbcTemplate) AppContextUtil.getBean("jdbcTemp"); @Override public boolean findCartbyCode(String code) { String sql="select * from t_cart where cart_code=?"; System.out.println(jdbcTemplate); List list=jdbcTemplate.queryForList(sql, code); if(null!=list&&list.size()>0){ return true; } return false; } @Override public boolean saveOrUpdateCartIp(String cartCode, Socket socket) { String sql="select * from t_cart where cart_code='"+cartCode+"'"; List list=jdbcTemplate.queryForList(sql); if(null!=list&&list.size()>0){ String cart_ip=socket.getInetAddress().getHostAddress()+socket.getLocalPort(); String sql_upd="update t_cart set ip_port='"+cart_ip+"' where cart_code='"+cartCode+"'"; int flag=jdbcTemp.update(sql_upd); if(flag>0){ return true; } } return false; } }
dong_19890208 2017-12-20
  • 打赏
  • 举报
回复
引用 5 楼 ygr_0801 的回复:
[quote=引用 3 楼 dong_19890208 的回复:] ICartService cartService=new CartServiceImpl(); 你的CartService是自己new出来的不是交给spring 管理的,这样肯定是注入不进去的; 你需要加一个AppContextUtil,然后通过下面的方式获取cartService ICartService cartService =(ICartService) AppContextUtil.getBean("cartService"); @Component public class AppContextUtil implements ApplicationContextAware { /** * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. */ private static ApplicationContext applicationContext; // Spring应用上下文环境 /** * 实现ApplicationContextAware接口的回调方法,设置上下文环境 * * @param applicationContext * @throws BeansException */ @Autowired public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { AppContextUtil.applicationContext = applicationContext; } /** * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 获取对象 * * @param name * @return Object 一个以所给名字注册的bean的实例 * @throws BeansException */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } /** * 获取类型为requiredType的对象 * 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException) * * @param name * bean注册名 * @param requiredType * 返回对象类型 * @return Object 返回requiredType类型对象 * @throws BeansException */ public static Object getBean(String name, Class<?> requiredType) throws BeansException { return applicationContext.getBean(name, requiredType); } /** * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true * * @param name * @return boolean */ public static boolean containsBean(String name) { return applicationContext.containsBean(name); } /** * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException) * * @param name * @return boolean * @throws NoSuchBeanDefinitionException */ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return applicationContext.isSingleton(name); } /** * @param name * @return Class 注册对象的类型 * @throws NoSuchBeanDefinitionException */ public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { return applicationContext.getType(name); } /** * 如果给定的bean名字在bean定义中有别名,则返回这些别名 * * @param name * @return * @throws NoSuchBeanDefinitionException */ public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return applicationContext.getAliases(name); } }
@Service("cartService") public class CartServiceImpl implements ICartService { @Autowired private JdbcTemplate jdbcTemplate; @Override public boolean findCartbyCode(String code) { String sql="select * from t_cart where cart_code=?"; System.out.println(jdbcTemplate); List list=jdbcTemplate.queryForList(sql, code); if(null!=list&&list.size()>0){ return true; } return false; } @Override public boolean saveOrUpdateCartIp(String cartCode, Socket socket) { String sql="select * from t_cart where cart_code='"+cartCode+"'"; List list=jdbcTemplate.queryForList(sql); if(null!=list&&list.size()>0){ String cart_ip=socket.getInetAddress().getHostAddress()+socket.getLocalPort(); String sql_upd="update t_cart set ip_port='"+cart_ip+"' where cart_code='"+cartCode+"'"; int flag=jdbcTemplate.update(sql_upd); if(flag>0){ return true; } } return false; } } 前面那个方法是controller层调用的都可以正常访问,后面这个方法是在socket那里调用的就会报JdbcTemplate空指针 [/quote]在Controller里面你的Service 是通过Spring注入的,所以可以正常注入,可是在你的socket处理这块spring是管理不到的,况且你直接new出来的service肯定是注不进去的; 通过我说的方法,就是从Spring 的容器中取出Spring 管理的service 的实例,这样取到的实例跟Controller里注入的是同一个实例 你new出来的就是另外一个实例,跟new了一个普通java对象是一样的 楼主应该对spring的注入机制了解得还不深入
lfiefjw09 2017-12-20
  • 打赏
  • 举报
回复
引用 3 楼 dong_19890208 的回复:
ICartService cartService=new CartServiceImpl(); 你的CartService是自己new出来的不是交给spring 管理的,这样肯定是注入不进去的; 你需要加一个AppContextUtil,然后通过下面的方式获取cartService ICartService cartService =(ICartService) AppContextUtil.getBean("cartService"); @Component public class AppContextUtil implements ApplicationContextAware { /** * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. */ private static ApplicationContext applicationContext; // Spring应用上下文环境 /** * 实现ApplicationContextAware接口的回调方法,设置上下文环境 * * @param applicationContext * @throws BeansException */ @Autowired public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { AppContextUtil.applicationContext = applicationContext; } /** * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 获取对象 * * @param name * @return Object 一个以所给名字注册的bean的实例 * @throws BeansException */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } /** * 获取类型为requiredType的对象 * 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException) * * @param name * bean注册名 * @param requiredType * 返回对象类型 * @return Object 返回requiredType类型对象 * @throws BeansException */ public static Object getBean(String name, Class<?> requiredType) throws BeansException { return applicationContext.getBean(name, requiredType); } /** * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true * * @param name * @return boolean */ public static boolean containsBean(String name) { return applicationContext.containsBean(name); } /** * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException) * * @param name * @return boolean * @throws NoSuchBeanDefinitionException */ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return applicationContext.isSingleton(name); } /** * @param name * @return Class 注册对象的类型 * @throws NoSuchBeanDefinitionException */ public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { return applicationContext.getType(name); } /** * 如果给定的bean名字在bean定义中有别名,则返回这些别名 * * @param name * @return * @throws NoSuchBeanDefinitionException */ public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return applicationContext.getAliases(name); } }
@Service("cartService") public class CartServiceImpl implements ICartService { @Autowired private JdbcTemplate jdbcTemplate; @Override public boolean findCartbyCode(String code) { String sql="select * from t_cart where cart_code=?"; System.out.println(jdbcTemplate); List list=jdbcTemplate.queryForList(sql, code); if(null!=list&&list.size()>0){ return true; } return false; } @Override public boolean saveOrUpdateCartIp(String cartCode, Socket socket) { String sql="select * from t_cart where cart_code='"+cartCode+"'"; List list=jdbcTemplate.queryForList(sql); if(null!=list&&list.size()>0){ String cart_ip=socket.getInetAddress().getHostAddress()+socket.getLocalPort(); String sql_upd="update t_cart set ip_port='"+cart_ip+"' where cart_code='"+cartCode+"'"; int flag=jdbcTemplate.update(sql_upd); if(flag>0){ return true; } } return false; } } 前面那个方法是controller层调用的都可以正常访问,后面这个方法是在socket那里调用的就会报JdbcTemplate空指针
lfiefjw09 2017-12-20
  • 打赏
  • 举报
回复
引用 3 楼 dong_19890208 的回复:
ICartService cartService=new CartServiceImpl(); 你的CartService是自己new出来的不是交给spring 管理的,这样肯定是注入不进去的; 你需要加一个AppContextUtil,然后通过下面的方式获取cartService ICartService cartService =(ICartService) AppContextUtil.getBean("cartService"); @Component public class AppContextUtil implements ApplicationContextAware { /** * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. */ private static ApplicationContext applicationContext; // Spring应用上下文环境 /** * 实现ApplicationContextAware接口的回调方法,设置上下文环境 * * @param applicationContext * @throws BeansException */ @Autowired public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { AppContextUtil.applicationContext = applicationContext; } /** * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 获取对象 * * @param name * @return Object 一个以所给名字注册的bean的实例 * @throws BeansException */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } /** * 获取类型为requiredType的对象 * 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException) * * @param name * bean注册名 * @param requiredType * 返回对象类型 * @return Object 返回requiredType类型对象 * @throws BeansException */ public static Object getBean(String name, Class<?> requiredType) throws BeansException { return applicationContext.getBean(name, requiredType); } /** * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true * * @param name * @return boolean */ public static boolean containsBean(String name) { return applicationContext.containsBean(name); } /** * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException) * * @param name * @return boolean * @throws NoSuchBeanDefinitionException */ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return applicationContext.isSingleton(name); } /** * @param name * @return Class 注册对象的类型 * @throws NoSuchBeanDefinitionException */ public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { return applicationContext.getType(name); } /** * 如果给定的bean名字在bean定义中有别名,则返回这些别名 * * @param name * @return * @throws NoSuchBeanDefinitionException */ public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return applicationContext.getAliases(name); } }
我这个项目也是可以用注解的,在其他地方用注解好着,在这个地方用就会和jdbcTemplate一样报空指针
dong_19890208 2017-12-20
  • 打赏
  • 举报
回复
ICartService cartService=new CartServiceImpl(); 你的CartService是自己new出来的不是交给spring 管理的,这样肯定是注入不进去的; 你需要加一个AppContextUtil,然后通过下面的方式获取cartService ICartService cartService =(ICartService) AppContextUtil.getBean("cartService"); @Component public class AppContextUtil implements ApplicationContextAware { /** * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. */ private static ApplicationContext applicationContext; // Spring应用上下文环境 /** * 实现ApplicationContextAware接口的回调方法,设置上下文环境 * * @param applicationContext * @throws BeansException */ @Autowired public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { AppContextUtil.applicationContext = applicationContext; } /** * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 获取对象 * * @param name * @return Object 一个以所给名字注册的bean的实例 * @throws BeansException */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } /** * 获取类型为requiredType的对象 * 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException) * * @param name * bean注册名 * @param requiredType * 返回对象类型 * @return Object 返回requiredType类型对象 * @throws BeansException */ public static Object getBean(String name, Class<?> requiredType) throws BeansException { return applicationContext.getBean(name, requiredType); } /** * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true * * @param name * @return boolean */ public static boolean containsBean(String name) { return applicationContext.containsBean(name); } /** * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException) * * @param name * @return boolean * @throws NoSuchBeanDefinitionException */ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return applicationContext.isSingleton(name); } /** * @param name * @return Class 注册对象的类型 * @throws NoSuchBeanDefinitionException */ public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { return applicationContext.getType(name); } /** * 如果给定的bean名字在bean定义中有别名,则返回这些别名 * * @param name * @return * @throws NoSuchBeanDefinitionException */ public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return applicationContext.getAliases(name); } }
lfiefjw09 2017-12-20
  • 打赏
  • 举报
回复
引用 1 楼 dong_19890208 的回复:
能不能把代码贴出来?
public class ServerSocketThread extends Thread{ Socket socket = null; public ServerSocketThread(Socket s) { this.socket = s; } public void run() { try { InputStream ips = socket.getInputStream(); OutputStream ops = socket.getOutputStream(); while (true) { byte[] bt = readStream(ips); String str = new String(bt); // System.out.println("主机收到信息:" + str); String cartCode=str.substring(8, 18); ICartService cartService=new CartServiceImpl(); boolean flag=cartService.saveOrUpdateCartIp(cartCode, socket); if(flag){ String restr="cmd1received"; ops.write(restr.getBytes()); ops.flush(); } } } catch (Exception e) { e.printStackTrace(); } } public static byte[] readStream(InputStream inStream) throws Exception { int count = 0; while (count == 0) { count = inStream.available(); } byte[] b = new byte[count]; inStream.read(b); return b; } } @Service("cartService") public class CartServiceImpl implements ICartService { @Autowired private JdbcTemplate jdbcTemplate; @Override public boolean saveOrUpdateCartIp(String cartCode, Socket socket) { String sql="select * from t_cart where cart_code='"+cartCode+"'"; List list=jdbcTemplate.queryForList(sql); if(null!=list&&list.size()>0){ String cart_ip=socket.getInetAddress().getHostAddress()+socket.getLocalPort(); String sql_upd="update t_cart set ip_port='"+cart_ip+"' where cart_code='"+cartCode+"'"; int flag=jdbcTemplate.update(sql_upd); if(flag>0){ return true; } } return false; } }
dong_19890208 2017-12-20
  • 打赏
  • 举报
回复
能不能把代码贴出来?

81,115

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧