Spring学習ノート(六)注釈方式物事の配置


私たちのプロジェクトがデータベースに対する操作を開発するとき、常に物事を管理する必要があります.
Springフレームワークを使用すると、springコンテナは物事を管理する構成を提供し、簡単な構成を使用すると煩雑な物事管理をspringコンテナに管理することができる.
Springは2つの構成方法を提供し、1つは注釈方式を採用し、もう1つはxmlファイル構成方式である.
注釈を使用して手順を設定します.
まずspringのプロファイルのbeansラベルに、物事のネーミングスペースを追加します.
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"

物事の注釈エンジンを開きます.
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"></property>
</bean>
  	<tx:annotation-driven transaction-manager="txManager"/>
必要な事務管理の業務クラスに事務注釈を追加する:
@Service("personService")
@Transactional
public class PersonServiceImpl implements PersonService {

	private JdbcTemplate jdbcTemplate;

	@Override
	public void save(Person person) {
		jdbcTemplate.update("insert into person (name) values (?)",
				new Object[] { person.getName() },
				new int[] { java.sql.Types.VARCHAR });
	}

注釈を追加すると、このクラスのすべてのビジネス・メソッドはデフォルトでは、それぞれのメソッドで動作が共通しています.
デフォルトでは、ビジネス・メソッドでRuntimeExceptionが投げ出されると、ビジネス・オペレーションがロールバックされます.
checkedExceptionが投げ出された場合、ビジネス操作はロールバックされません.
もちろん、手動で物事の操作を必要とするように構成することもできます.
業務方法に注釈を追加して、その異常なロールバックを制御して、ロールバックしません;この方法は物事をサポートするかどうかなど:
@Override
	@Transactional(noRollbackFor=RuntimeException.class)
	public void update(Person person) {
		jdbcTemplate.update("update person set name=? where id=?",
				new Object[] { person.getName(), person.getId()},
				new int[] { java.sql.Types.VARCHAR, java.sql.Types.INTEGER});
	}
上の注記は、運転期間異常を投げ出したときに転がらないことを示しています.
@Transactional(rollbackFor=Exception.class)

checkedExceptionを表すと、ロールします.
また、物事をサポートしない制御方法もあります.
@Transactional(propagation=Propagation.NOT_SUPPORTED)
デフォルトのすべての方法で採用されているpropagationは
PropagationREQIRED