NutzCN Logo
问答 Spring整合nutz报错
发布于 2895天前 作者 黄海峰 1632 次浏览 复制 上一个帖子 下一个帖子
标签:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
	org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

root cause

java.lang.NullPointerException
	org.nutz.dao.impl.NutDao.getEntity(NutDao.java:661)
	com.guanfutech.admin.utils.guanfu.v1.index.dao.IndexDao.queryBanners(IndexDao.java:35)
	com.guanfutech.admin.utils.guanfu.v1.index.service.IndexService.buildIndexView(IndexService.java:24)
	com.guanfutech.admin.module.course.controller.CourseAction.saveOrUpdateCourse(CourseAction.java:479)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:497)
	org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
	org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
	org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
	org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

Sql sql = dao.sqls().create("article.banner");
        sql.setParam("dev",device);
        sql.setCallback(Sqls.callback.entities());
        Entity<BannerView> entity = dao.getEntity(BannerView.class);  //错误代码
        sql.setEntity(entity);
        dao.execute(sql);
        return sql.getList(BannerView.class);
11 回复

没有为NutDao设置DataSource的样子

<bean id="nutDao" class="org.nutz.dao.impl.NutDao">
              <property name="dataSource" ref="dataSource_db01"/>
       </bean>
       <!--<bean id="sqlManager" class="org.nutz.dao.impl.FileSqlManager">
              <constructor-arg>
                     <array value-type="java.lang.String">
                            <value>/conf/sqls/article.sql</value>
                            <value>/conf/sqls/index.sql</value>
                     </array>
              </constructor-arg>
       </bean> 昨天的思路是这样,.sql文件并没有读取-->
       <bean id="gfDao" class="com.guanfutech.admin.utils.guanfu.v1.common.GfDao">
              <property name="dataSource" ref="dataSource_db01"/>
       </bean>
@Component
public class GfDao extends NutDao implements Dao{

    private DataSource dataSource;

    private FileSqlManager fileSqlManager;

    private static final String[] SQLS = {
            "article.sql",
            "index.sql"
    };

    @Autowired
    public void setFileSqlManager() {
        List<BufferedReader> sqlList = new ArrayList<>(SQLS.length);

        try {
            for(String sql : SQLS)
                sqlList.add(new BufferedReader(new InputStreamReader(new ClassPathResource("/sqls/"+sql).getInputStream())));
        } catch (IOException e) {
            e.printStackTrace();
        }

        this.fileSqlManager = new FileSqlManager();

        try {
            for(BufferedReader reader : sqlList){
                this.fileSqlManager.add(reader);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        setSqlManager(fileSqlManager);
    }

    @Override
    public void run(ConnCallback callback) {
        Connection con = DataSourceUtils.getConnection(getDataSource());
        try {
            callback.invoke(con);
        } catch (Exception e) {
            if (e instanceof RuntimeException)
                throw (RuntimeException) e;
            else
                throw new RuntimeException(e);
        } finally {
            DataSourceUtils.releaseConnection(con, getDataSource());
        }
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    @Override
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
}

我debug跟进去 DataSource 是有的 ,是我思路错了吗?兽总,给点建议,或者参考文件,我重新来也好 没思路啦
整个.sql文件中的sql,是fileSqlManager.add(reader)添加进去的 在调用dao.getEntity()报错

<bean id="nutDao" class="org.nutz.dao.impl.NutDao">
              <property name="dataSource" ref="dataSource_db01"/>
       </bean>
       <bean id="sqlManager" class="org.nutz.dao.impl.FileSqlManager">
              <constructor-arg>
                     <array value-type="java.lang.String">
                            <value>/conf/sqls/article.sql</value>
                            <value>/conf/sqls/index.sql</value>
                     </array>
              </constructor-arg>
       </bean>

我一开始想着这样加入 但是sql文件读取不到 所以我就集成nutz复写了方法

<bean id="nutDao" class="org.nutz.dao.impl.NutDao">
              <property name="dataSource" ref="dataSource_db01"/>
              <property name="sqlManager" ref="sqlManager"/>
       </bean>

是这样引入sqlMapper,刚才那个不全

单说GfDao 这个类 存在的意义是啥? 为啥不直接用NutDao实例,为啥要继承它?

另外, GfDao 有setDataSource方法, 然后NutDao也有setDataSource,然后GfDao的setDataSource要调用super.setDataSource才行啊

单纯使用NutDao就好了, NutDao是线程安全的, 不需要继承

专心配NutDao. 把GfDao 删了,不要折腾GfDao ,删掉删掉.

      <bean id="nutDao" class="org.nutz.dao.impl.NutDao">
              <property name="dataSource" ref="dataSource_db01"/>
              <property name="sqlManager" ref="sqlManager"/> <!-- 要引用才能读 -->
       </bean>
       <bean id="sqlManager" class="org.nutz.dao.impl.FileSqlManager">
              <constructor-arg>
                     <array value-type="java.lang.String">
                            <value>/conf/sqls/article.sql</value>
                            <value>/conf/sqls/index.sql</value>
                     </array>
              </constructor-arg>
       </bean>

找到原因啦
GfDao的这个吧nutDao中的那个覆盖啦
@Override
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
我把这些都删除啦,就好使啦。

兽总,我有一个问题:你是怎么分析出来他缺少DataSource的?
接下来我看怎么配置nutDao 让他读取到sql文件中的sql

没在NutDao检测过DataSource为null... 既然抛NPE了, 99%是DataSource没设置了

添加回复
请先登陆
回到顶部