mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:35:50 +00:00
Migrate XML datasource definition beans to Java configuration
This commit is contained in:
parent
f92c31c31a
commit
05955cbc1b
8 changed files with 141 additions and 49 deletions
1
pom.xml
1
pom.xml
|
@ -168,7 +168,6 @@
|
||||||
<groupId>org.apache.tomcat</groupId>
|
<groupId>org.apache.tomcat</groupId>
|
||||||
<artifactId>tomcat-jdbc</artifactId>
|
<artifactId>tomcat-jdbc</artifactId>
|
||||||
<version>${tomcat-jdbc.version}</version>
|
<version>${tomcat-jdbc.version}</version>
|
||||||
<scope>runtime</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Logging with SLF4J & LogBack -->
|
<!-- Logging with SLF4J & LogBack -->
|
||||||
|
|
|
@ -8,14 +8,12 @@ import org.springframework.context.annotation.PropertySource;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
// import the dataSource definition
|
|
||||||
@ImportResource("classpath:spring/datasource-config.xml")
|
|
||||||
@ComponentScan("org.springframework.samples.petclinic.service")
|
@ComponentScan("org.springframework.samples.petclinic.service")
|
||||||
// Configurer that replaces ${...} placeholders with values from a properties file
|
// Configurer that replaces ${...} placeholders with values from a properties file
|
||||||
// (in this case, JDBC-related settings for the JPA EntityManager definition below)
|
// (in this case, JDBC-related settings for the JPA EntityManager definition below)
|
||||||
@PropertySource("classpath:spring/data-access.properties")
|
@PropertySource("classpath:spring/data-access.properties")
|
||||||
@EnableTransactionManagement
|
@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 {
|
public class BusinessConfig {
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -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<String, Object> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -13,8 +13,8 @@ jdbc.username=sa
|
||||||
jdbc.password=
|
jdbc.password=
|
||||||
|
|
||||||
# Properties that control the population of schema and data for a new data source
|
# Properties that control the population of schema and data for a new data source
|
||||||
jdbc.initLocation=classpath:db/hsqldb/initDB.sql
|
jdbc.initLocation=db/hsqldb/initDB.sql
|
||||||
jdbc.dataLocation=classpath:db/hsqldb/populateDB.sql
|
jdbc.dataLocation=db/hsqldb/populateDB.sql
|
||||||
|
|
||||||
# Property that determines which database to use with an AbstractJpaVendorAdapter
|
# Property that determines which database to use with an AbstractJpaVendorAdapter
|
||||||
jpa.database=HSQL
|
jpa.database=HSQL
|
||||||
|
|
|
@ -1,43 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!--
|
|
||||||
Application context definition for PetClinic Datasource.
|
|
||||||
-->
|
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xmlns:p="http://www.springframework.org/schema/p"
|
|
||||||
xmlns:context="http://www.springframework.org/schema/context"
|
|
||||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
|
||||||
xmlns:jee="http://www.springframework.org/schema/jee"
|
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
|
||||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
|
||||||
http://www.springframework.org/schema/context
|
|
||||||
http://www.springframework.org/schema/context/spring-context.xsd
|
|
||||||
http://www.springframework.org/schema/jdbc
|
|
||||||
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
|
|
||||||
http://www.springframework.org/schema/jee
|
|
||||||
http://www.springframework.org/schema/jee/spring-jee.xsd">
|
|
||||||
|
|
||||||
<!-- ========================= DATASOURCE DEFINITION ========================= -->
|
|
||||||
|
|
||||||
<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
|
|
||||||
<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
|
|
||||||
<context:property-placeholder location="classpath:spring/data-access.properties" system-properties-mode="OVERRIDE"/>
|
|
||||||
|
|
||||||
<!-- DataSource configuration for the tomcat jdbc connection pool
|
|
||||||
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/-->
|
|
||||||
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
|
|
||||||
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
|
|
||||||
p:username="${jdbc.username}" p:password="${jdbc.password}"/>
|
|
||||||
|
|
||||||
<!-- Database initializer. If any of the script fails, the initialization stops. -->
|
|
||||||
<!-- As an alternative, for embedded databases see <jdbc:embedded-database/>. -->
|
|
||||||
<jdbc:initialize-database data-source="dataSource">
|
|
||||||
<jdbc:script location="${jdbc.initLocation}"/>
|
|
||||||
<jdbc:script location="${jdbc.dataLocation}"/>
|
|
||||||
</jdbc:initialize-database>
|
|
||||||
|
|
||||||
<beans profile="javaee" >
|
|
||||||
<!-- JNDI DataSource for JEE environments -->
|
|
||||||
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/petclinic"/>
|
|
||||||
</beans>
|
|
||||||
</beans>
|
|
Loading…
Reference in a new issue