diff --git a/pom.xml b/pom.xml index cbc3501bf..4e6d08005 100644 --- a/pom.xml +++ b/pom.xml @@ -168,7 +168,6 @@ org.apache.tomcat tomcat-jdbc ${tomcat-jdbc.version} - runtime diff --git a/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java b/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java index fd258f7e6..190318edb 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java @@ -8,14 +8,12 @@ import org.springframework.context.annotation.PropertySource; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration -// import the dataSource definition -@ImportResource("classpath:spring/datasource-config.xml") @ComponentScan("org.springframework.samples.petclinic.service") // Configurer that replaces ${...} placeholders with values from a properties file // (in this case, JDBC-related settings for the JPA EntityManager definition below) @PropertySource("classpath:spring/data-access.properties") @EnableTransactionManagement -@Import({JdbcConfig.class, SharedJpaConfig.class, JpaConfig.class, SpringDataJpaConfig.class}) +@Import({DataSourceConfig.class, InitDataSourceConfig.class, JdbcConfig.class, SharedJpaConfig.class, JpaConfig.class, SpringDataJpaConfig.class}) public class BusinessConfig { diff --git a/src/main/java/org/springframework/samples/petclinic/config/DataSourceConfig.java b/src/main/java/org/springframework/samples/petclinic/config/DataSourceConfig.java new file mode 100644 index 000000000..5082bd8d6 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/DataSourceConfig.java @@ -0,0 +1,46 @@ +package org.springframework.samples.petclinic.config; + +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Description; +import org.springframework.context.annotation.Profile; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.jndi.JndiObjectFactoryBean; + +@Configuration +@PropertySource("classpath:spring/data-access.properties") +public class DataSourceConfig { + + @Autowired + private Environment env; + + @Bean(name = "dataSource") + @Description("DataSource configuration for the tomcat jdbc connection pool") + @NotProfile("javaee") + public DataSource dataSource() { + // See here for more details on commons-dbcp versus tomcat-jdbc: + // http://blog.ippon.fr/2013/03/13/improving-the-performance-of-the-spring-petclinic-sample-application-part-3-of-5/--> + org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(); + dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); + dataSource.setUrl(env.getProperty("jdbc.url")); + dataSource.setUsername(env.getProperty("jdbc.username")); + dataSource.setPassword(env.getProperty("jdbc.password")); + return dataSource; + } + + @Bean(name = "dataSource") + @Description("JNDI DataSource for JEE environments") + @Profile("javaee") + public JndiObjectFactoryBean jndiDataSource() + throws IllegalArgumentException { + JndiObjectFactoryBean dataSource = new JndiObjectFactoryBean(); + dataSource.setExpectedType(DataSource.class); + dataSource.setJndiName(env.getProperty("java:comp/env/jdbc/petclinic")); + return dataSource; + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/config/InitDataSourceConfig.java b/src/main/java/org/springframework/samples/petclinic/config/InitDataSourceConfig.java new file mode 100644 index 000000000..7ecae6c09 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/InitDataSourceConfig.java @@ -0,0 +1,31 @@ +package org.springframework.samples.petclinic.config; + +import javax.annotation.PostConstruct; +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.core.io.ClassPathResource; +import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; +import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; + +@Configuration +public class InitDataSourceConfig { + + @Autowired + private Environment env; + + @Autowired + private DataSource dataSource; + + @PostConstruct + public void init() { + ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); + databasePopulator.addScript(new ClassPathResource(env.getProperty("jdbc.initLocation"))); + databasePopulator.addScript(new ClassPathResource(env.getProperty("jdbc.dataLocation"))); + DatabasePopulatorUtils.execute(databasePopulator, dataSource); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/config/NotProfile.java b/src/main/java/org/springframework/samples/petclinic/config/NotProfile.java new file mode 100644 index 000000000..111c1f1a6 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/NotProfile.java @@ -0,0 +1,29 @@ +package org.springframework.samples.petclinic.config; + + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.context.annotation.Conditional; + + +/** + * Indicates that a component is eligible for registration when none of the {@linkplain + * #value specified profiles} is active. +*/ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +@Documented +@Conditional(NotProfileCondition.class) +public @interface NotProfile { + + /** + * The set of profiles for which the annotated component should not be registered. + */ + String[] value(); + + +} diff --git a/src/main/java/org/springframework/samples/petclinic/config/NotProfileCondition.java b/src/main/java/org/springframework/samples/petclinic/config/NotProfileCondition.java new file mode 100644 index 000000000..097a47aca --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/NotProfileCondition.java @@ -0,0 +1,32 @@ +package org.springframework.samples.petclinic.config; + +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; +import org.springframework.util.MultiValueMap; + +/** + * {@link Condition} that matches based on the value of a {@link NotProfile @NotProfile} + * annotation. + * + */ +class NotProfileCondition implements Condition { + + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + if (context.getEnvironment() != null) { + MultiValueMap attrs = metadata.getAllAnnotationAttributes(NotProfile.class.getName()); + if (attrs != null) { + for (Object value : attrs.get("value")) { + if (context.getEnvironment().acceptsProfiles(((String[]) value))) { + return false; + } + } + return true; + } + } + return true; + } + +} + diff --git a/src/main/resources/spring/data-access.properties b/src/main/resources/spring/data-access.properties index c1cc3cefd..e22ce1df6 100644 --- a/src/main/resources/spring/data-access.properties +++ b/src/main/resources/spring/data-access.properties @@ -13,8 +13,8 @@ jdbc.username=sa jdbc.password= # Properties that control the population of schema and data for a new data source -jdbc.initLocation=classpath:db/hsqldb/initDB.sql -jdbc.dataLocation=classpath:db/hsqldb/populateDB.sql +jdbc.initLocation=db/hsqldb/initDB.sql +jdbc.dataLocation=db/hsqldb/populateDB.sql # Property that determines which database to use with an AbstractJpaVendorAdapter jpa.database=HSQL diff --git a/src/main/resources/spring/datasource-config.xml b/src/main/resources/spring/datasource-config.xml deleted file mode 100644 index f74129963..000000000 --- a/src/main/resources/spring/datasource-config.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file