Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring:在主方法中获取Bean失败_Java_Spring - Fatal编程技术网

Java Spring:在主方法中获取Bean失败

Java Spring:在主方法中获取Bean失败,java,spring,Java,Spring,我对Java和Spring框架都比较陌生。这是我正在构建的第一个Spring应用程序,我遇到了自动连接Bean的问题 My Application.java: @SpringBootApplication public class Application { public static void main(String[] args) throws Exception { SpringApplicationBuilder builder = new SpringApplicationB

我对Java和Spring框架都比较陌生。这是我正在构建的第一个Spring应用程序,我遇到了自动连接Bean的问题

My Application.java:

@SpringBootApplication
public class Application {

public static void main(String[] args) throws Exception {
    SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
    builder.headless(false);
    ConfigurableApplicationContext ctx = builder.run(args);
    AppRepository oAppRepo = ctx.getBean(AppRepository.class);
    // Check the SystemTray is supported
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;
    }
    CustomTray oTray = ctx.getBean(CustomTray.class);
    }
}
ressource文件夹中的My config.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:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    ">

<context:property-placeholder location="classpath:/application.properties"/>
<context:component-scan base-package="app"/>
<import resource="beans/*.xml" />
</beans>
所以我不明白为什么没有合格的Bean,因为我在tray.xml中注册了这个Bean。CustomTray类派生自TrayIcon类,创建TrayIcon除了完成之外没有什么特别的, 我的定制托盘:

public class CustomTray extends TrayIcon {

private static final String IMAGE_PATH = "/images/icon.png";
private static final String TOOLTIP = "Test";

private PopupMenu popup;
private SystemTray tray;
@Autowired private AppRepository oAppRepo;

public CustomTray() {
    super(createImage(IMAGE_PATH, TOOLTIP), TOOLTIP);
    popup = new PopupMenu();
    tray = SystemTray.getSystemTray();
    try {
        this.setup();
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@PostConstruct
private void setup() throws AWTException {

}

protected static Image createImage(String path, String description) {
    URL imageURL = CustomTray.class.getResource(path);
    if (imageURL == null) {
        System.err.println("Failed Creating Image. Resource not found: " + path);
        return null;
    } else {
        return new ImageIcon(imageURL, description).getImage();
    }
    }
}
我以前运行过这个应用程序,没有使用自动连线和bean,但现在我想重构并安装一个干净的Spring应用程序,我希望有人知道我做错了什么

编辑: 好的,现在如果我导入带有UUID提到的注释的xml文件,我会收到另一条错误消息。bean customTray有两个属性,SchedulerThread属性也应该是单例bean。它是在beans/scheduler.xml中声明的,它本身也有一个属性,应该是bean。就像这样:

scheduler.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="schedulerThread" class="app.SchedulerThread">
        <property name="processManager" value="app.ProcessManager"></property>
    </bean>
</beans>

连接这些类并创建bean的最佳方法是什么?正如我所说,我是Spring的新手,我想我不了解它与Bean和自动连接的工作方式。

在SpringBoot应用程序中,您需要使用
ImportResource
注释指定资源

@ImportResource(locations = { "classpath:config.xml", "beans/tray.xml" })

您可以使用

Arrays.stream(ctx.getBeanDefinitionNames()).forEach(System.out::println);

对我来说,解决方案是删除XML配置文件并创建AppConfig文件,现在我的Application.java如下所示:

    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
            builder.headless(false);
            builder.run(args);
        }
    }
    @Configuration
    public class AppConfig {
        @Autowired
        GridFsTemplate gridFSTemplate;

        @Autowired
        AppRepository appRepository;

        @Autowired
        UserRepository userRepository;

        @Bean
        ProcessManager processManager() {
            return new ProcessManager();
        }

        @Bean
        SchedulerService schedulerService() {
            SchedulerService schedulerService = new SchedulerService();
            SchedulerService.setProcessManager(processManager());
            SchedulerService.start();
            return schedulerService;
        }

        @Bean
        CustomTray customTray() {
            if (!SystemTray.isSupported()) {
                System.out.println("SystemTray is not supported");
                return null;
            }
            CustomTray customTray = new CustomTray();
            customTray.setSchedulerService(schedulerService());
            customTray.setAppRepository(this.appRepository);
            try {
                customTray.setup();
            } catch (AWTException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return customTray;
        }

        @Bean
        GridFSController gridFSController() {
            GridFSController gridFSController = new GridFSController();
            return gridFSController;
        }
    }
AppConfig如下所示:

    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
            builder.headless(false);
            builder.run(args);
        }
    }
    @Configuration
    public class AppConfig {
        @Autowired
        GridFsTemplate gridFSTemplate;

        @Autowired
        AppRepository appRepository;

        @Autowired
        UserRepository userRepository;

        @Bean
        ProcessManager processManager() {
            return new ProcessManager();
        }

        @Bean
        SchedulerService schedulerService() {
            SchedulerService schedulerService = new SchedulerService();
            SchedulerService.setProcessManager(processManager());
            SchedulerService.start();
            return schedulerService;
        }

        @Bean
        CustomTray customTray() {
            if (!SystemTray.isSupported()) {
                System.out.println("SystemTray is not supported");
                return null;
            }
            CustomTray customTray = new CustomTray();
            customTray.setSchedulerService(schedulerService());
            customTray.setAppRepository(this.appRepository);
            try {
                customTray.setup();
            } catch (AWTException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return customTray;
        }

        @Bean
        GridFSController gridFSController() {
            GridFSController gridFSController = new GridFSController();
            return gridFSController;
        }
    }
现在,我可以使用自动连线注释访问所有这些bean,而不需要context.getBean()之类的东西。它大大减少了代码,我认为这是更干净的解决方案

    @Configuration
    public class AppConfig {
        @Autowired
        GridFsTemplate gridFSTemplate;

        @Autowired
        AppRepository appRepository;

        @Autowired
        UserRepository userRepository;

        @Bean
        ProcessManager processManager() {
            return new ProcessManager();
        }

        @Bean
        SchedulerService schedulerService() {
            SchedulerService schedulerService = new SchedulerService();
            SchedulerService.setProcessManager(processManager());
            SchedulerService.start();
            return schedulerService;
        }

        @Bean
        CustomTray customTray() {
            if (!SystemTray.isSupported()) {
                System.out.println("SystemTray is not supported");
                return null;
            }
            CustomTray customTray = new CustomTray();
            customTray.setSchedulerService(schedulerService());
            customTray.setAppRepository(this.appRepository);
            try {
                customTray.setup();
            } catch (AWTException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return customTray;
        }

        @Bean
        GridFSController gridFSController() {
            GridFSController gridFSController = new GridFSController();
            return gridFSController;
        }
    }