2017-04-19 105 views
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); 
    } 
} 

回答

0

編輯: 爲了讓春處理您的AppGui課,而不會在XML手動聲明爲一個Spring bean,你需要

註釋類爲@Component像你做了:

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

然後在你的Spring x中添加組件掃描指令ml的配置文件(Spring上下文的命名空間,如果不存在):

<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上下文豆,你可以撥打:

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

而直接實例與new關鍵字生成一個不被Spring處理的對象。

+0

已經嘗試過這一點,並且:線程中的異常「main」org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有可用的bean類型'com.example.gui.AppGui'available' –

+0

您是否檢查了整個錯誤stacktrace ?這可能是由於UserController的自動裝配問題。你可以編輯你的帖子來添加'UserController'類的定義嗎? – ChapL

+0

這不是因爲自動裝配,我已經檢查過。還發布了UserController類。 –