gpt4 book ai didi

java - 带着 Spring 的烦恼 Swing

转载 作者:行者123 更新时间:2023-12-01 23:29:44 25 4
gpt4 key购买 nike

我正在尝试在位于 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 应用程序之外的其他类。

根上下文.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>

最佳答案

属性在构建后设置。尝试将初始化代码从 Ctor 移至初始化方法。

例如

public class MainFrame() {

public MainFrame() {}

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

关于java - 带着 Spring 的烦恼 Swing ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19518823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com