首页 \ 问答 \ spring测试事务回滚,抛出RuntimeException

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;//高度

相关问答

更多
  • 边框布局: 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 ...
  • 你可以通过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 ...
  • 您将两个组件添加到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()方法。 这是您的应用程序即将 ...
  • 更新:这是一个工作示例: 按钮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 ...
  • 如果真的永远不应该有一个派生类的实例,你可以使它成为一个单例 ,例如 public class MyFrame extends JFrame { private static MyFrame instance = null; private MyFrame() { // Private to prevent instantiation. } public static MyFrame getInstance() { if(instance == null) ...
  • 通常有许多方法来布局以相同效果结束的组件。 在这个例子中,我们使用一个面板来包含列中的按钮(使用GridLayout buttonContainer ),然后使用面板将该容器限制在顶部(使用BorderLayout buttonConstrainPanel ),然后使用一个容器将该面板放在左侧( ui with BorderLayout )。 它也可以使用单个GridBagLayout或GroupLayout来实现,尽管实现它的逻辑可能不那么简单。 蓝色按钮上显示的焦点边框表示鼠标单击激活按钮的位置限制。 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)