有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hibernate和Swing应用程序中的Spring

我是个编程新手。我的Swing应用程序有问题。我想会话有问题。我使用Hibernate并通过Spring配置它。当我按下按钮时,我想向数据库添加信息,但我得到了NullPoinerException。也许我必须用另一种方式编写用户界面代码? 需要你的帮助!谢谢

这是我的代码:

大型机。爪哇

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();   
    }   
}

行动。爪哇

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

用户服务。爪哇

@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。爪哇

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

豆子。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>

共 (1) 个答案

  1. # 1 楼答案

    Spring需要记住的重要一点是,它只能将引用注入到Spring管理的bean中。在代码中,您希望Spring将UserService的实例注入Action类。Spring应该正确地执行对名为usergui的Spring bean的注入,但是,在UI中,您正在使用以下代码创建自己的Action类实例:

    loginButton.addActionListener(new Action());
    

    每当您自己创建一个对象的实例时,它都不会由Spring管理,需要像对待任何自管理对象一样处理,即手动设置所有必需的引用

    为了得到预期的结果,需要更改UI逻辑,以引用配置文件中定义的Spring userguibean。为了获得这个实例,首先需要检索Spring的BeanFactory'. Here is an example of how your code can look to retrieve the correct instance ofusergui`:

    // 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);
    

    示例代码引用了ActionListener,而不是Action类。通常在使用Spring时,您希望与类实现的接口(ActionListener)交互,而不是与类本身(Action)交互。这样做允许您更改bean usergui引用的实现,例如Action->;差异化,无需修改用户界面代码