java vs £define

lexnewgate 2014-09-24 11:56:38
在c++当中 一般用条件编译来控制代码编译 比如£define test编译测试代码 然后运行 不想运行测试代码只需要undef即可

请问java中有类似的功能或者类似的解决方案么?

ps 我想写一些测试代码 但是不想在真正程序中运行这些测试代码
...全文
211 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
youzi05 2014-09-25
  • 打赏
  • 举报
回复
如果想要写一些测试代码,可以使用Junit.相关的用法可以参见博客: http://blog.csdn.net/wangpeng047/article/details/9627527
rumlee 2014-09-25
  • 打赏
  • 举报
回复
java没有类似c++中的#ifdef #endif 但是你可以这么做,定义一个全局的boolean型属性 public static boolean debug=true; if(debug){ //to do something } 不希望执行这些调试代码的时候,只需要将debug改成false即可,或者将debug的值从配置文件获取,则只需要改动配置文件即可决定是否执行。 不过这种方法与c++的#ifdef有本质区别,#ifdef是属于条件编译,而java里面用if来解决只是条件执行。一个是编译期决定,一个是运行期决定的。
ghx287524027 2014-09-25
  • 打赏
  • 举报
回复
引用 1 楼 ghx287524027 的回复:
常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId"); 说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。 方法二:通过Spring提供的工具类获取ApplicationContext对象 代码: import org.springframework.web.context.support.WebApplicationContextUtils; ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc); ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); ac1.getBean("beanId"); ac2.getBean("beanId"); 说明: 这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。 上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。 方法三:继承自抽象类ApplicationObjectSupport 说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。 Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。 方法四:继承自抽象类WebApplicationObjectSupport 说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext 方法五:实现接口ApplicationContextAware 说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。 Spring初始化时,会通过该方法将ApplicationContext对象注入。 虽然,spring提供了后三种方法可以实现在普通的类中继承或实现相应的类或接口来获取spring 的ApplicationContext对象,但是在使用是一定要注意实现了这些类或接口的普通java类一定要在Spring 的配置文件application-context.xml文件中进行配置。否则获取的ApplicationContext对象将为null。 如下是我实现了ApplicationContextAware接口的例子 package quartz.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class SpringConfigTool implements ApplicationContextAware{//extends ApplicationObjectSupport{ private static ApplicationContext context = null; private static SpringConfigTool stools = null; public synchronized static SpringConfigTool init(){ if(stools == null){ stools = new SpringConfigTool(); } return stools; } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } public synchronized static Object getBean(String beanName) { return context.getBean(beanName); } } XML文件中的配置信息 <bean id="SpringConfigTool" class="quartz.util.SpringConfigTool"></bean> 最后提供一种不依赖于servlet,不需要注入的方式 注意一点,在服务器启动时,Spring容器初始化时,不能通过以下方法获取Spring 容器,如需细节可以观看源码org.springframework.web.context.ContextLoader Title 1 import org.springframework.web.context.ContextLoader; 2 import org.springframework.web.context.WebApplicationContext; 3 4 WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); 5 wac.getBean(beanID);
对不住,回错问题了……
ghx287524027 2014-09-25
  • 打赏
  • 举报
回复
常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId"); 说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。 方法二:通过Spring提供的工具类获取ApplicationContext对象 代码: import org.springframework.web.context.support.WebApplicationContextUtils; ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc); ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); ac1.getBean("beanId"); ac2.getBean("beanId"); 说明: 这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。 上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。 方法三:继承自抽象类ApplicationObjectSupport 说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。 Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。 方法四:继承自抽象类WebApplicationObjectSupport 说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext 方法五:实现接口ApplicationContextAware 说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。 Spring初始化时,会通过该方法将ApplicationContext对象注入。 虽然,spring提供了后三种方法可以实现在普通的类中继承或实现相应的类或接口来获取spring 的ApplicationContext对象,但是在使用是一定要注意实现了这些类或接口的普通java类一定要在Spring 的配置文件application-context.xml文件中进行配置。否则获取的ApplicationContext对象将为null。 如下是我实现了ApplicationContextAware接口的例子 package quartz.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class SpringConfigTool implements ApplicationContextAware{//extends ApplicationObjectSupport{ private static ApplicationContext context = null; private static SpringConfigTool stools = null; public synchronized static SpringConfigTool init(){ if(stools == null){ stools = new SpringConfigTool(); } return stools; } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } public synchronized static Object getBean(String beanName) { return context.getBean(beanName); } } XML文件中的配置信息 <bean id="SpringConfigTool" class="quartz.util.SpringConfigTool"></bean> 最后提供一种不依赖于servlet,不需要注入的方式 注意一点,在服务器启动时,Spring容器初始化时,不能通过以下方法获取Spring 容器,如需细节可以观看源码org.springframework.web.context.ContextLoader Title 1 import org.springframework.web.context.ContextLoader; 2 import org.springframework.web.context.WebApplicationContext; 3 4 WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); 5 wac.getBean(beanID);

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