自动装配不起作用

时间:2013-09-27 07:03:18

标签: java spring spring-mvc

请帮我解决这个错误,我是春天的新手。

这是堆栈跟踪:

12:23:16,350 ERROR [STDERR] java.lang.NullPointerException
    12:23:16,351 ERROR [STDERR]     at igate.service.ProgramsOfferedServiceImpl.retrievePrograms(ProgramsOfferedServiceImpl.java:39)
    12:23:16,351 ERROR [STDERR]     at org.apache.jsp.programs_jsp._jspService(programs_jsp.java:118)
    12:23:16,351 ERROR [STDERR]     at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    12:23:16,351 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    12:23:16,351 ERROR [STDERR]     at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:387)
    12:23:16,351 ERROR [STDERR]     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    12:23:16,351 ERROR [STDERR]     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    12:23:16,351 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    12:23:16,351 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    12:23:16,351 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    12:23:16,351 ERROR [STDERR]     at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:687)
    12:23:16,351 ERROR [STDERR]     at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:469)
    12:23:16,351 ERROR [STDERR]     at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:403)
    12:23:16,351 ERROR [STDERR]     at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
    12:23:16,351 ERROR [STDERR]     at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    12:23:16,351 ERROR [STDERR]     at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    12:23:16,351 ERROR [STDERR]     at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
    12:23:16,351 ERROR [STDERR]     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    12:23:16,351 ERROR [STDERR]     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    12:23:16,351 ERROR [STDERR]     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    12:23:16,351 ERROR [STDERR]     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    12:23:16,351 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    12:23:16,351 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    12:23:16,351 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    12:23:16,351 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    12:23:16,351 ERROR [STDERR]     at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    12:23:16,351 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    12:23:16,351 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    12:23:16,351 ERROR [STDERR]     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    12:23:16,351 ERROR [STDERR]     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    12:23:16,351 ERROR [STDERR]     at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
    12:23:16,351 ERROR [STDERR]     at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
    12:23:16,351 ERROR [STDERR]     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    12:23:16,351 ERROR [STDERR]     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    12:23:16,352 ERROR [STDERR]     at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
    12:23:16,352 ERROR [STDERR]     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    12:23:16,352 ERROR [STDERR]     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
    12:23:16,352 ERROR [STDERR]     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    12:23:16,352 ERROR [STDERR]     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
    12:23:16,352 ERROR [STDERR]     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    12:23:16,352 ERROR [STDERR]     at java.lang.Thread.run(Unknown Source)

这是数据访问的代码:

package igate.dao;
import java.sql.*;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import igate.dto.ProgramsOffered;
@Component
public class ProgramsOfferedDataAccessImpl implements ProgramsOfferedDataAccess 
{
    @Autowired
    JdbcTemplate template;

    public ProgramsOfferedDataAccessImpl() {
        // TODO Auto-generated constructor stub
    System.out.println("inside programs offered");
    }

    @Override
    public boolean DeleteProgramsOffered(String name)throws SQLException 
    {
        boolean rowsAffected = false;
        String delete = "DELETE FROM programs_offered WHERE program_name='"+name+"'";
        int r = template.update(delete);
        if(r>0)
            rowsAffected=true;
        return rowsAffected;
    }

    @Override
    public ArrayList<ProgramsOffered> retrievePrograms() 
    {
        RowMapper<ProgramsOffered> row = new RowMapper<ProgramsOffered>()
        {

            @Override
            public ProgramsOffered mapRow(ResultSet rs, int arg1) throws SQLException 
            {
                ProgramsOffered po = new ProgramsOffered();
                po.setProgramName(rs.getString(1));
                po.setDescription(rs.getString(2));
                po.setApplicantEligibility(rs.getString(3));
                po.setDuration(rs.getInt(4));
                po.setDegreeCertificateOffered(rs.getString(5));
                return po;
            }

        };
        String sql = "select * from programs_offered";
        System.out.println(template);
        return (ArrayList<ProgramsOffered>)template.query(sql, row);
    }

