我是Spring 3的新手,它提供了一堆注释,它避免了声明式方法。 基于注释和声明式方法之间的区别究竟是什么?注释是否有缺点?Spring声明与注释
回答
使用注释是一种方法使用声明的方式,而不是使用programmpatic方法,包括在你的方法额外的Java代码:
声明方式:
@Transactional
public void transferMoney(Long debitorId, Long creditorId, BigDecimal amount) {
Account debitor = accountDAO.findById(debitorId);
Account creditor = accountDAO.findById(creditorId);
creditor.add(amount);
debitor.remove(amount);
}
编程方法:
public void transferMoney(Long debitorId, Long creditorId, BigDecimal amount) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
Account debitor = accountDAO.findById(debitorId);
Account creditor = accountDAO.findById(creditorId);
creditor.add(amount);
debitor.remove(amount);
}
});
}
不应该第一个是'注解方法',而声明式方法==通过上下文配置XML声明? – 2012-08-10 08:25:36
你给他们你想要的名字。我的观点是使用注释仍然是一种声明方式。您使用注释在源代码中声明,而不是在外部XML文件中声明它,但它仍然是声明性的,因为它使方法的代码保持原样,完全集中在业务代码上。 – 2012-08-10 08:33:22
你是对的,它们都是自然界的声明。我可能会在原始问题的推动下发表评论:声明与注释?在这种情况下,正确的问题应该是:'在XML中声明而在注释中声明'。 – 2012-08-10 09:05:40
- 1. 多行注释声明后
- 2. Spring批注与注释
- 3. 如何在使用注释时声明Spring bean autowire-candidate =“false”?
- 4. Spring JMS - 声明式事务管理使用注释
- 5. Spring AOP与aspectj注释
- 6. 注释中的spring-restdocs字段说明
- 7. Spring @ComponentScan注释
- 8. Spring名称注释
- 9. 注释在Spring MVC
- 10. Spring MVC注释@ModelAttribute
- 11. 是否有用XML声明的Spring Mvc控制器属性的注释替代?
- 12. 在Spring框架中使用** @ Autowired **注释和接口声明的一些疑问
- 13. JPA与Spring MVC通过注释配置
- 14. 使用Spring MVC注释验证与Freemarker
- 15. Spring @RequestMapping值与名称注释
- 16. 使用@Inject注释注入xml文件中声明的bean
- 17. Java注释在注释声明中执行一个方法(用于Android)
- 18. 注解与Spring @Value元注释不注入价值
- 19. 试图使用Spring与JPA和JTA只有Spring注释
- 20. JPA/JTA/@Transactional Spring注释
- 21. Spring引导@RequestMapping注释
- 22. Spring MVC 3.1没有注释?
- 23. Spring @PreDestroy和@PostConstruct注释
- 24. Spring @ContextConfiguration注释,TestNG和IDEA
- 25. @计划的注释Spring
- 26. 如何调试Spring注释?
- 27. Spring Portlet中的@GetMapping注释
- 28. Spring 3 @Autowired注释问题
- 29. Spring注释扫描标签
- 30. Spring + Vaadin注释@Uutowired UI
可能是http://stackoverflow.com/questions/182393/xml-configuration-versus-annotation-based-configuration?rq=1 – heldt 2012-08-10 06:19:26