<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<context:component-scan
base-package="com.apress.prospring3.ch13.service.annotation" />
<jpa:repositories base-package="com.apress.prospring3.ch13.repository"
entity-manager-factory-ref="emf"
transaction-manager-ref="transactionManager"/>
</beans>
If you have read Chapter 10, the previous configuration should be familiar to you. First, an
embedded H2 database was defined with the database creation and data population scripts. Then,
because we are using JPA, the JpaTransactionManager bean was defined. The <tx:annotation-driven>
specifies that we are using annotations for transaction management. The EntityManagerFactory bean
was then defined, followed by the <context:component-scan> to scan the service layer classes. Finally, the
<jpa:repositories> tag was used to enable Spring Data JPA's repository abstraction.
For the implementation class of the ContactService interface, the
com.apress.prospring3.ch13.service.annotation.ContactServiceImpl class, we begin by creating the
class with an empty implementation of all the methods in the ContactService interface. In STS, it's very
easy to do it. For details, please refer to Chapter 8.
Let's implement the ContactService.findAll() method first. Listing 13-10 shows the
ContactServiceImpl class with the findAll() method implemented.
Listing 13-10. The ContactServiceImpl Class with the findAll() Method Implemented
package com.apress.prospring3.ch13.service.annotation;
// Import statement omitted
@Service("contactService")
@Repository
@Transactional
public class ContactServiceImpl implements ContactService {
@Autowired
private ContactRepository contactRepository;
@Transactional(readOnly=true)
public List<Contact> findAll() {
return Lists.newArrayList(contactRepository.findAll());
}
}
When using annotation-based transaction management, the only annotation that we need to deal
with is the @Transactional annotation. From Listing 13-10, the @Transactional annotation was applied
at the class level, which means that, by default, Spring will ensure that a transaction is present before the
execution of each method within the class. The @Transactional annotation supports a number of
attributes that you can provide to override the default behavior. Table 13-4 shows the available
attributes, together with the possible and default values.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home