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類。 –