我正在按照此处的教程创建一个纯注释驱动(无xml)的spring 3应用程序
这是我的配置文件
@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("org.postgresql.Driver");
driverManagerDataSource.setUrl("jdbc:postgresql://localhost:5432/test");
driverManagerDataSource.setUsername("postgres");
driverManagerDataSource.setPassword("gayle");
return driverManagerDataSource;
}
@Bean(name = "studentJDBCTemplate")
public StudentJDBCTemplate studentJDBCTemplate() {
StudentJDBCTemplate studentJDBCTemplate = new StudentJDBCTemplate();
studentJDBCTemplate.setDataSource(dataSource());
studentJDBCTemplate.setDataSourceTransactionManager(dataSourceTransactionManager());
studentJDBCTemplate.setJdbcTemplate(new JdbcTemplate());
return studentJDBCTemplate;
}
@Bean
public DataSourceTransactionManager dataSourceTransactionManager() {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource());
return dataSourceTransactionManager;
}
现在我正在尝试这样做,declarative transaction management
因此我的create()
方法在里面StudentJDBCTemplate
public void create(String name, Integer age) {
System.out.println("Creating!");
String SQL = "insert into Student (name, age) values (?, ?)";
jdbcTemplate.update(SQL, name, age);
System.out.println("Created Record Name=" + name + " Age=" + age);
}
不以编程方式调用事务管理器。
我如何实现这一目标?我必须声明@Aspect
这样做吗?是否有任何注释可以自动配置何时提交事务?