spring测试事务回滚,抛出RuntimeException
我在测试spring事务回滚的时候,自己抛了个RuntimeException出来,想根据这个测试事务是否回滚,按理由junit应该捕捉到这个RuntimeException的,实际运行的时候却没捕捉到,求解? package com.bjsxt.dao.impl; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Component; import com.bjsxt.dao.LogDAO; import com.bjsxt.model.Log; @Component("logDAOImpl") public class LogDAOImpl implements LogDAO { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } @Resource public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void save(Log log){ Session session=sessionFactory.getCurrentSession(); session.save(log); throw new RuntimeException("error!"); //System.out.println("Log save!"); } } package com.bjsxt.service; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import com.bjsxt.dao.LogDAO; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.Log; import com.bjsxt.model.User; @Component("userService") public class UserService { private UserDAO userDAO; private LogDAO logDAO; public void add(User user) { userDAO.save(user); Log log=new Log(); log.setMsg("I love you!"); logDAO.save(log); } @Resource(name="userDAOImpl") public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } public UserDAO getUserDAO() { return userDAO; } @Resource(name="logDAOImpl") public void setLogDAO(LogDAO logDAO) { this.logDAO = logDAO; } public LogDAO getLogDAO() { return logDAO; } }
更新时间:2021-11-18 13:11
最满意答案
你可以通过setLocation(int x , int y);来设置JFrame窗口左上角启动的的x和y坐标。通过setSize(width, height);来设置JFrame的初始化大小。例如: public class LocationTest { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(100, 100); frame.setSize(600, 600); frame.setVisible(true); } }
其他回答
jframe.add(p);方法并不是jframe自己的,而是从java.awt.container中继承来的 之所以可以add一个jpanel,是因为jpanel 和jframe都是java.awt.container的子类 所以正确的做法是要用jframe.setcontentpane(p);的方式去添加面板
如007所说
长知识了.
Dimension screen=Toolkit.getDefaultToolkit.getScreenSize();//获取屏幕尺寸对象 Dimension myframe=this.getSize();//获取当前窗体的尺寸对象 int w=(screen.width-myframe.width)/2;//水平位置 int h=(screen.height-myframe.width)/2);//垂直位置 楼主能看懂什么意思吧,就是屏幕宽度(高度)减去窗口宽度(高度),然后在除以2,就是居中了 setLocation(w,h); 以上方法绝对行,这是我在做GUI设计的时候用到的代码!
可以:如下 假如你有个JFrame 对象 f; f.setLocationRelativeTo(null);//f启动后就在屏幕正中央了 --------------------------- 屏幕分辨率求法: int w = f.getToolkit().getScreenSize().width;//宽度 int h = f.getToolkit().getScreenSize().height;//高度
相关问答
更多-
Java的swing的布局那方面的知识[2022-03-23]
边框布局: package swing_BorderLayout; import javax.swing.*; import java.awt.*; import java.awt.event.*; class swing3 extends JFrame implements ActionListener { JPanel jp=new JPanel(); JButton jb1=new JButton("东"); JButton jb2=new JButton("南"); JButton jb3=new ... -
Java Swing里面的JFrame怎么设置默认启动位置?[2022-07-19]
你可以通过setLocation(int x , int y);来设置JFrame窗口左上角启动的的x和y坐标。通过setSize(width, height);来设置JFrame的初始化大小。例如: public class LocationTest { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fra ... -
但现在我想添加打印按钮来打印该行的记录... 那么BOND给出的建议有什么问题? 但是如果你真的想在表格的每一行都有一个单独的按钮,你可以使用表格按钮列中的渲染器/编辑器。 它允许您在列中呈现按钮并将Action与该按钮相关联。 but now i want to add print button to print records of that row... So what is wrong with the suggestion given by BOND? But if you really want ...
-
Java Swing - 鼠标进入JFrame后组件消失(Java Swing - Component Disappears After Mouse Enters JFrame)[2023-02-07]
您将两个组件添加到JFrame的contentPane中,默认情况下使用BorderLayout的容器,因此一个组件覆盖另一个组件。 我建议您更改程序结构,只需要一个JPanel即可完成所有绘制,并且您可以将gui精灵组织为逻辑和非gui组件类。 这意味着Puck和Mallot类不会扩展JPanel,但会有一个public void draw(Graphics g)方法,绘制JPanel会在其paintComponent方法中调用它。 Your adding two components to the JF ... -
使用适当的LayoutManager。 例如GridBagLayout。 或者,您可以组合多个嵌套面板,为每个面板分配自己的LayoutManager。 最糟糕的方式是将布局设置为null并使用setBounds() Use proper LayoutManager. E.g. GridBagLayout. Or you can combine multiple nested panels assigning own LayoutManager for each panel. The worst way i ...
-
Nailgun作者在这里。 安德鲁的直觉是完全正确的。 Nailgun使用SecurityManager来防止一个类的System.exit()杀死整个Nailgun VM。 如果将默认关闭操作设置为EXIT_ON_CLOSE,则关闭窗口将关闭VM并阻止将来调用,我猜你不想阻止它。 快速检查JFrame的openjdk源确认在调用setDefaultCloseOperation(EXIT_ON_CLOSE)时调用System SecurityManager的checkExit()方法。 这是您的应用程序即将 ...
-
创建Java Swing - JFrame,JRadioButton,JCheckBox(Creating Java Swing - JFrame, JRadioButton, JCheckBox)[2022-05-09]
更新:这是一个工作示例: 按钮1 http://img709.imageshack.us/img709/1640/radiobutton1.png 按钮2 http://img822.imageshack.us/img822/5313/radiobutton2.png public class MainView extends JFrame implements ActionListener{ JRadioButton radioButton1 = new JRadioButton("Button ... -
Java Swing;(Java Swing; JFrame Popup)[2021-12-16]
如果真的永远不应该有一个派生类的实例,你可以使它成为一个单例 ,例如 public class MyFrame extends JFrame { private static MyFrame instance = null; private MyFrame() { // Private to prevent instantiation. } public static MyFrame getInstance() { if(instance == null) ... -
尝试使用JFrame设置Java Button的位置不起作用?(Trying to set the location of a Java Button using JFrame isn't working?)[2021-08-25]
通常有许多方法来布局以相同效果结束的组件。 在这个例子中,我们使用一个面板来包含列中的按钮(使用GridLayout buttonContainer ),然后使用面板将该容器限制在顶部(使用BorderLayout buttonConstrainPanel ),然后使用一个容器将该面板放在左侧( ui with BorderLayout )。 它也可以使用单个GridBagLayout或GroupLayout来实现,尽管实现它的逻辑可能不那么简单。 蓝色按钮上显示的焦点边框表示鼠标单击激活按钮的位置限制。 ...