app-context.xml 的完整 Java 配置,包括例如 jdbc 和 tx 指令

Full Java Config of app-context.xml including for example jdbc and tx directives

我正在使用 spring 4.2.1 并希望摆脱所有风格的 xml 配置。在这种情况下,我想摆脱我的 app-config.xml 但我发现很难找到关于此的信息,除了最琐碎的条目(@Configuration 和 @Bean)。我的旧 xml 配置 class 有如下条目:

<jdbc:embedded-database id="dataSource" type="H2" />
<tx:annotation-driven transaction-manager="transactionManager" />
<jpa:repositories base-package="de.dahi.resourcecal.repos"/>

如何在 java 配置中执行这些操作?我发现了一个 2008 年的旧 spring-javaconfig 以及旧项目负责人的评论,它已被合并到核心中,不再适用。但问题是我现在如何在 java 中做这样的事情? 我在各自的 spring 罐子 (org.springframework.transaction.config) 中找到了与 spring-security 和 .config 包相同的以下博客条目 (http://automateddeveloper.blogspot.co.uk/2014/02/spring-4-xml-to-annotation-configuration.html),但没有关于如何实现的信息在那里使用任何东西。

查看 docs. It has an example of annotation-based configuration for the tags you include in your question. You can set the basePackage for the jpa repository using the value property of the @EnableJpaRepositories annotation - see the javadoc2.1.2 Annotation based configuration 部分。

EmbeddedDataBaseBuilder 也可以处理 H2,例如:

@Bean
public DataSource dataSource() {
  EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
  return builder.setType(EmbeddedDatabaseType.H2).build();
}