假设我有一个Foo实现接口的类,如MouseListener.该MouseListener接口由五个方法组成,但我只希望覆盖其中一个(mouseClicked()).是否有一种标准的,惯用的格式化其他方法的方法?
我倾向于写下以下内容:
@Override
public void mouseClicked(MouseEvent e) {
// (...) <-- actual code here
}
@Override
public void mouseEntered(MouseEvent e) {
// Do nothing. Exists to satisfy MouseListener interface.
}
@Override
public void mouseExited(MouseEvent e) {
// Do nothing. Exists to satisfy MouseListener interface.
}
@Override
public void mousePressed(MouseEvent e) {
// Do nothing. Exists to satisfy MouseListener interface.
}
@Override
public void mouseReleased(MouseEvent e) {
// Do nothing. Exists to satisfy …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用java注释处理器.我能够使用"JavaCompiler"编写集成测试(事实上我现在正在使用"hickory").我可以运行编译过程并分析输出.问题:即使我的注释处理器中没有任何代码,单个测试也会运行大约半秒钟.这在TDD风格中使用它太长了.
嘲笑依赖关系对我来说似乎很难(我必须模拟整个"javax.lang.model.element"包).有人成功为注释处理器(Java 6)编写单元测试吗?如果不是......你的方法是什么?
使用注释控制器在基于Spring MVC的站点上组织状态消息("您的数据已成功保存/添加/删除")的最佳方法是什么?
因此,问题在于从控制器中的POST方法发送消息.
伙计们有一种方法可以将Annotation作为直接参数传递(而不是通过执行所有反射开销)?例如,在下面的代码中,我有一个包含int值的注释Number,我想作为参数传递给addImpl方法,我该怎么做(除了通过反射)?
代码片段:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
public @interface Number {
int value();
}
public void add(int x2) {
addImpl(@Number(value = 10) lol, x2);
}
public void addImpl(Number a, int b) {
System.out.println(a.value() + b);
}
public static void main(String[] args) {
new TestClass().add(3);
}
Run Code Online (Sandbox Code Playgroud) 我有点糊涂,因为我正在尝试使用@Scheduled注释,但Spring似乎没有找到我的方法.最终结果是,我的所有注释方法@Scheduled都没有被执行.
我用以下声明调用了Spring的任务魔法:
<beans> <!-- XMLNS, XSD declarations omitted for brevity -->
<context:component-scan base-package="com.mypackage"/>
<task:executor id="executor" pool-size="5"/>
<task:scheduler id="scheduler" pool-size="5"/>
<task:annotation-driven scheduler="scheduler" executor="executor"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
我有一个看起来像这样的界面:
package com.mypackage;
public interface MyInterface {
public void executePeriodically();
}
Run Code Online (Sandbox Code Playgroud)
有这样的相应impl:
package com.mypackage.impl;
// imports omitted for brevity
@Service
public class MyInterfaceImpl implements MyInterface {
@Scheduled(cron = "0/5 * * * * ?")
public void executePeriodically() {
System.out.println("The time is now : " + new Date());
}
}
Run Code Online (Sandbox Code Playgroud)
现在预期的结果是,我有一个非常吵闹的小家伙告诉我每5秒钟的时间......但实际上我什么都没有.我已尝试使用接口方法和impl方法上的注释,但这似乎没有改变任何东西.
我确定正在初始化执行程序和调度程序,因为我在日志中有以下内容:
INFO - …Run Code Online (Sandbox Code Playgroud) 定义Java接口时,可以使用类型参数声明方法,例如:
public interface ExampleInterface {
<E extends Enum<E>> Class<E> options();
}
Run Code Online (Sandbox Code Playgroud)
同样的事情在注释中不起作用.例如,这是非法的:
public @interface ExampleAnnotation {
<E extends Enum<E>> Class<E> options();
}
Run Code Online (Sandbox Code Playgroud)
我可以通过使用原始类型得到我想要的东西Enum:
public @interface ExampleAnnotation {
@SuppressWarnings("rawtypes")
Class<? extends Enum> options();
}
Run Code Online (Sandbox Code Playgroud)
究竟是什么原因导致无法使用类型参数声明注释属性?
我正在尝试按照此处的说明创建一个简单的bundle继承,并遇到路由问题.我正在使用注释进行路由.当我在AppKernel.php中注册我的子包时,我的所有父包路由都将丢失.
根据我从文档中理解的内容,Symfony2应首先从子包中查找所有文件,包括路由,然后从父包中查找.现在没有发生,只有子捆绑控制器似乎被加载.
在我的子包Bundle文件中,我按照指示实现了getParent函数,在我的routing.yml中,我有:
ParentBundle:
resource: "@Parent/Controller/"
type: annotation
prefix: /admin/
Run Code Online (Sandbox Code Playgroud)
在继承之前工作得很好.
我已经测试过系统工作正常,如果在routing.yml中包含所有控制器文件,但这似乎是非常麻烦的方式使继承工作,因为我只想覆盖父包的几个部分(不是所有控制器).
Profiler将我的两个捆绑包显示为活动状态.
其中一种情况是从Bundle中读取一个int并将其存储到由@IndDef注释限制的变量中:
public class MainActivity extends ActionBarActivity {
@IntDef({STATE_IDLE, STATE_PLAYING, STATE_RECORDING})
@Retention(RetentionPolicy.SOURCE)
public @interface State {}
public static final int STATE_IDLE = 0;
public static final int STATE_PLAYING = 1;
public static final int STATE_RECORDING = 2;
@MainActivity.State int fPlayerState = STATE_IDLE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
fPlayerState = savedInstanceState.getInt(BUNDLE_STATE); //Causes "Must be one of: ..." error
Run Code Online (Sandbox Code Playgroud)
必须有某种方法可以抑制检查或从int转换为@ MainActivity.State int,以便在最后一行设置变量.
另一种情况是编写一个负面测试,调用带有注释参数的函数故意传递错误的参数,以便测试在这种情况下抛出异常.必须有一种方法来抑制注释检查以编译这样的测试.
android annotations casting suppress android-support-library
按照春季文档
将此批注添加到
@Configuration类中以WebSecurityConfigurer通过扩展WebSecurityConfigurerAdapter基类并覆盖单个方法来定义任何或更多可能的Spring Security配置:
或者如图所示@EnableWebSecurity,用于在我们的项目中启用SpringSecurity.
但我的问题是,即使我没有注释我的任何类,@EnableWebSecurity仍有应用程序提示输入用户名和密码.(默认行为)
所以@EnableWebSecurity无论有没有,我都会收到同样的行为@EnableWebSecurity.
有人可以解释这个注释究竟是什么?
我发现,在Python都collections.Iterable和typing.Iterable在类型注释,并检查对象是否是迭代的,即可以使用,无论是isinstance(obj, collections.Iterable)和isinstance(obj, typing.Iterable)作品。我的问题是,它们之间有什么区别?在哪些情况下更喜欢哪一个?
annotations ×10
java ×6
android ×1
casting ×1
controller ×1
generics ×1
idioms ×1
interface ×1
isinstance ×1
iterable ×1
oop ×1
php ×1
python ×1
routing ×1
scheduler ×1
spring ×1
spring-boot ×1
spring-mvc ×1
suppress ×1
symfony ×1
types ×1
unit-testing ×1