    @Override
    public boolean insertProgram(ProgramsOffered po) 
    {
        boolean rowsAffected = false;
        String insert = "insert into programs_offered values(?,?,?,?,?)";
        int r = template.update(insert);
        if(r>0)
            rowsAffected=true;
        return rowsAffected;
    }

    @Override
    public boolean updateProgram(String progName, String colName, String newValue) 
    {
        return false;
    }

    @Override
    public boolean updateProgram(String progName, String colName, int newValue) 
    {
        return false;
    }

    public JdbcTemplate getTemplate() 
    {
        return template;
    }

    public void setTemplate(JdbcTemplate template) 
    {
        this.template = template;
    }
}

这是我的服务层。我无法解决问题所在,因为我自动连线ProgramsOffered poda但我仍然得到空指针异常。

package igate.service;
import java.sql.SQLException;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import igate.dao.*;
import igate.dto.ProgramsOffered;
@Component("offeredService")
public class ProgramsOfferedServiceImpl implements ProgramsOfferedService 
{
    @Autowired
    public ProgramsOfferedDataAccess poda;


    @Override
    public boolean DeleteProgramsOffered(String name)
    {
        boolean rowsaffected=false;
        try
        {
            rowsaffected = poda.DeleteProgramsOffered(name);

        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return rowsaffected;
    }
    @Override
    public ArrayList<ProgramsOffered> retrievePrograms() 
    {
        ArrayList<ProgramsOffered> al = poda.retrievePrograms();
        return al;
    }
    @Override
    public boolean insertProgram(ProgramsOffered po) 
    {

        return poda.insertProgram(po);
    }
    @Override
    public boolean updateProgram(String progName, String colName, String newValue) 
    {

        return poda.updateProgram(progName, colName, newValue);
    }
    @Override
    public boolean updateProgram(String progName, String colName, int newValue) 
    {

        return poda.updateProgram(progName, colName, newValue);
    }
    @Override
    public ArrayList<ProgramsOffered> programDetails(String programName) 
    {
        ArrayList<ProgramsOffered> list = new ArrayList<ProgramsOffered>();
        list = poda.retrievePrograms();
        System.out.println("to keep at 0 "+programName);
        System.out.println("list size:" +list.size());
        int size = list.size();
        for(int i = 0;i<size;i++)
        {
            System.out.println(i);
            if(!list.get(i).getProgramName().trim().equals(programName))
            {
                System.out.println("removing: "+list.get(i).getProgramName());
                //list.remove(i);
            }
            else
            {
                ProgramsOffered temp = new ProgramsOffered();
                temp = list.get(0);
                list.set(0, list.get(i));
                list.set(i, temp);
                System.out.println("at 0 prog: "+list.get(0));
            }
        }
        return list;
    }


}

我在这一行得到一个空指针异常: ArrayList<ProgramsOffered> al = poda.retrievePrograms();

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

            <mvc:annotation-driven />
            <context:annotation-config/>
            <context:component-scan base-package="igate.dto" />
                <context:component-scan base-package="igate.dao" />
                    <context:component-scan base-package="igate.service" />
                        <context:component-scan base-package="igate.controller" />
                <context:component-scan base-package="igate.logs" />
                    <context:component-scan base-package="igate.testcases" />


            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                  <property name="prefix" value="/"/>
                  <property name="suffix" value=".jsp" />
            </bean> 


            <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
                <property name="url" value="jdbc:oracle:thin:@172.21.17.5:1521:oraten" />
                <property name="username" value="lab01trg21" />
                <property name="password" value="lab01oracle" />
            </bean>

            <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
                <property name="dataSource" ref="ds"/>
            </bean>

</beans>

2 个答案:

答案 0 :(得分:0)

您的applicationContext.xml(或dispatcher-servlet.xml)是否包含以下行:

<!-- Use annotation based configuration (e.g. @Service, @Component etc.) -->
<context:annotation-config/>
<!-- Autowire beans -->
<context:component-scan base-package="igate.service"/>

答案 1 :(得分:-1)

你没有poda的setter和getter。没有setter和getter Spring就无法自动装配