当我在带有tomcat服务器的Spring Tool Suite中执行以下操作时,我没有收到任何错误。但是当我通过创建war在外部tomcat服务器中执行相同的操作时,我遇到了以下错误:
错误1:由以下原因引起:org.springframework.beans.factory.BeanCreationException
错误2:由以下原因引起:org.springframework.beans.BeanInstantiationException
错误3:由:java.awt.HeadlessException
引起无头(假)没有帮助我。
我的主要班级。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({"service.my.rest.app"})
public class ConnectionTest extends SpringBootServletInitializer{
public static void main(String[] args) {
// This method will run the application
SpringApplicationBuilder builder = new
SpringApplicationBuilder(ConnectionTest.class);
builder.headless(false);
ConfigurableApplicationContext context = builder.run(args);
}
}
使用Swing元素的类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.CallableStatement;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
@SuppressWarnings("serial")
class SpringTable1 extends JTable {
public SpringTable1(TableModel dm) {
super(dm);
setAutoResizeMode(AUTO_RESIZE_ALL_COLUMNS);
}
public SpringTable1() {
setAutoResizeMode(AUTO_RESIZE_ALL_COLUMNS);
}
@Override
public void doLayout() {
int width = getWidth();
int columnCount = getColumnCount();
int columnSize = width / columnCount;
for (int index = 0; index < columnCount; index++) {
TableColumn column = getColumnModel().getColumn(index);
column.setResizable(true);
column.setPreferredWidth(width);
column.setMinWidth(100);
}
super.doLayout();
}
@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}
}
@RestController
public class TLOG_Backup_Report extends JFrame {
private JButton _clickme;
private JPanel _panel;
private SpringTable1 table;
// this method is used to get TLOG status report automatically on loading
the link.
@RequestMapping("/tlog")
public String getTlogStatusReport() throws ClassNotFoundException,
SQLException
{
//Calling our inventory to get the details
Class.forName("*****************");
String dbURL = "**************";
Connection con = DriverManager.getConnection(dbURL);
CallableStatement proc_stmt = con.prepareCall("
{********************************}");
ResultSet rs = proc_stmt.executeQuery();
//It creates and displays the table
table = new SpringTable1(buildTableModel(rs));
// Closes the Connection
table.setSize(1000, 1000);
table.setRowHeight(40);
setVisible(true);
setResizable(true);
setTitle("Table");
setLocationRelativeTo(null);
setSize(1000,1000);
_panel = new JPanel();
getContentPane().add(new JScrollPane(table));
return "Results are displayed ";
}
//to build the structure of the table
public static DefaultTableModel buildTableModel(ResultSet rs) throws
SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
}
}
错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'TLOG_Backup_Report' defined in file [C:\Users\VALSA05\Documents\share\apache-tomcat-7.0.56-windows-x86\apache-tomcat-7.0.56\webapps\DbConnectionStatus-0.0.4\WEB-INF\classes\connectionStatusDB\TLOG_Backup_Report.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [connectionStatusDB.TLOG_Backup_Report]: Constructor threw exception; nested exception is java.awt.HeadlessException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1110)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1054)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:754)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.web.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:151)
at org.springframework.boot.web.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:131)
at org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:86)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5481)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 44 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [connectionStatusDB.TLOG_Backup_Report]: Constructor threw exception; nested exception is java.awt.HeadlessException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1102)
... 64 more
Caused by: java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:204)
at java.awt.Window.<init>(Window.java:536)
at java.awt.Frame.<init>(Frame.java:420)
at java.awt.Frame.<init>(Frame.java:385)
at javax.swing.JFrame.<init>(JFrame.java:189)
at connectionStatusDB.TLOG_Backup_Report.<init>(ConnectionTest.java:87)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
... 66 more
May 30, 2018 11:21:06 PM org.apache.catalina.startup.HostConfig deployWAR
SEVERE: Error deploying web application archive C:\Users\VALSA05\Documents\share\apache-tomcat-7.0.56-windows-x86\apache-tomcat-7.0.56\webapps\DbConnectionStatus-0.0.4.war
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/DbConnectionStatus-0.0.4]]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:904)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1083)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:553)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1670)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)