2017-04-19 57 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类。 –

相关问题