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? Need your help!Thanks.
Here my code:
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
class Action implements ActionListener{
@Autowired
private UserServiceInterface userService;
public void setuserServ开发者_如何学编程ice(UserServiceInterface userService) {
this.userService=userService;
}
public void actionPerformed (ActionEvent e){
User u=new User();
u.setName("HellofromGUI");
userService.addUser(u);
}
}
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
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>
<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. In your code, you are expecting that Spring will inject an instance of UserService
into you Action
class. 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:
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, i.e., set all required references manually.
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. 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
usergui`:
// 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. Typically when using Spring, you want to interact with the Interface (ActionListener
) implemented by a Class rather than the Class itself (Action
). Doing so allows for you to change the implementation being referenced for the bean usergui
, e.g., Action -> DifferentAction, without requiring you to modify your UI code.
精彩评论