简体   繁体   English

Swing应用程序中的Hibernate和Spring

[英]Hibernate and Spring in Swing Application

I'm a newbie in programming.I have problems in my Swing application.I suppose something wrong with session.I use Hibernate and configure it by means of Spring.When I press button I want to add info to database but I get NullPoinerException.Maybe I must code user interface another way? 我是编程的新手。我在Swing应用程序中遇到问题。我想会话有问题。我使用Hibernate并通过Spring配置它。当我按下按钮我想向数据库添加信息但我得到NullPoinerException。也许我必须以另一种方式编写用户界面? Need your help!Thanks. 需要你的帮助!谢谢。

Here my code: 这是我的代码:

MainFrame.java MainFrame.java

public class MainFrame extends JFrame {

    public MainFrame(){
        setTitle("Title");
        setSize(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        makeButtons();
        setVisible(true);
    }
    public void makeButtons(){
        JPanel panel=new JPanel();
        panel.add(makeLoginField());
        panel.add(makeLoginButton());
        panel.add(makePassField());
        panel.setVisible(true);
        this.add(panel);
    }
    public JButton makeLoginButton(){
        JButton loginButton=new JButton("Login");
        loginButton.addActionListener(new Action());
        return loginButton;
    }
    public JTextField makeLoginField(){
        JTextField loginField=new JTextField();
        loginField.setSize(new Dimension(134, 20));
        return loginField;
    }
    public JPasswordField makePassField(){
        JPasswordField passField=new JPasswordField();
        passField.setSize(new Dimension(134, 20));
        return passField;
    }
    public static void main(String[] args) {
         JFrame m=new MainFrame();   
    }   
}

Action.java Action.java

class Action implements ActionListener{
    @Autowired
    private UserServiceInterface userService;

    public void setuserService(UserServiceInterface userService) {
        this.userService=userService;
    }
    public void actionPerformed (ActionEvent e){
        User u=new User();
        u.setName("HellofromGUI");      
        userService.addUser(u);
    }
}

UserService.java UserService.java

@Transactional
public class UserService implements UserServiceInterface{
    @Autowired
    private UserDaoInterface dao;

    public void setDao(UserDaoInterface dao) {
        this.dao = dao;
    }
    public void addUser(User u){
    dao.insertRow(u);
    }
    public List getData(){
        return dao.getDBValues();
    }
}

UserDao.java UserDao.java

public class UserDao implements UserDaoInterface{
    @Autowired
    private SessionFactory sessionFactory;

    public void insertRow(User user) {
        Session session = null;
        session = sessionFactory.getCurrentSession();
        session.save(user);

    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
     public List getDBValues() {
            Session session = sessionFactory.getCurrentSession();
            List<User> users = session.createCriteria(User.class).list();
            return users;
    }
}

beans.xml beans.xml中

<beans>

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

    <bean id="userdao" class="dao.UserDao">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="userservice" class="service.UserService">
        <property name="dao">
            <ref bean="userdao" />
        </property>
    </bean>
    <bean id="paymentdao" class="dao.PaymentDao">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="paymentservice" class="service.PaymentService">
        <property name="dao">
            <ref bean="paymentdao" />
        </property> 
    </bean>
    <bean id="usergui" class="ui.Action">
        <property name="userService">
            <ref bean="userservice" />
        </property> 
    </bean>
</beans>

The important thing to remember with Spring is that it can only inject references into Spring managed beans. 要记住Spring的重要一点是它只能将引用注入Spring托管bean。 In your code, you are expecting that Spring will inject an instance of UserService into you Action class. 在您的代码中,您期望Spring会将一个UserService实例注入您的Action类。 Spring should be correctly performing this injection into the Spring bean named usergui , however, in you UI you are creating your own instance of the Action class with the following code: Spring应该正确地将这个注入执行到名为usergui的Spring bean中,但是,在您的UI中,您正在使用以下代码创建自己的Action类实例:

loginButton.addActionListener(new Action());

Anytime you create an instance of an Object yourself, it will not be managed by Spring and needs to be treated as you would any self managed object, ie, set all required references manually. 无论何时自己创建一个Object实例,它都不会被Spring管理,需要像处理任何自我管理对象一样对待,即手动设置所有必需的引用。

To get the result you are expecting, you need to changed your UI logic to reference the Spring usergui bean that you defined in your configuration file. 要获得您期望的结果,您需要更改UI逻辑以引用您在配置文件中定义的Spring usergui bean。 In order to get this instance, you first need to retrieve an instance of Spring's BeanFactory'. Here is an example of how your code can look to retrieve the correct instance of 为了获得这个实例,首先需要检索Spring的BeanFactory'. Here is an example of how your code can look to retrieve the correct instance of实例BeanFactory'. Here is an example of how your code can look to retrieve the correct instance of BeanFactory'. Here is an example of how your code can look to retrieve the correct instance of usergui`: BeanFactory'. Here is an example of how your code can look to retrieve the correct instance of usergui` BeanFactory'. Here is an example of how your code can look to retrieve the correct instance of

// using ClassPathResource, you can also use a FileResource or other method to load config
Resource  res = new ClassPathResource("/beans.xml");
// initialize bean factory
BeanFactory  factory = new XmlBeanFactory(res);        

// retrieve Spring managed Action class
ActionListener action = factory.getBean("usergui", ActionListener.class);

// configure login button
loginButton.addActionListener(action);

The sample code referenced ActionListener rather than the Action class directly. 示例代码直接引用ActionListener而不是Action类。 Typically when using Spring, you want to interact with the Interface ( ActionListener ) implemented by a Class rather than the Class itself ( Action ). 通常在使用Spring时,您希望与Class(而不是Class本身)( Action )实现的接口( ActionListener )进行交互。 Doing so allows for you to change the implementation being referenced for the bean usergui , eg, Action -> DifferentAction, without requiring you to modify your UI code. 这样做允许您更改bean usergui引用的实现,例如Action - > DifferentAction,而无需修改UI代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM