在我们调试程序的时候, 往往会遇到在循环中要打断点, 然后进行过滤调试的情况, 这时就可以用上按条件来进行打断点调试了.

比如在 Spring 源码中有如下代码

	protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
			StringValueResolver valueResolver) {

		BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);

		String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
		for (String curName : beanNames) {
			// Check that we're not parsing our own bean definition,
			// to avoid failing on unresolvable placeholders in properties file locations.
			if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
				BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName);
				try {
					visitor.visitBeanDefinition(bd);
				}
				catch (Exception ex) {
					throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage(), ex);
				}
			}
		}

		// New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
		beanFactoryToProcess.resolveAliases(valueResolver);

		// New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
		beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);
	}

我们想在 curName 为我们项目里自定义的名字时, 才进行调试. 这时就可以如下处理:

先打上断点, 然后在断点的位置处右键, 这时就可以看到如下的提示窗, 然后在 Condition 里输入我们的表达式即可.

img

这时, 只有表达式为 true 的时候, IDE 才会暂停进入Debug状态了.