0
@Component来注解我有使用了@Component注解像这样我的GUI类有道:春天 - 自动装配GUI主
@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);
}
}
已经尝试过这一点,并且:线程中的异常“main”org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的bean类型'com.example.gui.AppGui'available' –
您是否检查了整个错误stacktrace ?这可能是由于UserController的自动装配问题。你可以编辑你的帖子来添加'UserController'类的定义吗? – ChapL
这不是因为自动装配,我已经检查过。还发布了UserController类。 –