に@Componentで注釈を付けGUIをautowireする適切な方法私はこのような@Componentでアノテートされた私の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()
と呼んでいます。アプリを実行すると2つのユーザーインターフェイスが表示されます。 Spring
を使用して私のメインでguiをインスタンス化する適切な方法は何ですか?
ありがとうございます。
EDIT:
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:タイプ 'com.example.gui.AppGui'の有効なBeanがありません –
エラースタックトレース全体をチェックしましたか? ?これはUserControllerのオートワイヤリングに関する以前の問題が原因である可能性があります。投稿を編集して 'UserController'クラスの定義を追加できますか? – ChapL
それはautowiringのせいではない、私はすでにそれをチェックした。また、UserControllerクラスを投稿しました。 –