2017-04-19 6 views
0

に@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); 
    } 
} 

答えて

0

EDIT: 春は春の豆としてXMLで手動で宣言しなくても、あなたのAppGuiクラスを扱うようにするには、

に必要@Componentなど、あなたのクラスに注釈を付けますあなたはそうでした:

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

そして、あなたのSpring x mlのコンフィギュレーション・ファイル(と春のコンテキスト・ネームスペース存在していない場合):

<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> 

しかし春コンテキストからBeanを取得するために、あなたが呼び出すことができます。

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

newとの直接インスタンス化しながら、キーワードは、Springによって処理されないオブジェクトを生成します。

+0

これは既に試してあります: '例外がスレッド" main "org.springframework.beans.factory.NoSuchBeanDefinitionException:タイプ 'com.example.gui.AppGui'の有効なBeanがありません –

+0

エラースタックトレース全体をチェックしましたか? ?これはUserControllerのオートワイヤリングに関する以前の問題が原因である可能性があります。投稿を編集して 'UserController'クラスの定義を追加できますか? – ChapL

+0

それはautowiringのせいではない、私はすでにそれをチェックした。また、UserControllerクラスを投稿しました。 –