- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在位于 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/
我有一个树结构,我想使用 Promises 遍历它,但我还没有找到适合它的代码模式。 假设我们得到了节点名称,并且将节点名称转换为节点的行为是一个异步过程(例如涉及网络访问)。还假设每个节点包含一个(
我正在从 github 存储库获取信息。我想要获取该存储库中的拉取请求列表,获取与每个拉取请求关联的提交列表,然后对于每个提交,我想要获取信息,例如提交的作者、与每个提交关联的文件数量以及对每个文件进
我正在尝试在位于 Spring Web MVC 项目中的 Swing 应用程序中使用 @Autowired 语句。我只是将它作为 Java 应用程序运行。 import javax.mail.Mess
我正在尝试测试一个函数,该函数调用另一个模块的函数并返回一个 promise ,问题是 jest 不会等待 myFunction 完成,而是跳出它并将其视为 promise ,因为结果部分显示“don
当我在 Rider (net core) 中按下运行按钮时,它会运行以下命令: /usr/local/share/dotnet/dotnet /pathtomyproject/myproject.dl
我在 hibernate 状态下有点困惑, 这里我有一个父对象,这个父对象(假设A是父对象,B是子对象)与子对象B具有多对一的单向关系。 现在,我有一个对象 A 和对象 B 。我所做的是,我将所有子对
我是一名优秀的程序员,十分优秀!