这是我的方法:

@Autowired
    private HeadHunterService headHunterService;

    public String getBarChartUrlByLang(String pl) {
        List<HeadHunter> headHunter = headHunterService.getLastDataByLang(pl);
        List<String> city = new ArrayList<String>();
        List<Integer> salaries = new ArrayList<Integer>();
        for (int i = 0; i < headHunter.size(); i++) {
            city.add(headHunter.get(i).getCity().toString());
            salaries.add(headHunter.get(i).getSalary().intValue());
        }
        final int MAX_SALARY = headHunter.get(0).getSalary();
        Data salaryData = DataUtil.scaleWithinRange(0, MAX_SALARY, salaries);
        BarChartPlot salary = Plots.newBarChartPlot(salaryData,
                Color.newColor("3298C9"));
        BarChart chart = GCharts.newBarChart(salary);

        // Defining axis info and styles
        AxisStyle axisStyle = AxisStyle.newAxisStyle(Color.newColor("000000"),
                9, AxisTextAlignment.CENTER);

        AxisLabels cities = AxisLabelsFactory.newAxisLabels(city);
        cities.setAxisStyle(axisStyle);

        AxisLabels salaryCount = AxisLabelsFactory.newNumericRangeAxisLabels(0,
                MAX_SALARY);
        salaryCount.setAxisStyle(axisStyle);

        // Adding axis info to chart.
        chart.addXAxisLabels(cities);
        chart.addYAxisLabels(salaryCount);
        chart.setHorizontal(false);
        chart.setSize(800, 300);
        chart.setSpaceBetweenGroupsOfBars(54);

        // /51 is the max number of medals.
        chart.setGrid(10, 800, 3, 2);
        chart.setBackgroundFill(Fills.newSolidFill(Color.newColor("FFFFFF")));
        String url = chart.toURLString();

        return url;
    }


这就是我的XML:

<bean id="googleCharts" class="net.babobka.blog.charts.GoogleCharts" />


如果我在控制器中调用它,它将正常工作。但是现在我试图调用另一个名为PDFBuilder的类。这就是我得到的:

java.lang.NullPointerException
    at net.babobka.blog.pdf.PDFBuilder.buildPdfDocument(PDFBuilder.java:47)
    at net.babobka.blog.pdf.AbstractITextPdfView.renderMergedOutputModel(AbstractITextPdfView.java:47)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
    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:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)


第47行是:System.out.println(googleCharts.getBarChartUrlByLang("Java"));

这是我的根上下文:

<context:annotation-config />


    <context:component-scan base-package="net.babobka.blog" />
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <cache:annotation-driven />

    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <bean
                    class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
                    p:name="template" />
                <bean
                    class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
                    p:name="headHunter" />

            </set>
        </property>
    </bean>

    <bean id="converter" class="net.babobka.blog.headHunter.Converter" />

    <bean id="googleCharts" class="net.babobka.blog.charts.GoogleCharts" />


    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/db/jdbc.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>/WEB-INF/db/hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">${jdbc.show_sql}</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
            </props>
        </property>
    </bean>




    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <mvc:annotation-driven />
    <tx:annotation-driven />
    <task:annotation-driven />



    <bean id="Backupper" class="net.babobka.blog.backupper.Backupper"></bean>

    <bean id="OldDataRemoval" class="net.babobka.blog.termination.OldDataRemoval"></bean>

    <bean id="HeadHunterImport" class="net.babobka.blog.headHunter.HeadHunterImport"></bean>



    <bean id="urlForwardController"
        class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />

    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.tiles2.TilesView" />
    </bean>

    <bean id="viewResolver1"
        class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
        <property name="order" value="1" />
        <property name="basename" value="views" />
    </bean>


和PDFBuilder(我认为这没有帮助):

public class PDFBuilder extends AbstractITextPdfView {

    @Autowired
    private GoogleCharts googleCharts;

    @Override
    protected void buildPdfDocument(Map<String, Object> model, Document doc,
            PdfWriter writer, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        // get data model which is passed by the Spring container

        List<HeadHunter> listHeadHunter = (List<HeadHunter>) model
                .get("listHeadHunter");
        doc.add(new Paragraph("Last data for "
                + listHeadHunter.get(0).getPl().toString()));
        System.out.println(googleCharts.getBarChartUrlByLang("C++"));
        String imageUrl = "http://chart.apis.google.com/chart?cht=bvg&chxt=y,x&chco=3298C9&chs=800x300&chbh=23,4,54&chf=bg,s,FFFFFF&chg=10.0,800.0,3,2&chxr=0,0.0,91557.0&chd=e:..0wx1t6tbrBpCkhjOi4&chxs=0,000000,9,0|1,000000,9,0&chxl=1:|Moscow|Saint+Petersburg|Voronezh|Nizhny+Novgorod|Tomsk|Novosibirsk|Yekaterinburg|Samara|Kazan|Rostov-on-Don";

        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(100.0f);
        table.setWidths(new float[] { 3.0f, 2.0f, 2.0f });
        table.setSpacingBefore(10);

        // define font for table header row
        Font font = FontFactory.getFont(FontFactory.HELVETICA);
        font.setColor(BaseColor.WHITE);

        // define table header cell
        PdfPCell cell = new PdfPCell();
        BaseColor color = new BaseColor(50, 152, 186);
        cell.setBackgroundColor(color);
        cell.setPadding(10);

        // write table header
        cell.setPhrase(new Phrase("City", font));
        table.addCell(cell);

        cell.setPhrase(new Phrase("Vacancies", font));
        table.addCell(cell);

        cell.setPhrase(new Phrase("Avg. salary", font));
        table.addCell(cell);

        // write table row data
        for (HeadHunter hh : listHeadHunter) {
            table.addCell(hh.getCity());
            table.addCell(hh.getVacancies().toString());
            table.addCell(hh.getSalary().toString());

        }

        doc.add(table);
        doc.add(new Paragraph("\n Visual representation (salary/city)"));
        Image image = Image.getInstance(new URL(imageUrl));
        image.setAbsolutePosition(35f, 335f);
        image.scaleAbsolute(540f, 203f);
        doc.add(image);

    }

}


怎么了?

最佳答案

PdfBuilder不是Spring托管的bean,因此没有@Autowired将起作用。使其成为bean或使用@Configurable

有关自动装配的更多信息:The IoC container(请参见第5.4.5节),@Configurable javadoc

08-17 15:32