Spring - 在Main中使用@Component自动注释GUI的正确方法

时间:2017-04-19 21:43:44

标签: java spring user-interface spring-boot autowired

我的GUI类使用@Component进行注释,如下所示:

@Component
public class AppGui {

@Autowired
private UserController userController;

private JPanel panel1;
private JButton button1;

public AppGui() {
    JFrame frame = new JFrame("App");
    frame.setContentPane(panel1);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    button1.addActionListener(event -> {
        User user = new User(1, "bogdan", "password", true);
        String fileName = "file.xml";
        try {
            userController.save(user, fileName);
            userController.findOne(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }

    });
}

我的主要课程是这样的:

@SpringBootApplication
public class SpringMarshallApplication {
    public static void main(String[] args) throws IOException {
        //configurations
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(AppConfig.class);
        context.refresh();
        new AppGui();
        SpringApplication.run(SpringMarshallApplication.class, args);
    }
}

因为GUI使用@Component注释,而我正在调用new AppGui(),所以在运行应用程序时会显示两个用户界面。 使用Spring在我的主要实例化gui的正确方法是什么?

谢谢。

修改

package com.example.controller;

import com.example.model.User;
import com.example.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * Created by bogdan.puscasu on 4/19/2017.
 */
@Controller
public class UserControllerImpl implements UserController {

    @Autowired
    private UserRepository userRepository;

    public void save(User user, String fileName) throws IOException {
        if (user != null) {
            if (!StringUtils.isEmpty(user.getUsername()) && !StringUtils.isEmpty(user.getPassword())) {
                userRepository.save(user,fileName);
            }
        }
    }

    public void findOne(String fileName) throws IOException {
        //todo: find by field
        userRepository.findOne(fileName);
    }
}

1 个答案:

答案 0 :(得分:0)

编辑: 要使Spring处理您的AppGui类而不将其作为Spring bean在XML中手动声明,您需要

将您的课程注释为@Component,就像您一样:

@Component
public class AppGui {
    [...]
}

在Spring xml配置文件中添加组件扫描指令(如果尚未存在,则添加spring context命名空间):

<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"
    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">
    [...]
    <context:component-scan base-package="com.example" />
    [...]
</beans>

但是,要从spring上下文中检索bean,可以调用:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
[...]
AppGui appGui = context.getBean(AppGui.class);

虽然使用new关键字进行直接实例化会生成一个未由Spring处理的对象。