added language element to programlisting for syntax highlighting
This commit is contained in:
parent
b7ab939d55
commit
f4b4f28fc2
|
|
@ -228,23 +228,23 @@
|
|||
<para>A simple query for getting the number of rows in a
|
||||
relation.</para>
|
||||
|
||||
<programlisting>int rowCount = this.jdbcTemplate.queryForInt("select count(0) from t_accrual");</programlisting>
|
||||
<programlisting language="java">int rowCount = this.jdbcTemplate.queryForInt("select count(0) from t_accrual");</programlisting>
|
||||
|
||||
<para>A simple query using a bind variable.</para>
|
||||
|
||||
<programlisting>int countOfActorsNamedJoe = this.jdbcTemplate.queryForInt(
|
||||
<programlisting language="java">int countOfActorsNamedJoe = this.jdbcTemplate.queryForInt(
|
||||
"select count(0) from t_actors where first_name = ?", new Object[]{"Joe"});</programlisting>
|
||||
|
||||
<para>Querying for a <classname>String</classname>.</para>
|
||||
|
||||
<programlisting>String surname = (String) this.jdbcTemplate.queryForObject(
|
||||
<programlisting language="java">String surname = (String) this.jdbcTemplate.queryForObject(
|
||||
"select surname from t_actor where id = ?",
|
||||
new Object[]{new Long(1212)}, String.class);</programlisting>
|
||||
|
||||
<para>Querying and populating a <emphasis>single</emphasis> domain
|
||||
object.</para>
|
||||
|
||||
<programlisting>Actor actor = (Actor) this.jdbcTemplate.queryForObject(
|
||||
<programlisting language="java">Actor actor = (Actor) this.jdbcTemplate.queryForObject(
|
||||
"select first_name, surname from t_actor where id = ?",
|
||||
new Object[]{new Long(1212)},
|
||||
new RowMapper() {
|
||||
|
|
@ -259,7 +259,7 @@
|
|||
|
||||
<para>Querying and populating a number of domain objects.</para>
|
||||
|
||||
<programlisting>Collection actors = this.jdbcTemplate.query(
|
||||
<programlisting language="java">Collection actors = this.jdbcTemplate.query(
|
||||
"select first_name, surname from t_actor",
|
||||
new RowMapper() {
|
||||
|
||||
|
|
@ -279,7 +279,7 @@
|
|||
by DAO methods as needed. For example, the last code snippet might
|
||||
be better off written like so:</para>
|
||||
|
||||
<programlisting>public Collection findAllActors() {
|
||||
<programlisting language="java">public Collection findAllActors() {
|
||||
return this.jdbcTemplate.query( "select first_name, surname from t_actor", new ActorMapper());
|
||||
}
|
||||
|
||||
|
|
@ -297,15 +297,15 @@ private static final class ActorMapper implements RowMapper {
|
|||
<section id="jdbc-JdbcTemplate-examples-update">
|
||||
<title>Updating (INSERT/UPDATE/DELETE)</title>
|
||||
|
||||
<programlisting>this.jdbcTemplate.update(
|
||||
<programlisting language="java">this.jdbcTemplate.update(
|
||||
"insert into t_actor (first_name, surname) values (?, ?)",
|
||||
new Object[] {"Leonor", "Watling"});</programlisting>
|
||||
|
||||
<programlisting>this.jdbcTemplate.update(
|
||||
<programlisting language="java">this.jdbcTemplate.update(
|
||||
"update t_actor set weapon = ? where id = ?",
|
||||
new Object[] {"Banjo", new Long(5276)});</programlisting>
|
||||
|
||||
<programlisting>this.jdbcTemplate.update(
|
||||
<programlisting language="java">this.jdbcTemplate.update(
|
||||
"delete from actor where id = ?",
|
||||
new Object[] {new Long.valueOf(actorId)});</programlisting>
|
||||
</section>
|
||||
|
|
@ -318,13 +318,13 @@ private static final class ActorMapper implements RowMapper {
|
|||
statements. It is heavily overloaded with variants taking callback
|
||||
interfaces, binding variable arrays, and suchlike.</para>
|
||||
|
||||
<programlisting>this.jdbcTemplate.execute("create table mytable (id integer, name varchar(100))");</programlisting>
|
||||
<programlisting language="java">this.jdbcTemplate.execute("create table mytable (id integer, name varchar(100))");</programlisting>
|
||||
|
||||
<para>Invoking a simple stored procedure (more sophisticated stored
|
||||
procedure support is <link linkend="jdbc-StoredProcedure">covered
|
||||
later</link>).</para>
|
||||
|
||||
<programlisting>this.jdbcTemplate.update(
|
||||
<programlisting language="java">this.jdbcTemplate.update(
|
||||
"call SUPPORT.REFRESH_ACTORS_SUMMARY(?)",
|
||||
new Object[]{Long.valueOf(unionId)});</programlisting>
|
||||
</section>
|
||||
|
|
@ -356,7 +356,7 @@ private static final class ActorMapper implements RowMapper {
|
|||
setter for the <interfacename>DataSource</interfacename>. This leads
|
||||
to DAOs that look in part like this:</para>
|
||||
|
||||
<programlisting>public class JdbcCorporateEventDao implements CorporateEventDao {
|
||||
<programlisting language="java">public class JdbcCorporateEventDao implements CorporateEventDao {
|
||||
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
|
|
@ -369,7 +369,7 @@ private static final class ActorMapper implements RowMapper {
|
|||
|
||||
<para>The attendant configuration might look like this.</para>
|
||||
|
||||
<programlisting><?xml version="1.0" encoding="UTF-8"?>
|
||||
<programlisting language="xml"><?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
|
|
@ -428,7 +428,7 @@ private static final class ActorMapper implements RowMapper {
|
|||
the <classname>JdbcTemplate</classname> itself; namely, programming JDBC
|
||||
statements using named parameters.</para>
|
||||
|
||||
<programlisting><lineannotation>// some JDBC-backed DAO class...</lineannotation>
|
||||
<programlisting language="java"><lineannotation>// some JDBC-backed DAO class...</lineannotation>
|
||||
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
|
|
@ -458,7 +458,7 @@ public int countOfActorsByFirstName(String firstName) {
|
|||
implemented by the <classname>NamedParameterJdbcTemplate</classname>
|
||||
class) follow a similar pattern and will not be covered here.)</para>
|
||||
|
||||
<programlisting><lineannotation>// some JDBC-backed DAO class...</lineannotation>
|
||||
<programlisting language="java"><lineannotation>// some JDBC-backed DAO class...</lineannotation>
|
||||
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
|
|
@ -498,7 +498,7 @@ public int countOfActorsByFirstName(String firstName) {
|
|||
conventions</ulink>), and uses the properties of the wrapped JavaBean as
|
||||
the source of named parameter values.</para>
|
||||
|
||||
<programlisting>public class Actor {
|
||||
<programlisting language="java">public class Actor {
|
||||
|
||||
private Long id;
|
||||
private String firstName;
|
||||
|
|
@ -520,7 +520,7 @@ public int countOfActorsByFirstName(String firstName) {
|
|||
|
||||
}</programlisting>
|
||||
|
||||
<programlisting><lineannotation>// some JDBC-backed DAO class...</lineannotation>
|
||||
<programlisting language="java"><lineannotation>// some JDBC-backed DAO class...</lineannotation>
|
||||
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
|
|
@ -581,7 +581,7 @@ public int countOfActors(Actor exampleActor) {
|
|||
a code snippet that does the same job, only this time using the
|
||||
<classname>SimpleJdbcTemplate</classname>.</para>
|
||||
|
||||
<programlisting><lineannotation>// classic <classname>JdbcTemplate</classname>-style...</lineannotation>
|
||||
<programlisting language="java"><lineannotation>// classic <classname>JdbcTemplate</classname>-style...</lineannotation>
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
|
|
@ -611,7 +611,7 @@ public Actor findActor(long id) {
|
|||
<classname>SimpleJdbcTemplate</classname>; notice how much 'cleaner' the
|
||||
code is.</para>
|
||||
|
||||
<programlisting><lineannotation>// <classname>SimpleJdbcTemplate</classname>-style...</lineannotation>
|
||||
<programlisting language="java"><lineannotation>// <classname>SimpleJdbcTemplate</classname>-style...</lineannotation>
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
|
|
@ -690,7 +690,7 @@ public Actor findActor(long id) {
|
|||
used to connect to the database. Here is an example of how to configure
|
||||
a <classname>DriverManagerDataSource</classname>:</para>
|
||||
|
||||
<programlisting>DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
<programlisting language="java">DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
|
||||
dataSource.setUrl("jdbc:hsqldb:hsql://localhost:");
|
||||
dataSource.setUsername("sa");
|
||||
|
|
@ -749,7 +749,7 @@ dataSource.setPassword("");</programlisting>
|
|||
<para><classname>SQLErrorCodeSQLExceptionTranslator</classname> can be
|
||||
extended the following way:</para>
|
||||
|
||||
<programlisting>public class MySQLErrorCodesTranslator extends SQLErrorCodeSQLExceptionTranslator {
|
||||
<programlisting language="java">public class MySQLErrorCodesTranslator extends SQLErrorCodeSQLExceptionTranslator {
|
||||
|
||||
protected DataAccessException customTranslate(String task, String sql, SQLException sqlex) {
|
||||
if (sqlex.getErrorCode() == -12345) {
|
||||
|
|
@ -769,7 +769,7 @@ dataSource.setPassword("");</programlisting>
|
|||
processing where this translator is needed. Here is an example of how
|
||||
this custom translator can be used:</para>
|
||||
|
||||
<programlisting><lineannotation>// create a <classname>JdbcTemplate</classname> and set data source</lineannotation>
|
||||
<programlisting language="java"><lineannotation>// create a <classname>JdbcTemplate</classname> and set data source</lineannotation>
|
||||
JdbcTemplate jt = new JdbcTemplate();
|
||||
jt.setDataSource(dataSource);
|
||||
<lineannotation>// create a custom translator and set the <interfacename>DataSource</interfacename> for the default translation lookup</lineannotation>
|
||||
|
|
@ -799,7 +799,7 @@ su.update();</programlisting>
|
|||
what you need to include for a minimal but fully functional class that
|
||||
creates a new table.</para>
|
||||
|
||||
<programlisting>import javax.sql.DataSource;
|
||||
<programlisting language="java">import javax.sql.DataSource;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
public class ExecuteAStatement {
|
||||
|
|
@ -833,7 +833,7 @@ public class ExecuteAStatement {
|
|||
an <classname>int</classname> and one that queries for a
|
||||
<classname>String</classname>.</para>
|
||||
|
||||
<programlisting>import javax.sql.DataSource;
|
||||
<programlisting language="java">import javax.sql.DataSource;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
public class RunAQuery {
|
||||
|
|
@ -867,7 +867,7 @@ public class RunAQuery {
|
|||
above example to retrieve a list of all the rows, it would look like
|
||||
this:</para>
|
||||
|
||||
<programlisting>
|
||||
<programlisting language="java">
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
|
|
@ -893,7 +893,7 @@ public List getList() {
|
|||
objects (and thus primitives have to be wrapped in the primitive wrapper
|
||||
classes).</para>
|
||||
|
||||
<programlisting>import javax.sql.DataSource;
|
||||
<programlisting language="java">import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
|
|
@ -929,7 +929,7 @@ public class ExecuteAnUpdate {
|
|||
signature is the way it is). An example that works on Oracle and may not
|
||||
work on other platforms is:</para>
|
||||
|
||||
<programlisting>final String INSERT_SQL = "insert into my_test (name) values(?)";
|
||||
<programlisting language="java">final String INSERT_SQL = "insert into my_test (name) values(?)";
|
||||
final String name = "Rob";
|
||||
|
||||
KeyHolder keyHolder = new GeneratedKeyHolder();
|
||||
|
|
@ -1169,7 +1169,7 @@ jdbcTemplate.update(
|
|||
where we update the actor table based on entries in a list. The entire
|
||||
list is used as the batch in his example.</para>
|
||||
|
||||
<para><programlisting>public class JdbcActorDao implements ActorDao {
|
||||
<para><programlisting language="java">public class JdbcActorDao implements ActorDao {
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
|
|
@ -1220,7 +1220,7 @@ jdbcTemplate.update(
|
|||
|
||||
<para>This example shows a batch update using named parameters:</para>
|
||||
|
||||
<para><programlisting>public class JdbcActorDao implements ActorDao {
|
||||
<para><programlisting language="java">public class JdbcActorDao implements ActorDao {
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
|
|
@ -1244,7 +1244,7 @@ jdbcTemplate.update(
|
|||
|
||||
<para>The same example using classic JDBC "?" place holders:</para>
|
||||
|
||||
<para><programlisting>public class JdbcActorDao implements ActorDao {
|
||||
<para><programlisting language="java">public class JdbcActorDao implements ActorDao {
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
|
|
@ -1301,7 +1301,7 @@ jdbcTemplate.update(
|
|||
configuration methods. In this case there is only one configuration
|
||||
method used but we will see examples of multiple ones soon.</para>
|
||||
|
||||
<programlisting>public class JdbcActorDao implements ActorDao {
|
||||
<programlisting language="java">public class JdbcActorDao implements ActorDao {
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
private SimpleJdbcInsert insertActor;
|
||||
|
||||
|
|
@ -1340,7 +1340,7 @@ jdbcTemplate.update(
|
|||
generated key column using the
|
||||
<classname>usingGeneratedKeyColumns</classname> method.</para>
|
||||
|
||||
<para><programlisting>public class JdbcActorDao implements ActorDao {
|
||||
<para><programlisting language="java">public class JdbcActorDao implements ActorDao {
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
private SimpleJdbcInsert insertActor;
|
||||
|
||||
|
|
@ -1381,7 +1381,7 @@ jdbcTemplate.update(
|
|||
specifying a list of column names to be used. This is accomplished using
|
||||
the <classname>usingColumns</classname> method.</para>
|
||||
|
||||
<para><programlisting>public class JdbcActorDao implements ActorDao {
|
||||
<para><programlisting language="java">public class JdbcActorDao implements ActorDao {
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
private SimpleJdbcInsert insertActor;
|
||||
|
||||
|
|
@ -1419,7 +1419,7 @@ jdbcTemplate.update(
|
|||
contains your values. It will use the corresponding getter method to
|
||||
extract the parameter values. Here is an example:</para>
|
||||
|
||||
<para><programlisting>public class JdbcActorDao implements ActorDao {
|
||||
<para><programlisting language="java">public class JdbcActorDao implements ActorDao {
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
private SimpleJdbcInsert insertActor;
|
||||
|
||||
|
|
@ -1443,7 +1443,7 @@ jdbcTemplate.update(
|
|||
provides a more convenient <classname>addValue</classname> method that
|
||||
can be chained.</para>
|
||||
|
||||
<para><programlisting>public class JdbcActorDao implements ActorDao {
|
||||
<para><programlisting language="java">public class JdbcActorDao implements ActorDao {
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
private SimpleJdbcInsert insertActor;
|
||||
|
||||
|
|
@ -1507,7 +1507,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
|
|||
subclass and we declare it in the initialization method. For this
|
||||
example, all we need to specify is the name of the procedure.</para>
|
||||
|
||||
<para><programlisting>public class JdbcActorDao implements ActorDao {
|
||||
<para><programlisting language="java">public class JdbcActorDao implements ActorDao {
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
private SimpleJdbcCall procReadActor;
|
||||
|
||||
|
|
@ -1562,7 +1562,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
|
|||
<classname>commons-collections.jar</classname> on your classpath for
|
||||
this to work. Here is an example of this configuration:</para>
|
||||
|
||||
<para><programlisting>public class JdbcActorDao implements ActorDao {
|
||||
<para><programlisting language="java">public class JdbcActorDao implements ActorDao {
|
||||
private SimpleJdbcCall procReadActor;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
|
|
@ -1603,7 +1603,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
|
|||
<para>This is what a fully declared procedure call declaration of our
|
||||
earlier example would look like:</para>
|
||||
|
||||
<para><programlisting>public class JdbcActorDao implements ActorDao {
|
||||
<para><programlisting language="java">public class JdbcActorDao implements ActorDao {
|
||||
private SimpleJdbcCall procReadActor;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
|
|
@ -1644,7 +1644,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
|
|||
<classname>java.sql.Types</classname> constants. We have already seen
|
||||
declarations like:</para>
|
||||
|
||||
<para><programlisting> new SqlParameter("in_id", Types.NUMERIC),
|
||||
<para><programlisting language="java"> new SqlParameter("in_id", Types.NUMERIC),
|
||||
new SqlOutParameter("out_first_name", Types.VARCHAR),</programlisting></para>
|
||||
|
||||
<para>The first line with the <classname>SqlParameter</classname>
|
||||
|
|
@ -1710,7 +1710,7 @@ END;</programlisting></para>
|
|||
<classname>SimpleJdbcCall</classname> in the initialization
|
||||
method.</para>
|
||||
|
||||
<para><programlisting>public class JdbcActorDao implements ActorDao {
|
||||
<para><programlisting language="java">public class JdbcActorDao implements ActorDao {
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
private SimpleJdbcCall funcGetActorName;
|
||||
|
||||
|
|
@ -1768,7 +1768,7 @@ END;</programlisting>In order to call this procedure we need to declare the
|
|||
created by passing in the required class to map to in the
|
||||
<classname>newInstance</classname> method.</para>
|
||||
|
||||
<para><programlisting>public class JdbcActorDao implements ActorDao {
|
||||
<para><programlisting language="java">public class JdbcActorDao implements ActorDao {
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
private SimpleJdbcCall procReadAllActors;
|
||||
|
||||
|
|
@ -1852,7 +1852,7 @@ END;</programlisting>In order to call this procedure we need to declare the
|
|||
customer relation to an instance of the <classname>Customer</classname>
|
||||
class.</para>
|
||||
|
||||
<programlisting>private class CustomerMappingQuery extends MappingSqlQuery {
|
||||
<programlisting language="java">private class CustomerMappingQuery extends MappingSqlQuery {
|
||||
|
||||
public CustomerMappingQuery(DataSource ds) {
|
||||
super(ds, "SELECT id, name FROM customer WHERE id = ?");
|
||||
|
|
@ -1883,7 +1883,7 @@ END;</programlisting>In order to call this procedure we need to declare the
|
|||
have been defined we call the <literal>compile()</literal> method so the
|
||||
statement can be prepared and later be executed.</para>
|
||||
|
||||
<programlisting>public Customer getCustomer(Integer id) {
|
||||
<programlisting language="java">public Customer getCustomer(Integer id) {
|
||||
CustomerMappingQuery custQry = new CustomerMappingQuery(dataSource);
|
||||
Object[] parms = new Object[1];
|
||||
parms[0] = id;
|
||||
|
|
@ -1921,7 +1921,7 @@ END;</programlisting>In order to call this procedure we need to declare the
|
|||
custom update method) it can easily be parameterized by setting SQL and
|
||||
declaring parameters.</para>
|
||||
|
||||
<programlisting>import java.sql.Types;
|
||||
<programlisting language="java">import java.sql.Types;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
|
|
@ -1972,7 +1972,7 @@ public class UpdateCreditRating extends SqlUpdate {
|
|||
SQL type is specified using the <classname>java.sql.Types</classname>
|
||||
constants. We have already seen declarations like:</para>
|
||||
|
||||
<para><programlisting> new SqlParameter("in_id", Types.NUMERIC),
|
||||
<para><programlisting language="java"> new SqlParameter("in_id", Types.NUMERIC),
|
||||
new SqlOutParameter("out_first_name", Types.VARCHAR),</programlisting></para>
|
||||
|
||||
<para>The first line with the <classname>SqlParameter</classname>
|
||||
|
|
@ -2012,7 +2012,7 @@ public class UpdateCreditRating extends SqlUpdate {
|
|||
<literal>execute()</literal> method returns a map with an entry for each
|
||||
declared output parameter using the parameter name as the key.</para>
|
||||
|
||||
<programlisting>import java.sql.Types;
|
||||
<programlisting language="java">import java.sql.Types;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
|
@ -2071,7 +2071,7 @@ public class TestStoredProcedure {
|
|||
<para>Find below an example of a <classname>StoredProcedure</classname>
|
||||
that has two output parameters (in this case Oracle REF cursors).</para>
|
||||
|
||||
<programlisting>import oracle.jdbc.driver.OracleTypes;
|
||||
<programlisting language="java">import oracle.jdbc.driver.OracleTypes;
|
||||
import org.springframework.jdbc.core.SqlOutParameter;
|
||||
import org.springframework.jdbc.object.StoredProcedure;
|
||||
|
||||
|
|
@ -2110,7 +2110,7 @@ public class TitlesAndGenresStoredProcedure extends StoredProcedure {
|
|||
<classname>Title</classname> domain object for each row in the supplied
|
||||
<interfacename>ResultSet</interfacename>.</para>
|
||||
|
||||
<programlisting>import com.foo.sprocs.domain.Title;
|
||||
<programlisting language="java">import com.foo.sprocs.domain.Title;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
|
|
@ -2131,7 +2131,7 @@ public final class TitleMapper implements RowMapper {
|
|||
<classname>Genre</classname> domain object for each row in the supplied
|
||||
<interfacename>ResultSet</interfacename>.</para>
|
||||
|
||||
<programlisting>import org.springframework.jdbc.core.RowMapper;
|
||||
<programlisting language="java">import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
|
@ -2152,7 +2152,7 @@ public final class GenreMapper implements RowMapper {
|
|||
superclass' (untyped) <literal>execute(Map parameters)</literal> (which
|
||||
has <literal>protected</literal> access); for example:</para>
|
||||
|
||||
<programlisting>import oracle.jdbc.driver.OracleTypes;
|
||||
<programlisting language="java">import oracle.jdbc.driver.OracleTypes;
|
||||
import org.springframework.jdbc.core.SqlOutParameter;
|
||||
import org.springframework.jdbc.object.StoredProcedure;
|
||||
|
||||
|
|
@ -2208,7 +2208,7 @@ public class TitlesAfterDateStoredProcedure extends StoredProcedure {
|
|||
the appropriate run method repeatedly to execute the function. Here is
|
||||
an example of retrieving the count of rows from a table:</para>
|
||||
|
||||
<programlisting>public int countRows() {
|
||||
<programlisting language="java">public int countRows() {
|
||||
SqlFunction sf = new SqlFunction(dataSource, "select count(*) from mytable");
|
||||
sf.compile();
|
||||
return sf.run();
|
||||
|
|
@ -2343,7 +2343,7 @@ public class TitlesAfterDateStoredProcedure extends StoredProcedure {
|
|||
<area coords="13" id="jdbc.lobhandler.setBlob" />
|
||||
</areaspec>
|
||||
|
||||
<programlisting>final File blobIn = new File("spring2004.jpg");
|
||||
<programlisting language="java">final File blobIn = new File("spring2004.jpg");
|
||||
final InputStream blobIs = new FileInputStream(blobIn);
|
||||
final File clobIn = new File("large.txt");
|
||||
final InputStream clobIs = new FileInputStream(clobIn);
|
||||
|
|
@ -2392,7 +2392,7 @@ clobReader.close();</programlisting>
|
|||
<area coords="7" id="jdbc.lobhandler.getBlob" />
|
||||
</areaspec>
|
||||
|
||||
<programlisting>List l = jdbcTemplate.query("select id, a_clob, a_blob from lob_table",
|
||||
<programlisting language="java">List l = jdbcTemplate.query("select id, a_clob, a_blob from lob_table",
|
||||
new RowMapper() {
|
||||
public Object mapRow(ResultSet rs, int i) throws SQLException {
|
||||
Map results = new HashMap();
|
||||
|
|
@ -2471,7 +2471,7 @@ clobReader.close();</programlisting>
|
|||
interface is used as part of the declaration of an
|
||||
<classname>SqlOutParameter</classname>.</para>
|
||||
|
||||
<para><programlisting>declareParameter(new SqlOutParameter("item", OracleTypes.STRUCT, "ITEM_TYPE",
|
||||
<para><programlisting language="java">declareParameter(new SqlOutParameter("item", OracleTypes.STRUCT, "ITEM_TYPE",
|
||||
new SqlReturnType() {
|
||||
public Object getTypeValue(CallableStatement cs, int colIndx, int sqlType, String typeName)
|
||||
throws SQLException {
|
||||
|
|
@ -2492,7 +2492,7 @@ clobReader.close();</programlisting>
|
|||
specific objects like <classname>StructDescriptor</classname>s or
|
||||
<classname>ArrayDescriptor</classname>s</para>
|
||||
|
||||
<para><programlisting>SqlTypeValue value = new AbstractSqlTypeValue() {
|
||||
<para><programlisting language="java">SqlTypeValue value = new AbstractSqlTypeValue() {
|
||||
protected Object createTypeValue(Connection conn, int sqlType, String typeName) throws SQLException {
|
||||
StructDescriptor itemDescriptor = new StructDescriptor(typeName, conn);
|
||||
Struct item = new STRUCT(itemDescriptor, conn,
|
||||
|
|
|
|||
|
|
@ -192,7 +192,7 @@
|
|||
JDBC <classname>DataSource</classname> and a Hibernate
|
||||
<interfacename>SessionFactory</interfacename> on top of it:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
|
||||
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
|
||||
|
|
@ -222,7 +222,7 @@
|
|||
<interfacename>DataSource</interfacename> (usually managed by an
|
||||
application server) is just a matter of configuration:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
|
||||
<property name="jndiName" value="java:comp/env/jdbc/myds"/>
|
||||
|
|
@ -251,7 +251,7 @@
|
|||
<interfacename>SessionFactory</interfacename>, and an example for a DAO
|
||||
method implementation.</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myProductDao" class="product.ProductDaoImpl">
|
||||
<property name="sessionFactory" ref="mySessionFactory"/>
|
||||
|
|
@ -259,7 +259,7 @@
|
|||
|
||||
</beans></programlisting>
|
||||
|
||||
<programlisting>public class ProductDaoImpl implements ProductDao {
|
||||
<programlisting language="java">public class ProductDaoImpl implements ProductDao {
|
||||
|
||||
private HibernateTemplate hibernateTemplate;
|
||||
|
||||
|
|
@ -280,7 +280,7 @@
|
|||
that are not exposed on the <classname>HibernateTemplate</classname>,
|
||||
you can always drop down to a callback-based approach like so.</para>
|
||||
|
||||
<programlisting>public class ProductDaoImpl implements ProductDao {
|
||||
<programlisting language="java">public class ProductDaoImpl implements ProductDao {
|
||||
|
||||
private HibernateTemplate hibernateTemplate;
|
||||
|
||||
|
|
@ -319,7 +319,7 @@
|
|||
combination, this allows for very simple DAO implementations for typical
|
||||
requirements:</para>
|
||||
|
||||
<programlisting>public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao {
|
||||
<programlisting language="java">public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao {
|
||||
|
||||
public Collection loadProductsByCategory(String category) throws DataAccessException {
|
||||
return this.getHibernateTemplate().find(
|
||||
|
|
@ -349,7 +349,7 @@
|
|||
<interfacename>Session</interfacename>, as its lifecycle is managed by
|
||||
the transaction).</para>
|
||||
|
||||
<programlisting>public class HibernateProductDao extends HibernateDaoSupport implements ProductDao {
|
||||
<programlisting language="java">public class HibernateProductDao extends HibernateDaoSupport implements ProductDao {
|
||||
|
||||
public Collection loadProductsByCategory(String category) throws DataAccessException, MyException {
|
||||
Session session = getSession(false);
|
||||
|
|
@ -391,7 +391,7 @@
|
|||
DAO implementation looks like as follows, based on the plain Hibernate
|
||||
API:</para>
|
||||
|
||||
<programlisting>public class ProductDaoImpl implements ProductDao {
|
||||
<programlisting language="java">public class ProductDaoImpl implements ProductDao {
|
||||
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
|
|
@ -424,7 +424,7 @@
|
|||
with the desired factory reference. As a Spring bean definition, it
|
||||
would look as follows:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myProductDao" class="product.ProductDaoImpl">
|
||||
<property name="sessionFactory" ref="mySessionFactory"/>
|
||||
|
|
@ -480,7 +480,7 @@
|
|||
a Spring application context, and an example for a business method
|
||||
implementation.</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
|
||||
<property name="sessionFactory" ref="mySessionFactory"/>
|
||||
|
|
@ -493,7 +493,7 @@
|
|||
|
||||
</beans></programlisting>
|
||||
|
||||
<programlisting>public class ProductServiceImpl implements ProductService {
|
||||
<programlisting language="java">public class ProductServiceImpl implements ProductService {
|
||||
|
||||
private TransactionTemplate transactionTemplate;
|
||||
private ProductDao productDao;
|
||||
|
|
@ -533,7 +533,7 @@
|
|||
configuration file and do not affect the business service
|
||||
implementations.</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
|
||||
<property name="sessionFactory" ref="mySessionFactory"/>
|
||||
|
|
@ -555,7 +555,7 @@
|
|||
|
||||
</beans></programlisting>
|
||||
|
||||
<programlisting>public class ProductServiceImpl implements ProductService {
|
||||
<programlisting language="java">public class ProductServiceImpl implements ProductService {
|
||||
|
||||
private ProductDao productDao;
|
||||
|
||||
|
|
@ -593,7 +593,7 @@
|
|||
have not done so already prior to continuing.</para>
|
||||
</note>
|
||||
|
||||
<programlisting><?xml version="1.0" encoding="UTF-8"?>
|
||||
<programlisting language="xml"><?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
|
|
@ -663,7 +663,7 @@
|
|||
factories without special regard, as long as it is using
|
||||
<classname>JtaTransactionManager</classname> as the strategy.</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myDataSource1" class="org.springframework.jndi.JndiObjectFactoryBean">
|
||||
<property name="jndiName value="java:comp/env/jdbc/myds1"/>
|
||||
|
|
@ -963,7 +963,7 @@
|
|||
<interfacename>PersistenceManagerFactory</interfacename> within a Spring
|
||||
application context:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myPmf" class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean">
|
||||
<property name="configLocation" value="classpath:kodo.properties"/>
|
||||
|
|
@ -984,7 +984,7 @@
|
|||
"connectionFactory" property. For example, for the open source JDO
|
||||
implementation JPOX (<ulink url="http://www.jpox.org"></ulink>):</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
|
||||
<property name="driverClassName" value="${jdbc.driverClassName}"/>
|
||||
|
|
@ -1025,7 +1025,7 @@
|
|||
usually rather be used with the Spring Framework's
|
||||
<classname>JdoTemplate</classname>:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myProductDao" class="product.ProductDaoImpl">
|
||||
<property name="persistenceManagerFactory" ref="myPmf"/>
|
||||
|
|
@ -1033,7 +1033,7 @@
|
|||
|
||||
</beans></programlisting>
|
||||
|
||||
<programlisting>public class ProductDaoImpl implements ProductDao {
|
||||
<programlisting language="java">public class ProductDaoImpl implements ProductDao {
|
||||
|
||||
private JdoTemplate jdoTemplate;
|
||||
|
||||
|
|
@ -1073,7 +1073,7 @@
|
|||
combination, this allows for very simple DAO implementations for typical
|
||||
requirements:</para>
|
||||
|
||||
<programlisting>public class ProductDaoImpl extends JdoDaoSupport implements ProductDao {
|
||||
<programlisting language="java">public class ProductDaoImpl extends JdoDaoSupport implements ProductDao {
|
||||
|
||||
public Collection loadProductsByCategory(String category) throws DataAccessException {
|
||||
return getJdoTemplate().find(
|
||||
|
|
@ -1101,7 +1101,7 @@
|
|||
<interfacename>PersistenceManagerFactory</interfacename>. A
|
||||
corresponding DAO implementation looks like as follows:</para>
|
||||
|
||||
<programlisting>public class ProductDaoImpl implements ProductDao {
|
||||
<programlisting language="java">public class ProductDaoImpl implements ProductDao {
|
||||
|
||||
private PersistenceManagerFactory persistenceManagerFactory;
|
||||
|
||||
|
|
@ -1126,7 +1126,7 @@
|
|||
it still fits nicely into a Spring container, just like it would if
|
||||
coded against Spring's <classname>JdoTemplate</classname>:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myProductDao" class="product.ProductDaoImpl">
|
||||
<property name="persistenceManagerFactory" ref="myPmf"/>
|
||||
|
|
@ -1143,7 +1143,7 @@
|
|||
<interfacename>PersistenceManagerFactory</interfacename>, passing the
|
||||
proxy into your DAOs.</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myPmfProxy"
|
||||
class="org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy">
|
||||
|
|
@ -1173,7 +1173,7 @@
|
|||
call and thus the entire <literal>finally</literal> block, which you
|
||||
might prefer to keep your DAO implementations concise:</para>
|
||||
|
||||
<programlisting>public class ProductDaoImpl implements ProductDao {
|
||||
<programlisting language="java">public class ProductDaoImpl implements ProductDao {
|
||||
|
||||
private PersistenceManagerFactory persistenceManagerFactory;
|
||||
|
||||
|
|
@ -1194,7 +1194,7 @@
|
|||
<classname>TransactionAwarePersistenceManagerFactoryProxy</classname>'s
|
||||
"allowCreate" flag off:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myPmfProxy"
|
||||
class="org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy">
|
||||
|
|
@ -1238,7 +1238,7 @@
|
|||
<para>To execute service operations within transactions, you can use
|
||||
Spring's common declarative transaction facilities. For example:</para>
|
||||
|
||||
<programlisting><?xml version="1.0" encoding="UTF-8"?>
|
||||
<programlisting language="xml"><?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
|
|
@ -1375,7 +1375,7 @@
|
|||
Spring-managed JDBC <interfacename>DataSource</interfacename> to
|
||||
use.</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
|
||||
<property name="driverClassName" value="${jdbc.driverClassName}"/>
|
||||
|
|
@ -1391,7 +1391,7 @@
|
|||
|
||||
</beans></programlisting>
|
||||
|
||||
<programlisting><toplink-configuration>
|
||||
<programlisting language="xml"><toplink-configuration>
|
||||
|
||||
<session>
|
||||
<name>Session</name>
|
||||
|
|
@ -1428,7 +1428,7 @@
|
|||
<interfacename>SessionFactory</interfacename>, but will usually rather
|
||||
be used with Spring's <literal>TopLinkTemplate</literal>:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myProductDao" class="product.ProductDaoImpl">
|
||||
<property name="sessionFactory" ref="mySessionFactory"/>
|
||||
|
|
@ -1436,7 +1436,7 @@
|
|||
|
||||
</beans></programlisting>
|
||||
|
||||
<programlisting>public class TopLinkProductDao implements ProductDao {
|
||||
<programlisting language="java">public class TopLinkProductDao implements ProductDao {
|
||||
|
||||
private TopLinkTemplate tlTemplate;
|
||||
|
||||
|
|
@ -1482,7 +1482,7 @@
|
|||
combination, this allows for simple DAO implementations for typical
|
||||
requirements:</para>
|
||||
|
||||
<programlisting>public class ProductDaoImpl extends TopLinkDaoSupport implements ProductDao {
|
||||
<programlisting language="java">public class ProductDaoImpl extends TopLinkDaoSupport implements ProductDao {
|
||||
|
||||
public Collection loadProductsByCategory(String category) throws DataAccessException {
|
||||
ReadAllQuery findOwnersQuery = new ReadAllQuery(Product.class);
|
||||
|
|
@ -1535,7 +1535,7 @@
|
|||
|
||||
<para>A corresponding DAO implementation looks like as follows:</para>
|
||||
|
||||
<programlisting>public class ProductDaoImpl implements ProductDao {
|
||||
<programlisting language="java">public class ProductDaoImpl implements ProductDao {
|
||||
|
||||
private Session session;
|
||||
|
||||
|
|
@ -1564,7 +1564,7 @@
|
|||
bean reference of type <interfacename>Session</interfacename>, to be
|
||||
passed into the DAO:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="mySessionAdapter"
|
||||
class="org.springframework.orm.toplink.support.TransactionAwareSessionAdapter">
|
||||
|
|
@ -1627,7 +1627,7 @@
|
|||
<para>To execute service operations within transactions, you can use
|
||||
Spring's common declarative transaction facilities. For example:</para>
|
||||
|
||||
<programlisting><?xml version="1.0" encoding="UTF-8"?>
|
||||
<programlisting language="xml"><?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
|
|
@ -1730,7 +1730,7 @@
|
|||
need to create the following SQL map
|
||||
<filename>'Account.xml'</filename>:</para>
|
||||
|
||||
<programlisting><sqlMap namespace="Account">
|
||||
<programlisting language="xml"><sqlMap namespace="Account">
|
||||
|
||||
<resultMap id="result" class="examples.Account">
|
||||
<result property="name" column="NAME" columnIndex="1"/>
|
||||
|
|
@ -1751,7 +1751,7 @@
|
|||
|
||||
<para>The configuration file for iBATIS 2 looks like this:</para>
|
||||
|
||||
<programlisting><sqlMapConfig>
|
||||
<programlisting language="xml"><sqlMapConfig>
|
||||
|
||||
<sqlMap resource="example/Account.xml"/>
|
||||
|
||||
|
|
@ -1767,7 +1767,7 @@
|
|||
<classname>SqlMapClientFactoryBean</classname>, which enables lazy
|
||||
loading.</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
|
||||
<property name="driverClassName" value="${jdbc.driverClassName}"/>
|
||||
|
|
@ -1792,7 +1792,7 @@
|
|||
supporting class similar to the <classname>SqlMapDaoSupport</classname>.
|
||||
We extend it to implement our DAO:</para>
|
||||
|
||||
<programlisting>public class SqlMapAccountDao extends SqlMapClientDaoSupport implements AccountDao {
|
||||
<programlisting language="java">public class SqlMapAccountDao extends SqlMapClientDaoSupport implements AccountDao {
|
||||
|
||||
public Account getAccount(String email) throws DataAccessException {
|
||||
return (Account) getSqlMapClientTemplate().queryForObject("getAccountByEmail", email);
|
||||
|
|
@ -1809,7 +1809,7 @@
|
|||
application context and wiring it with our
|
||||
<literal>SqlMapClient</literal> instance:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="accountDao" class="example.SqlMapAccountDao">
|
||||
<property name="sqlMapClient" ref="sqlMapClient"/>
|
||||
|
|
@ -1828,7 +1828,7 @@
|
|||
<literal>SqlMapClientCallback</literal> implementation as argument. This
|
||||
can, for example, be used for batching:</para>
|
||||
|
||||
<programlisting>public class SqlMapAccountDao extends SqlMapClientDaoSupport implements AccountDao {
|
||||
<programlisting language="java">public class SqlMapAccountDao extends SqlMapClientDaoSupport implements AccountDao {
|
||||
|
||||
public void insertAccount(Account account) throws DataAccessException {
|
||||
getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
|
||||
|
|
@ -1857,7 +1857,7 @@
|
|||
<literal>SqlMapClient</literal>. A corresponding DAO implementation
|
||||
looks like as follows:</para>
|
||||
|
||||
<programlisting>public class SqlMapAccountDao implements AccountDao {
|
||||
<programlisting language="java">public class SqlMapAccountDao implements AccountDao {
|
||||
|
||||
private SqlMapClient sqlMapClient;
|
||||
|
||||
|
|
@ -1891,7 +1891,7 @@
|
|||
that the plain iBATIS-based DAO still follows the Dependency Injection
|
||||
pattern:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="accountDao" class="example.SqlMapAccountDao">
|
||||
<property name="sqlMapClient" ref="sqlMapClient"/>
|
||||
|
|
@ -1929,7 +1929,7 @@
|
|||
and, in most cases, requires only the persistence unit name to be
|
||||
specified:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myEmf" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
|
||||
<property name="persistenceUnitName" value="myPersistenceUnit"/>
|
||||
|
|
@ -1959,7 +1959,7 @@
|
|||
from JNDI (for example in a Java EE 5 environment), is just a matter
|
||||
of changing the XML configuration:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<jee:jndi-lookup id="myEmf" jndi-name="persistence/myPersistenceUnit"/>
|
||||
|
||||
|
|
@ -2013,7 +2013,7 @@
|
|||
custom DataSources outside of JNDI and to control the weaving
|
||||
process.</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
<property name="dataSource" ref="someDataSource"/>
|
||||
|
|
@ -2026,7 +2026,7 @@
|
|||
|
||||
<para>A typical <literal>persistence.xml</literal> file looks as follows:</para>
|
||||
|
||||
<programlisting><persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
|
||||
<programlisting language="xml"><persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
|
||||
|
||||
<persistence-unit name="myUnit" transaction-type="RESOURCE_LOCAL">
|
||||
<mapping-file>META-INF/orm.xml</mapping-file>
|
||||
|
|
@ -2144,7 +2144,7 @@
|
|||
of the default one) by editing the web application context
|
||||
file:</para>
|
||||
|
||||
<programlisting><Context path="/myWebApp" docBase="/my/webApp/location">
|
||||
<programlisting language="xml"><Context path="/myWebApp" docBase="/my/webApp/location">
|
||||
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
|
||||
</Context></programlisting>
|
||||
|
||||
|
|
@ -2175,7 +2175,7 @@
|
|||
|
||||
<para>If you are using Tomcat 5.5.20+ you can set
|
||||
<emphasis>useSystemClassLoaderAsParent</emphasis> to
|
||||
<literal>false</literal> to fix the problem: <programlisting><Context path="/myWebApp" docBase="/my/webApp/location">
|
||||
<literal>false</literal> to fix the problem: <programlisting language="xml"><Context path="/myWebApp" docBase="/my/webApp/location">
|
||||
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"
|
||||
useSystemClassLoaderAsParent="false"/>
|
||||
</Context></programlisting></para>
|
||||
|
|
@ -2199,7 +2199,7 @@
|
|||
of the default one) by editing the web application context
|
||||
file:</para>
|
||||
|
||||
<programlisting><Context path="/myWebApp" docBase="/my/webApp/location">
|
||||
<programlisting language="xml"><Context path="/myWebApp" docBase="/my/webApp/location">
|
||||
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
|
||||
</Context></programlisting>
|
||||
|
||||
|
|
@ -2227,7 +2227,7 @@
|
|||
configuring
|
||||
<classname>LocalContainerEntityManagerFactoryBean</classname>:</para>
|
||||
|
||||
<programlisting><bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
<programlisting language="xml"><bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
<property name="loadTimeWeaver">
|
||||
<bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
|
||||
</property>
|
||||
|
|
@ -2258,7 +2258,7 @@
|
|||
a Spring-specific (but very general) VM agent (<filename
|
||||
class="libraryfile">spring-agent.jar</filename>):</para>
|
||||
|
||||
<programlisting><bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
<programlisting language="xml"><bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
<property name="loadTimeWeaver">
|
||||
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
|
||||
</property>
|
||||
|
|
@ -2282,7 +2282,7 @@
|
|||
autodetection of the platform (WebLogic, OC4J, GlassFish, Tomcat, Resin, VM agent)
|
||||
as well as automatic propagation of the weaver to all weaver-aware beans.</para>
|
||||
|
||||
<programlisting><context:load-time-weaver/>
|
||||
<programlisting language="xml"><context:load-time-weaver/>
|
||||
|
||||
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
...
|
||||
|
|
@ -2309,7 +2309,7 @@
|
|||
parsed and later on retrieved through the persistence unit
|
||||
name:</para>
|
||||
|
||||
<programlisting><bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
|
||||
<programlisting language="xml"><bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
|
||||
<property name="persistenceXmlLocation">
|
||||
<list>
|
||||
<value>org/springframework/orm/jpa/domain/persistence-multi.xml</value>
|
||||
|
|
@ -2353,7 +2353,7 @@
|
|||
given <interfacename>EntityManagerFactory</interfacename> or through
|
||||
Spring's <classname>JpaTemplate</classname>:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="myProductDao" class="product.ProductDaoImpl">
|
||||
<property name="entityManagerFactory" ref="myEmf"/>
|
||||
|
|
@ -2361,7 +2361,7 @@
|
|||
|
||||
</beans></programlisting>
|
||||
|
||||
<programlisting>public class JpaProductDao implements ProductDao {
|
||||
<programlisting language="java">public class JpaProductDao implements ProductDao {
|
||||
|
||||
private JpaTemplate jpaTemplate;
|
||||
|
||||
|
|
@ -2401,7 +2401,7 @@
|
|||
<methodname>getJpaTemplate()</methodname> to be used by
|
||||
subclasses:</para>
|
||||
|
||||
<programlisting>public class ProductDaoImpl extends JpaDaoSupport implements ProductDao {
|
||||
<programlisting language="java">public class ProductDaoImpl extends JpaDaoSupport implements ProductDao {
|
||||
|
||||
public Collection loadProductsByCategory(String category) throws DataAccessException {
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
|
|
@ -2457,7 +2457,7 @@
|
|||
<classname>PersistenceAnnotationBeanPostProcessor</classname> is
|
||||
enabled. A corresponding DAO implementation might look like this:</para>
|
||||
|
||||
<programlisting>public class ProductDaoImpl implements ProductDao {
|
||||
<programlisting language="java">public class ProductDaoImpl implements ProductDao {
|
||||
|
||||
private EntityManagerFactory emf;
|
||||
|
||||
|
|
@ -2487,7 +2487,7 @@
|
|||
advantage of annotations to require the injection of the default
|
||||
<interfacename>EntityManagerFactory</interfacename>:</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<lineannotation><!-- bean post-processor for JPA annotations --></lineannotation>
|
||||
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
|
||||
|
|
@ -2505,7 +2505,7 @@
|
|||
(including <classname>CommonAnnotationBeanPostProcessor</classname>
|
||||
etc).</para>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<lineannotation><!-- post-processors for all standard config annotations --></lineannotation>
|
||||
<context:annotation-config/>
|
||||
|
|
@ -2522,7 +2522,7 @@
|
|||
transactional EntityManager) to be injected instead of the
|
||||
factory:</para>
|
||||
|
||||
<programlisting>public class ProductDaoImpl implements ProductDao {
|
||||
<programlisting language="java">public class ProductDaoImpl implements ProductDao {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
|
@ -2597,14 +2597,14 @@
|
|||
exception translation to be applied transparently through the
|
||||
<interfacename>@Repository</interfacename> annotation:</para>
|
||||
|
||||
<programlisting>@Repository
|
||||
<programlisting language="java">@Repository
|
||||
public class ProductDaoImpl implements ProductDao {
|
||||
|
||||
<lineannotation>// class body here...</lineannotation>
|
||||
|
||||
}</programlisting>
|
||||
|
||||
<programlisting><beans>
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<lineannotation><!-- <classname>Exception</classname> translation bean post processor --></lineannotation>
|
||||
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
|
||||
|
|
@ -2635,7 +2635,7 @@ public class ProductDaoImpl implements ProductDao {
|
|||
<para>To execute service operations within transactions, you can use
|
||||
Spring's common declarative transaction facilities. For example:</para>
|
||||
|
||||
<programlisting><?xml version="1.0" encoding="UTF-8"?>
|
||||
<programlisting language="xml"><?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@
|
|||
Spring abstracts all marshalling operations behind the
|
||||
<interfacename>org.springframework.oxm.Marshaller</interfacename> interface, the main methods of which
|
||||
is listed below.
|
||||
<programlisting><![CDATA[
|
||||
<programlisting language="java"><![CDATA[
|
||||
public interface Marshaller {
|
||||
|
||||
/**
|
||||
|
|
@ -115,7 +115,7 @@ public interface Marshaller {
|
|||
<para>
|
||||
Similar to the <interfacename>Marshaller</interfacename>, there is the
|
||||
<interfacename>org.springframework.oxm.Unmarshaller</interfacename> interface.
|
||||
<programlisting><![CDATA[
|
||||
<programlisting language="java"><![CDATA[
|
||||
public interface Unmarshaller {
|
||||
|
||||
/**
|
||||
|
|
@ -205,7 +205,7 @@ public interface Unmarshaller {
|
|||
Spring's OXM can be used for a wide variety of situations. In the following example, we will use it to
|
||||
marshal the settings of a Spring-managed application as an XML file. We will use a simple JavaBean to
|
||||
represent the settings:
|
||||
<programlisting><![CDATA[
|
||||
<programlisting language="java"><![CDATA[
|
||||
public class Settings {
|
||||
private boolean fooEnabled;
|
||||
|
||||
|
|
@ -223,7 +223,7 @@ public class Settings {
|
|||
methods: <methodname>saveSettings</methodname> saves the settings bean to a file named
|
||||
<filename>settings.xml</filename>, and <methodname>loadSettings</methodname> loads these settings again. A
|
||||
<methodname>main</methodname> method constructs a Spring application context, and calls these two methods.
|
||||
<programlisting><![CDATA[
|
||||
<programlisting language="java"><![CDATA[
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
|
@ -284,7 +284,7 @@ public class Application {
|
|||
The <classname>Application</classname> requires both a <property>marshaller</property>
|
||||
and <property>unmarshaller</property> property to be set. We can do so using the following
|
||||
<filename>applicationContext.xml</filename>:
|
||||
<programlisting><![CDATA[
|
||||
<programlisting language="xml"><![CDATA[
|
||||
<beans>
|
||||
<bean id="application" class="Application">
|
||||
<property name="marshaller" ref="castorMarshaller" />
|
||||
|
|
@ -302,7 +302,7 @@ public class Application {
|
|||
</para>
|
||||
<para>
|
||||
This sample application produces the following <filename>settings.xml</filename> file:
|
||||
<programlisting><![CDATA[
|
||||
<programlisting language="xml"><![CDATA[
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<settings foo-enabled="false"/>
|
||||
]]></programlisting>
|
||||
|
|
@ -315,7 +315,7 @@ public class Application {
|
|||
To make these tags available, the appropriate schema has to be referenced first in the preamble of the XML configuration file.
|
||||
The emboldened text in the below snippet references the OXM schema:
|
||||
</para>
|
||||
<programlisting><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
|
||||
<programlisting language="xml"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
]]><emphasis role="bold"><![CDATA[xmlns:oxm="http://www.springframework.org/schema/oxm"]]></emphasis>
|
||||
|
|
@ -342,7 +342,7 @@ public class Application {
|
|||
Each tag will be explained in its respective marshaller's section. As an example though, here is how
|
||||
the configuration of a JAXB2 marshaller might look like:
|
||||
</para>
|
||||
<programlisting><![CDATA[<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.ws.samples.airline.schema"/>]]></programlisting>
|
||||
<programlisting language="xml"><![CDATA[<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.ws.samples.airline.schema"/>]]></programlisting>
|
||||
</section>
|
||||
<section id="oxm-jaxb">
|
||||
<title>JAXB</title>
|
||||
|
|
@ -368,7 +368,7 @@ public class Application {
|
|||
classes to be supported by the marshaller. Schema validation is performed by specifying one or more
|
||||
schema resource to the bean, like so:
|
||||
</para>
|
||||
<programlisting><![CDATA[
|
||||
<programlisting language="xml"><![CDATA[
|
||||
<beans>
|
||||
|
||||
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
|
||||
|
|
@ -389,11 +389,11 @@ public class Application {
|
|||
The <literal>jaxb2-marshaller</literal> tag configures a <classname>org.springframework.oxm.jaxb.Jaxb2Marshaller</classname>.
|
||||
Here is an example:
|
||||
</para>
|
||||
<programlisting><![CDATA[<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.ws.samples.airline.schema"/>]]></programlisting>
|
||||
<programlisting language="xml"><![CDATA[<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.ws.samples.airline.schema"/>]]></programlisting>
|
||||
<para>
|
||||
Alternatively, the list of classes to bind can be provided to the marshaller via the <literal>class-to-be-bound</literal> child tag:
|
||||
</para>
|
||||
<programlisting><![CDATA[<oxm:jaxb2-marshaller id="marshaller">
|
||||
<programlisting language="xml"><![CDATA[<oxm:jaxb2-marshaller id="marshaller">
|
||||
<oxm:class-to-be-bound name="org.springframework.ws.samples.airline.schema.Airport"/>
|
||||
<oxm:class-to-be-bound name="org.springframework.ws.samples.airline.schema.Flight"/>
|
||||
...
|
||||
|
|
@ -450,7 +450,7 @@ public class Application {
|
|||
<interfacename>Marshaller</interfacename> and <interfacename>Unmarshaller</interfacename> interface.
|
||||
It can be wired up as follows:
|
||||
</para>
|
||||
<programlisting><![CDATA[
|
||||
<programlisting language="xml"><![CDATA[
|
||||
<beans>
|
||||
|
||||
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" />
|
||||
|
|
@ -469,7 +469,7 @@ public class Application {
|
|||
The mapping can be set using the <property>mappingLocation</property> resource property, indicated
|
||||
below with a classpath resource.
|
||||
</para>
|
||||
<programlisting><![CDATA[
|
||||
<programlisting language="xml"><![CDATA[
|
||||
<beans>
|
||||
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" >
|
||||
<property name="mappingLocation" value="classpath:mapping.xml" />
|
||||
|
|
@ -500,7 +500,7 @@ public class Application {
|
|||
and <interfacename>Unmarshaller</interfacename>
|
||||
interfaces. It can be configured as follows:
|
||||
</para>
|
||||
<programlisting><![CDATA[
|
||||
<programlisting language="xml"><![CDATA[
|
||||
<beans>
|
||||
|
||||
<bean id="xmlBeansMarshaller" class="org.springframework.oxm.xmlbeans.XmlBeansMarshaller" />
|
||||
|
|
@ -520,7 +520,7 @@ public class Application {
|
|||
The <literal>xmlbeans-marshaller</literal> tag configures a <classname>org.springframework.oxm.xmlbeans.XmlBeansMarshaller</classname>.
|
||||
Here is an example:
|
||||
</para>
|
||||
<programlisting><![CDATA[<oxm:xmlbeans-marshaller id="marshaller"/>]]></programlisting>
|
||||
<programlisting language="xml"><![CDATA[<oxm:xmlbeans-marshaller id="marshaller"/>]]></programlisting>
|
||||
<para>
|
||||
Available attributes are:
|
||||
<informaltable>
|
||||
|
|
@ -580,7 +580,7 @@ public class Application {
|
|||
<property>bindingName</property> property. In the next sample, we bind the
|
||||
<classname>Flights</classname> class:
|
||||
</para>
|
||||
<programlisting><![CDATA[
|
||||
<programlisting language="xml"><![CDATA[
|
||||
<beans>
|
||||
|
||||
<bean id="jibxFlightsMarshaller" class="org.springframework.oxm.jibx.JibxMarshaller">
|
||||
|
|
@ -600,7 +600,7 @@ public class Application {
|
|||
The <literal>jibx-marshaller</literal> tag configures a <classname>org.springframework.oxm.jibx.JibxMarshaller</classname>.
|
||||
Here is an example:
|
||||
</para>
|
||||
<programlisting><![CDATA[<oxm:jibx-marshaller id="marshaller" target-class="org.springframework.ws.samples.airline.schema.Flight"/>]]></programlisting>
|
||||
<programlisting language="xml"><![CDATA[<oxm:jibx-marshaller id="marshaller" target-class="org.springframework.ws.samples.airline.schema.Flight"/>]]></programlisting>
|
||||
<para>
|
||||
Available attributes are:
|
||||
<informaltable>
|
||||
|
|
@ -656,7 +656,7 @@ public class Application {
|
|||
in an application context directly. To further customize the XML, you can set an
|
||||
<emphasis>alias map</emphasis>, which consists of string aliases mapped to classes:
|
||||
</para>
|
||||
<programlisting><![CDATA[
|
||||
<programlisting language="xml"><![CDATA[
|
||||
<beans>
|
||||
|
||||
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
|
||||
|
|
|
|||
|
|
@ -149,7 +149,7 @@
|
|||
defined by the
|
||||
<interfacename>org.springframework.transaction.PlatformTransactionManager</interfacename>
|
||||
interface, shown below:</para>
|
||||
<programlisting><![CDATA[public interface PlatformTransactionManager {
|
||||
<programlisting language="java"><![CDATA[public interface PlatformTransactionManager {
|
||||
|
||||
TransactionStatus getTransaction(TransactionDefinition definition)
|
||||
throws TransactionException;
|
||||
|
|
@ -223,7 +223,7 @@
|
|||
way for transactional code to control transaction execution and query
|
||||
transaction status. The concepts should be familiar, as they are common to
|
||||
all transaction APIs:</para>
|
||||
<programlisting><![CDATA[public interface TransactionStatus {
|
||||
<programlisting language="java"><![CDATA[public interface TransactionStatus {
|
||||
|
||||
boolean isNewTransaction();
|
||||
|
||||
|
|
@ -246,7 +246,7 @@
|
|||
<para>We must define a JDBC <interfacename>DataSource</interfacename>, and
|
||||
then use the Spring <classname>DataSourceTransactionManager</classname>, giving
|
||||
it a reference to the <interfacename>DataSource</interfacename>.</para>
|
||||
<programlisting><![CDATA[<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
|
||||
<programlisting language="xml"><![CDATA[<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
|
||||
<property name="driverClassName" value="${jdbc.driverClassName}" />
|
||||
<property name="url" value="${jdbc.url}" />
|
||||
<property name="username" value="${jdbc.username}" />
|
||||
|
|
@ -254,7 +254,7 @@
|
|||
</bean>]]></programlisting>
|
||||
<para>The related <interfacename>PlatformTransactionManager</interfacename> bean
|
||||
definition will look like this:</para>
|
||||
<programlisting><![CDATA[<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
||||
<programlisting language="xml"><![CDATA[<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
</bean>]]></programlisting>
|
||||
<para>If we use JTA in a J2EE container, as in the <filename>'dataAccessContext-jta.xml'</filename>
|
||||
|
|
@ -263,7 +263,7 @@
|
|||
The <classname>JtaTransactionManager</classname> doesn't need to know about the
|
||||
<interfacename>DataSource</interfacename>, or any other specific resources, as
|
||||
it will use the container's global transaction management infrastructure.</para>
|
||||
<programlisting><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
|
||||
<programlisting language="xml"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:jee="http://www.springframework.org/schema/jee"
|
||||
|
|
@ -301,7 +301,7 @@ xsi:schemaLocation="
|
|||
<interfacename>DataSource</interfacename>, the
|
||||
<classname>HibernateTransactionManager</classname> needs a reference to the
|
||||
<interfacename>SessionFactory</interfacename>.</para>
|
||||
<programlisting><![CDATA[<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
|
||||
<programlisting language="xml"><![CDATA[<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="mappingResources">
|
||||
<list>
|
||||
|
|
@ -320,7 +320,7 @@ xsi:schemaLocation="
|
|||
</bean>]]></programlisting>
|
||||
<para>With Hibernate and JTA transactions, we can simply use the
|
||||
<classname>JtaTransactionManager</classname> as with JDBC or any other resource strategy.</para>
|
||||
<programlisting><![CDATA[<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>]]></programlisting>
|
||||
<programlisting language="xml"><![CDATA[<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>]]></programlisting>
|
||||
<para>Note that this is identical to JTA configuration for any resource,
|
||||
as these are global transactions, which can enlist any transactional
|
||||
resource.</para>
|
||||
|
|
@ -377,7 +377,7 @@ xsi:schemaLocation="
|
|||
<interfacename>DataSource</interfacename>, you would instead use Spring's
|
||||
<classname>org.springframework.jdbc.datasource.DataSourceUtils</classname>
|
||||
class as follows:</para>
|
||||
<programlisting><![CDATA[Connection conn = DataSourceUtils.getConnection(dataSource);]]></programlisting>
|
||||
<programlisting language="java"><![CDATA[Connection conn = DataSourceUtils.getConnection(dataSource);]]></programlisting>
|
||||
<para>If an existing transaction exists, and already has a connection
|
||||
synchronized (linked) to it, that instance will be returned. Otherwise,
|
||||
the method call will trigger the creation of a new connection, which
|
||||
|
|
@ -543,7 +543,7 @@ xsi:schemaLocation="
|
|||
(The intent is to convey the concepts, and using the rote <classname>Foo</classname> and
|
||||
<classname>Bar</classname> tropes means that you can concentrate on the transaction
|
||||
usage and not have to worry about the domain model.)</para>
|
||||
<programlisting><lineannotation>// the service interface that we want to make transactional</lineannotation><![CDATA[
|
||||
<programlisting language="java"><lineannotation>// the service interface that we want to make transactional</lineannotation><![CDATA[
|
||||
|
||||
package x.y.service;
|
||||
|
||||
|
|
@ -558,7 +558,7 @@ public interface FooService {
|
|||
void updateFoo(Foo foo);
|
||||
|
||||
}]]></programlisting>
|
||||
<programlisting><lineannotation>// an implementation of the above interface</lineannotation><![CDATA[
|
||||
<programlisting language="java"><lineannotation>// an implementation of the above interface</lineannotation><![CDATA[
|
||||
|
||||
package x.y.service;
|
||||
|
||||
|
|
@ -594,7 +594,7 @@ public class DefaultFooService implements FooService {
|
|||
<literal>updateFoo(Foo)</literal>) have to execute in the context of a transaction
|
||||
with read-write semantics. Don't worry about taking the following configuration in
|
||||
all at once; everything will be explained in detail in the next few paragraphs.</para>
|
||||
<programlisting><lineannotation><!-- from the file <literal>'context.xml'</literal> --></lineannotation><![CDATA[
|
||||
<programlisting language="xml"><lineannotation><!-- from the file <literal>'context.xml'</literal> --></lineannotation><![CDATA[
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
|
|
@ -678,7 +678,7 @@ public class DefaultFooService implements FooService {
|
|||
<para>A common requirement is to make an entire service layer transactional.
|
||||
The best way to do this is simply to change the pointcut expression to match
|
||||
any operation in your service layer. For example:</para>
|
||||
<programlisting><![CDATA[<aop:config>
|
||||
<programlisting language="xml"><![CDATA[<aop:config>
|
||||
<aop:pointcut id="fooServiceMethods" expression="execution(* x.y.service.*.*(..))"/>
|
||||
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceMethods"/>
|
||||
</aop:config>]]></programlisting>
|
||||
|
|
@ -696,7 +696,7 @@ public class DefaultFooService implements FooService {
|
|||
be started, suspended, be marked as read-only, etc., depending on the
|
||||
transaction configuration associated with that method. Consider the following
|
||||
program that test drives the above configuration.</para>
|
||||
<programlisting><![CDATA[public final class Boot {
|
||||
<programlisting language="java"><![CDATA[public final class Boot {
|
||||
|
||||
public static void main(final String[] args) throws Exception {
|
||||
ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml", Boot.class);
|
||||
|
|
@ -710,7 +710,7 @@ public class DefaultFooService implements FooService {
|
|||
<methodname>insertFoo(..)</methodname> method of the
|
||||
<classname>DefaultFooService</classname> class have been truncated in
|
||||
the interest of clarity.)</emphasis></para>
|
||||
<programlisting> <lineannotation><emphasis role="bold"><!-- the Spring container is starting up... --></emphasis></lineannotation><![CDATA[
|
||||
<programlisting language="xml"> <lineannotation><emphasis role="bold"><!-- the Spring container is starting up... --></emphasis></lineannotation><![CDATA[
|
||||
[AspectJInvocationContextExposingAdvisorAutoProxyCreator] - Creating implicit proxy
|
||||
for bean 'fooService' with 0 common interceptors and 1 specific interceptors
|
||||
]]><lineannotation><emphasis role="bold"><!-- the <classname>DefaultFooService</classname> is actually proxied --></emphasis></lineannotation><![CDATA[
|
||||
|
|
@ -767,7 +767,7 @@ Exception in thread "main" java.lang.UnsupportedOperationException
|
|||
for rollback can be configured. Find below a snippet of XML configuration that
|
||||
demonstrates how one would configure rollback for a checked, application-specific
|
||||
<exceptionname>Exception</exceptionname> type.</para>
|
||||
<programlisting><![CDATA[<tx:advice id="txAdvice" transaction-manager="txManager">
|
||||
<programlisting language="xml"><![CDATA[<tx:advice id="txAdvice" transaction-manager="txManager">
|
||||
<tx:attributes>
|
||||
<tx:method name="get*" read-only="true" ]]><lineannotation><emphasis role="bold">rollback-for="NoProductInStockException"</emphasis></lineannotation><![CDATA[/>
|
||||
<tx:method name="*"/>
|
||||
|
|
@ -778,7 +778,7 @@ Exception in thread "main" java.lang.UnsupportedOperationException
|
|||
In the example configuration below, we effectively are telling the Spring Framework's transaction
|
||||
infrastructure to commit the attendant transaction even in the face of an unhandled
|
||||
<exceptionname>InstrumentNotFoundException</exceptionname>.</para>
|
||||
<programlisting><![CDATA[<tx:advice id="txAdvice">
|
||||
<programlisting language="xml"><![CDATA[<tx:advice id="txAdvice">
|
||||
<tx:attributes>
|
||||
<tx:method name="updateStock" ]]><lineannotation><emphasis role="bold">no-rollback-for="InstrumentNotFoundException"</emphasis></lineannotation><![CDATA[/>
|
||||
<tx:method name="*"/>
|
||||
|
|
@ -789,7 +789,7 @@ Exception in thread "main" java.lang.UnsupportedOperationException
|
|||
<emphasis>strongest</emphasis> matching rule wins. So in the case of the following configuration,
|
||||
any exception other than an <exceptionname>InstrumentNotFoundException</exceptionname> would result in the
|
||||
attendant transaction being marked for rollback.</para>
|
||||
<programlisting><![CDATA[<tx:advice id="txAdvice">
|
||||
<programlisting language="xml"><![CDATA[<tx:advice id="txAdvice">
|
||||
<tx:attributes>
|
||||
<tx:method name="*" rollback-for="Throwable" no-rollback-for="InstrumentNotFoundException"/>
|
||||
</tx:attributes>
|
||||
|
|
@ -797,7 +797,7 @@ Exception in thread "main" java.lang.UnsupportedOperationException
|
|||
<para>The second way to indicate that a rollback is required is to do so
|
||||
<emphasis>programmatically</emphasis>. Although very simple, this way is quite invasive, and tightly couples
|
||||
your code to the Spring Framework's transaction infrastructure, as can be seen below:</para>
|
||||
<programlisting><![CDATA[public void resolvePosition() {
|
||||
<programlisting language="java"><![CDATA[public void resolvePosition() {
|
||||
try {
|
||||
]]><lineannotation>// some business logic...</lineannotation><![CDATA[
|
||||
} catch (NoProductInStockException ex) {
|
||||
|
|
@ -822,7 +822,7 @@ Exception in thread "main" java.lang.UnsupportedOperationException
|
|||
defined in that package (or in subpackages) and that have names ending in
|
||||
<literal>'Service'</literal> have the default transactional configuration, you would write
|
||||
the following:</para>
|
||||
<programlisting><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
|
||||
<programlisting language="xml"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
|
|
@ -863,7 +863,7 @@ Exception in thread "main" java.lang.UnsupportedOperationException
|
|||
<para>Find below an example of configuring two distinct beans with totally different
|
||||
transactional settings.</para>
|
||||
|
||||
<programlisting><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
|
||||
<programlisting language="xml"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
|
|
@ -1015,15 +1015,7 @@ Exception in thread "main" java.lang.UnsupportedOperationException
|
|||
</tbody>
|
||||
</tgroup>
|
||||
</table>
|
||||
</para>
|
||||
<para>At the time of writing it is not possible to have explicit control over the
|
||||
name of a transaction, where 'name' means the transaction name that will be shown
|
||||
in a transaction monitor, if applicable (for example, WebLogic's transaction
|
||||
monitor), and in logging output. For declarative transactions, the transaction
|
||||
name is always the fully-qualified class name + "." + method name of the
|
||||
transactionally-advised class. For example
|
||||
<literal>'com.foo.BusinessService.handlePayment'</literal>.</para>
|
||||
</section>
|
||||
</section>
|
||||
|
||||
<section id="transaction-declarative-annotations">
|
||||
<title>Using <interfacename>@Transactional</interfacename></title>
|
||||
|
|
@ -1041,7 +1033,7 @@ Exception in thread "main" java.lang.UnsupportedOperationException
|
|||
<para>The ease-of-use afforded by the use of the <interfacename>@Transactional</interfacename>
|
||||
annotation is best illustrated with an example, after which all of the details
|
||||
will be explained. Consider the following class definition:</para>
|
||||
<programlisting><lineannotation>// the service class that we want to make transactional</lineannotation><![CDATA[
|
||||
<programlisting language="java"><lineannotation>// the service class that we want to make transactional</lineannotation><![CDATA[
|
||||
]]><emphasis role="bold">@Transactional</emphasis><![CDATA[
|
||||
public class DefaultFooService implements FooService {
|
||||
|
||||
|
|
@ -1056,7 +1048,7 @@ public class DefaultFooService implements FooService {
|
|||
<para>When the above POJO is defined as a bean in a Spring IoC container, the bean
|
||||
instance can be made transactional by adding merely <emphasis>one</emphasis> line of
|
||||
XML configuration, like so:</para>
|
||||
<programlisting><lineannotation><!-- from the file <literal>'context.xml'</literal> --></lineannotation><![CDATA[
|
||||
<programlisting language="xml"><lineannotation><!-- from the file <literal>'context.xml'</literal> --></lineannotation><![CDATA[
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
|
|
@ -1231,7 +1223,7 @@ public class DefaultFooService implements FooService {
|
|||
<methodname>updateFoo(Foo)</methodname> method in the same class takes precedence
|
||||
over the transactional settings defined at the class level.</para>
|
||||
|
||||
<programlisting><![CDATA[@Transactional(readOnly = true)
|
||||
<programlisting language="java"><![CDATA[@Transactional(readOnly = true)
|
||||
public class DefaultFooService implements FooService {
|
||||
|
||||
public Foo getFoo(String fooName) {
|
||||
|
|
@ -1362,8 +1354,7 @@ public class DefaultFooService implements FooService {
|
|||
transaction name is always the fully-qualified class name + "." + method name of the
|
||||
transactionally-advised class. For example, if the <methodname>handlePayment(..)</methodname>
|
||||
method of the <classname>BusinessService</classname> class started a transaction, the name of the
|
||||
transaction would be:</para>
|
||||
<programlisting><![CDATA[com.foo.BusinessService.handlePayment]]></programlisting>
|
||||
transaction would be: <literal>com.foo.BusinessService.handlePayment</literal>.</para>
|
||||
</section>
|
||||
</section>
|
||||
|
||||
|
|
@ -1476,7 +1467,7 @@ public class DefaultFooService implements FooService {
|
|||
<para>Here is the code for a simple profiling aspect. The
|
||||
ordering of advice is controlled via the <interfacename>Ordered</interfacename>
|
||||
interface. For full details on advice ordering, see <xref linkend="aop-ataspectj-advice-ordering"/>.</para>
|
||||
<programlisting><![CDATA[package x.y;
|
||||
<programlisting language="java"><![CDATA[package x.y;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.springframework.util.StopWatch;
|
||||
|
|
@ -1510,7 +1501,7 @@ public class SimpleProfiler implements Ordered {
|
|||
}
|
||||
}
|
||||
]]></programlisting>
|
||||
<programlisting><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
|
||||
<programlisting language="xml"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
|
|
@ -1557,7 +1548,7 @@ public class SimpleProfiler implements Ordered {
|
|||
aspects is effected in a similar fashion.</para>
|
||||
<para>Finally, find below some example configuration for effecting the same
|
||||
setup as above, but using the purely XML declarative approach.</para>
|
||||
<programlisting><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
|
||||
<programlisting language="xml"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
|
|
@ -1640,7 +1631,7 @@ public class SimpleProfiler implements Ordered {
|
|||
<xref linkend="transaction-declarative-annotations"/> and <xref linkend="aop"/>
|
||||
respectively.</para>
|
||||
</note>
|
||||
<programlisting><lineannotation>// construct an appropriate transaction manager </lineannotation><![CDATA[
|
||||
<programlisting language="java"><lineannotation>// construct an appropriate transaction manager </lineannotation><![CDATA[
|
||||
DataSourceTransactionManager txManager = new DataSourceTransactionManager(getDataSource());
|
||||
|
||||
]]><lineannotation>// configure the <classname>AnnotationTransactionAspect</classname> to use it; this must be done before executing any transactional methods</lineannotation><![CDATA[
|
||||
|
|
@ -1709,7 +1700,7 @@ AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager); ]]></pr
|
|||
pass an instance of your custom <interfacename>TransactionCallback</interfacename>
|
||||
to the <methodname>execute(..)</methodname> method exposed on the
|
||||
<classname>TransactionTemplate</classname>. </para>
|
||||
<programlisting><![CDATA[public class SimpleService implements Service {
|
||||
<programlisting language="java"><![CDATA[public class SimpleService implements Service {
|
||||
|
||||
]]><lineannotation>// single <classname>TransactionTemplate</classname> shared amongst all methods in this instance</lineannotation><![CDATA[
|
||||
private final TransactionTemplate transactionTemplate;
|
||||
|
|
@ -1734,7 +1725,7 @@ AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager); ]]></pr
|
|||
<para>If there is no return value, use the convenient
|
||||
<classname>TransactionCallbackWithoutResult</classname> class via an
|
||||
anonymous class like so:</para>
|
||||
<programlisting><![CDATA[transactionTemplate.execute(new ]]><emphasis role="bold">TransactionCallbackWithoutResult</emphasis><![CDATA[() {
|
||||
<programlisting language="java"><![CDATA[transactionTemplate.execute(new ]]><emphasis role="bold">TransactionCallbackWithoutResult</emphasis><![CDATA[() {
|
||||
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
updateOperation1();
|
||||
|
|
@ -1744,7 +1735,7 @@ AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager); ]]></pr
|
|||
<para>Code within the callback can roll the transaction back by calling
|
||||
the <literal>setRollbackOnly()</literal> method on the supplied
|
||||
<interfacename>TransactionStatus</interfacename> object.</para>
|
||||
<programlisting><![CDATA[transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
<programlisting language="java"><![CDATA[transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
try {
|
||||
|
|
@ -1765,7 +1756,7 @@ AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager); ]]></pr
|
|||
Find below an example of programmatically customizing the
|
||||
transactional settings for a specific <classname>TransactionTemplate</classname>.
|
||||
</para>
|
||||
<programlisting><![CDATA[public class SimpleService implements Service {
|
||||
<programlisting language="java"><![CDATA[public class SimpleService implements Service {
|
||||
|
||||
private final TransactionTemplate transactionTemplate;
|
||||
|
||||
|
|
@ -1782,7 +1773,7 @@ AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager); ]]></pr
|
|||
<para>Find below an example of defining a <classname>TransactionTemplate</classname> with some custom
|
||||
transactional settings, using Spring XML configuration. The '<literal>sharedTransactionTemplate</literal>'
|
||||
can then be injected into as many services as are required.</para>
|
||||
<programlisting><![CDATA[<bean id="sharedTransactionTemplate"
|
||||
<programlisting language="xml"><![CDATA[<bean id="sharedTransactionTemplate"
|
||||
class="org.springframework.transaction.support.TransactionTemplate">
|
||||
<property name="isolationLevelName" value="ISOLATION_READ_UNCOMMITTED"/>
|
||||
<property name="timeout" value="30"/>
|
||||
|
|
@ -1811,7 +1802,7 @@ AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager); ]]></pr
|
|||
<interfacename>TransactionDefinition</interfacename> and
|
||||
<interfacename>TransactionStatus</interfacename> objects you can
|
||||
initiate transactions, rollback and commit.</para>
|
||||
<programlisting><![CDATA[DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||
<programlisting language="java"><![CDATA[DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||
]]><lineannotation>// explicitly setting the transaction name is something that can only be done programmatically</lineannotation><![CDATA[
|
||||
def.setName("SomeTxName");
|
||||
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
|
|
|
|||
Loading…
Reference in New Issue