随着春天的烦恼摆动

时间:2013-10-22 13:08:59

标签: java spring swing autowired

我正在尝试在Spring Web MVC项目中使用的Swing应用程序中使用@Autowired语句。我只是将其作为Java应用程序运行。

import javax.mail.MessagingException;
import javax.swing.*;

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

import net.babobka.blog.headHunter.Converter;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class MainFrame implements ActionListener {
    @Autowired
    private Converter converter;    
    JFrame jfrm = new JFrame("Project backup system");
    JButton startButton = new JButton("Start"),
            stopButton = new JButton("Stop");
    JLabel statusLab = new JLabel();
    BackupThread bt;

    MainFrame() {
        int eur = (int) converter.getRate("EUR", "RUB");//This is MainFrame.java:29
        jfrm.setLayout(new FlowLayout());
        startButton.addActionListener(this);
        stopButton.addActionListener(this);
        jfrm.setSize(275, 100);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfrm.add(startButton);
        jfrm.add(stopButton);
        jfrm.add(statusLab);
        jfrm.setVisible(true);
    }...

NullPointerException就是我所拥有的一切。接线有什么问题? 完整跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at net.babobka.blog.app.MainFrame.<init>(MainFrame.java:29)
    at net.babobka.blog.app.MainFrame$1.run(MainFrame.java:60)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Converter适用于其他类,但Swing应用除外。

根context.xml中:

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
     http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd     
     http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">



    <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="commonStatistic" />

                <bean
                    class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
                    p:name="langStatistic" />

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

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

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

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

    <bean id="mainFrame" class="net.babobka.blog.app.MainFrame" />


    <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>



</beans>

1 个答案:

答案 0 :(得分:3)

施工后设置属性。尝试将初始化代码从Ctor移动到初始化方法。

例如

public class MainFrame() {

    public MainFrame() {}

    @PostConstruct
    public void init() {
        int eur = (int) converter.getRate("EUR", "RUB");
        ....
   }
}