Home > database >  Error creating bean with name 'cvpSpringBootApplication': Bean instantiation via construct
Error creating bean with name 'cvpSpringBootApplication': Bean instantiation via construct

Time:07-18

I want to run a Maven install, but I get an "org.springframework.beans.factory.BeanCreationException" exception. The start with the Maven goal works normally. Unfortunately I can not find the error why the instance is not filled.

The project is a Spring Boot application with a Jswing user interface. This makes several api calls and includes its own rest controller.

Main-Class:

package com.rofu.cvp;

@SpringBootApplication
public class CvpSpringBootApplication extends JFrame {

    private static final long serialVersionUID = -4207943038367121887L;

    private Logger logger = LoggerFactory.getLogger(CvpSpringBootApplication.class);

    @Value("${spring.application.name}")
    private String applicationName;
    private TrayIcon trayIcon;

    @Autowired
    public CvpSpringBootApplication() {
        
        initializeUI();
    }

    /**
     * This Method set the Theme and components from the UI
     */
    private void initializeUI() {

        try {

            FlatLightLaf.setup();
            UIManager.setLookAndFeel(new FlatLightLaf());
            UIManager.put("TabbedPane.selectedBackground", Color.white);
            UIManager.put("ScrollBar.thumbArc", 999);
            UIManager.put("ScrollBar.thumbInsets", new Insets(2, 2, 2, 2));
            UIManager.put("ScrollBar.width", 14);
            setIconImage(Toolkit.getDefaultToolkit()
                    .getImage(CvpSpringBootApplication.class.getResource("/ic_cvp_springboot.png")));
            trayIcon = new TrayIcon(getIconImage());

            Font font = new Font("Consolas", Font.PLAIN, 12);

            JTextArea textAreaGeneral = new JTextArea();
            textAreaGeneral.setFont(font);
            ((DefaultCaret) textAreaGeneral.getCaret()).setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

            JTabbedPane tappedPane = new JTabbedPane();
            tappedPane.addTab(CvpConstants.TAB_OVERVIEW, new JPanel());
            tappedPane.addTab(CvpConstants.TAB_LOG_MONITOR, new JScrollPane(textAreaGeneral));

            JLabel uebersicht = new JLabel(CvpConstants.TAB_OVERVIEW, SwingConstants.CENTER);
            uebersicht.setPreferredSize(new Dimension(75, 35));
            tappedPane.setTabComponentAt(0, uebersicht);

            JLabel logGeneral = new JLabel(CvpConstants.TAB_LOG_MONITOR, SwingConstants.CENTER);
            logGeneral.setPreferredSize(new Dimension(75, 35));
            tappedPane.setTabComponentAt(1, logGeneral);

            Container pane = getContentPane();
            pane.add(tappedPane, BorderLayout.CENTER);

            createLayout(tappedPane);

            CvpLogger.getInstance().addTextArea(CvpConstants.TAB_LOG_MONITOR, textAreaGeneral);

            // Check the SystemTray support
            if (!SystemTray.isSupported()) {
                System.out.println("SystemTray is not supported");
                return;
            }

            final PopupMenu popup = new PopupMenu();
            final SystemTray tray = SystemTray.getSystemTray();

            // Create a popup menu components
            MenuItem openItem = new MenuItem("Open");
            Menu exportMenu = new Menu("Export");
            MenuItem exitItem = new MenuItem("Exit");

            // Add components to popup menu
            popup.add(openItem);
            popup.addSeparator();
            popup.add(exportMenu);
            popup.addSeparator();
            popup.add(exitItem);

            trayIcon.setPopupMenu(popup);
            trayIcon.setImageAutoSize(true);
            trayIcon.setToolTip(applicationName);

            trayIcon.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    tray.remove(trayIcon);
                    setVisible(true);
                    setState(Frame.NORMAL);
                }
            });
            openItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    tray.remove(trayIcon);
                    setVisible(true);
                    setState(Frame.NORMAL);
                }
            });
            exitItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    tray.remove(trayIcon);
                    System.exit(0);
                }
            });

            Handler h = new Handler(tray, trayIcon);

            addWindowStateListener(h);
            addWindowListener(h);

            setTitle("CVP Schnittstelle");
            setSize(1000, 600);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

        } catch (Exception e) {
            logger.error("Failed to initialize UI: "   e);
        }

    }

    /**
     * This Method create a Layout from JComponents
     * 
     * @param arg JComponent
     */
    private void createLayout(JComponent... arg) {

        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createSequentialGroup().addComponent(arg[0]));

        gl.setVerticalGroup(gl.createSequentialGroup().addComponent(arg[0]));
    }

    public static void main(String[] args) {

        ConfigurableApplicationContext ctx = new SpringApplicationBuilder(CvpSpringBootApplication.class)
                .headless(false).run(args);

        EventQueue.invokeLater(() -> {

            CvpSpringBootApplication ex = ctx.getBean(CvpSpringBootApplication.class);
            ex.setVisible(true);
        });

    }

    private class Handler extends WindowAdapter {
        private final SystemTray tray;
        private final TrayIcon icon;

        public Handler(SystemTray tray, TrayIcon icon) {
            super();
            this.tray = tray;
            this.icon = icon;
        }

        private void addTrayIconDisposeFrame(JFrame frame) {
            try {
                tray.add(icon);
                // frame.dispose();
                frame.setVisible(false);
                icon.displayMessage("CVP", "Application minimized to SystemTray", TrayIcon.MessageType.INFO);
            } catch (AWTException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public void windowStateChanged(WindowEvent e) {
            if (e.getNewState() == JFrame.ICONIFIED) {
                addTrayIconDisposeFrame((JFrame) e.getSource());
            }
        }

        @Override
        public void windowClosing(WindowEvent e) {

            Object[] options = { "Ja, Anwendung schließen", "Nein, nur minimieren" };
            int n = JOptionPane.showOptionDialog((JFrame) e.getSource(), "Möchten Sie die Anwendung schließen?",
                    "Anwendung schließen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, // do not use
                                                                                                            // a custom
                                                                                                            // Icon
                    options, // the titles of buttons
                    options[0]); // default button title

            if (n == 0) {
                ((JFrame) e.getSource()).dispose();
                System.exit(0);
            } else {
                addTrayIconDisposeFrame((JFrame) e.getSource());
            }
        }

        @Override
        public void windowOpened(WindowEvent e) {
            super.windowOpened(e);

            setTitle(applicationName);
            trayIcon.setToolTip(applicationName);
        }
    }

}

Exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cvpSpringBootApplication': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.rofu.cvp.CvpSpringBootApplication$$EnhancerBySpringCGLIB$$e59151e0]: Constructor threw exception; nested exception is java.awt.HeadlessException
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:315)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:181)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:132)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:248)
    at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$8(ClassBasedTestDescriptor.java:363)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:368)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$9(ClassBasedTestDescriptor.java:363)
    at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
    at java.util.stream.ReferencePipeline$2$1.accept(Unknown Source)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(Unknown Source)
    at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
    at java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(Unknown Source)
    at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Unknown Source)
    at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Unknown Source)
    at java.util.stream.ReferencePipeline$Head.forEach(Unknown Source)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:362)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$instantiateAndPostProcessTestInstance$6(ClassBasedTestDescriptor.java:283)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:282)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$4(ClassBasedTestDescriptor.java:272)
    at java.util.Optional.orElseGet(Unknown Source)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$5(ClassBasedTestDescriptor.java:271)
    at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:31)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare$0(TestMethodTestDescriptor.java:102)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:101)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:66)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$2(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:90)
    at java.util.ArrayList.forEach(Unknown Source)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    at java.util.ArrayList.forEach(Unknown Source)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
    at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invokeAllTests(JUnitPlatformProvider.java:150)
    at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:124)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
    at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)

CodePudding user response:

I solved the problem by adding the following plugin including configuration to the pom.xml.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <testFailureIgnore>true</testFailureIgnore>
            </configuration>
        </plugin>

After that I didn't get any more exceptions.

  • Related