Java Reference
In-Depth Information
private JdbcTemplate jdbcTemplate;
@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void purchase(String username) {
jdbcTemplate.update(
" UPDATE BOOK_STOCK SET STOCK = STOCK - 1 " +
" WHERE ISBN = ? " ,
new Object[] { isbn });
jdbcTemplate.update(
" UPDATE ACCOUNT SET BALANCE = BALANCE - ? " +
" WHERE USERNAME = ? " ,
new Object[] { price, username });
}
}
This domain class has a purchase() method that will deduct the current book instance's stock and
the user account's balance from the database. To utilize Spring's powerful JDBC support features, you
can inject the JDBC template via setter injection.
You can use Spring's load-time weaving support to inject a JDBC template into book domain
objects. You have to annotate this class with @Configurable to declare that this type of object is
configurable in the Spring IoC container. Moreover, you can annotate the JDBC template's setter
method with @Autowired to have it auto-wired.
Spring includes an AspectJ aspect, AnnotationBeanConfigurerAspect , in its aspect library
for configuring object dependencies even if these objects are created outside the IoC container.
To enable this aspect, you just define the <context:spring-configured> element in your bean
configuration file. To weave this aspect into your domain classes at load time, you also have to define
<context:load-time-weaver> . Finally, to auto-wire the JDBC template into book domain objects via
@Autowired , you need <context:annotation-config> also.
Note To use the Spring aspect library for AspectJ in Spring 2.0 and 2.5, you have to include
spring-aspects.jar (located in the dist/weaving directory of the Spring installation) in your classpath. In
Spring 3.0, the jar's been renamed spring-instruments.jar . If you're using Maven, the following configuration
will work. Replace ${spring.version} with the 3.0 series version you want:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${spring3.version}</version>
</dependency>
Search WWH ::




Custom Search