0

I am new to Spring 3 and it offers a bunch of annotations, which avoids the declarative approach. What exactly is the difference between annotation based and declarative approach? Are there any downsides to annotation based?

  • might be duplicate of http://stackoverflow.com/questions/182393/xml-configuration-versus-annotation-based-configuration?rq=1 – heldt Aug 10 '12 at 06:19

1 Answers1

2

Using annotations is one way to use a declarative approach, as opposed to using a programmpatic approach, involving additional Java code in your methods:

Declarative approach:

@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);
}

Programmatic approach:

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);
        }
    });
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Shouldn't the first one be 'Annotations approach', while declarative approach == declaring via context configuration XML? – Art Licis Aug 10 '12 at 08:25
  • You give them the names you want. My point is that using annotations is still a declarative way of doing. You make the declaration in the source code using annotations rather than making it in an external XML file, but it's still declarative, because it leaves the code of the methods untouched, focused completely on business code. – JB Nizet Aug 10 '12 at 08:33
  • You are right, they both are declarative by nature. I probably made that comment driven by the original question: declarative versus annotations? In that case the correct question should be: 'declarative in XML versus declarative with annotations'. – Art Licis Aug 10 '12 at 09:05