Skip to content

Commit 876801d

Browse files
committed
Migrate XML-configuration to Java-configuration.
NOTE: config with DataSource included conditionally in each profile to not include dependency from another profiles Fixed #108 No functional changes.
1 parent 298d92f commit 876801d

File tree

9 files changed

+319
-117
lines changed

9 files changed

+319
-117
lines changed

pom.xml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,6 @@
495495
<groupId>commons-dbcp</groupId>
496496
<artifactId>commons-dbcp</artifactId>
497497
<version>1.4</version>
498-
<scope>runtime</scope>
499498
</dependency>
500499
<dependency>
501500
<groupId>mysql</groupId>
@@ -507,6 +506,16 @@
507506
<build>
508507
<plugins>
509508

509+
<plugin>
510+
<groupId>org.apache.maven.plugins</groupId>
511+
<artifactId>maven-compiler-plugin</artifactId>
512+
<configuration>
513+
<excludes>
514+
<exclude>**/TestDataSource.java</exclude>
515+
</excludes>
516+
</configuration>
517+
</plugin>
518+
510519
<plugin>
511520
<artifactId>maven-resources-plugin</artifactId>
512521
<version>2.5</version>
@@ -562,7 +571,6 @@
562571
<groupId>org.springframework</groupId>
563572
<artifactId>spring-jdbc</artifactId>
564573
<version>${spring.version}</version>
565-
<scope>runtime</scope>
566574
</dependency>
567575
<dependency>
568576
<groupId>org.hsqldb</groupId>
@@ -574,6 +582,16 @@
574582
<build>
575583
<plugins>
576584

585+
<plugin>
586+
<groupId>org.apache.maven.plugins</groupId>
587+
<artifactId>maven-compiler-plugin</artifactId>
588+
<configuration>
589+
<excludes>
590+
<exclude>**/DevDataSource.java</exclude>
591+
</excludes>
592+
</configuration>
593+
</plugin>
594+
577595
<plugin>
578596
<artifactId>maven-antrun-plugin</artifactId>
579597
<version>1.6</version>

src/main/java/ru/mystamps/web/SiteMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public final class SiteMap {
3131
// defined at pom.xml (and used by functional tests only)
3232
public static final String SITE_URL = "http://127.0.0.1:8081";
3333

34-
// defined at src/main/resources/spring/DispatcherServletContext.xml
34+
// defined at ru.mystamps.web.config.MvcConfig
3535
public static final String INDEX_PAGE_URL = "/";
3636
public static final String MAINTENANCE_PAGE_URL = "/site/maintenance";
3737
public static final String SUCCESSFUL_REGISTRATION_PAGE_URL = "/successful/registration";
@@ -50,7 +50,7 @@ public final class SiteMap {
5050

5151
public static final String ADD_COUNTRY_PAGE_URL = "/country/add";
5252

53-
// defined at src/main/resources/spring/DispatcherServletContext.xml
53+
// defined at ru.mystamps.web.config.MvcConfig
5454
public static final String RESTORE_PASSWORD_PAGE_URL = "/password/restore";
5555

5656
// see also error-page definition at src/main/webapp/WEB-INF/web.xml
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (C) 2011 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
19+
package ru.mystamps.web.config;
20+
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
24+
import javax.sql.DataSource;
25+
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.beans.factory.annotation.Value;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.annotation.PropertySource;
31+
import org.springframework.orm.jpa.JpaTransactionManager;
32+
import org.springframework.orm.jpa.JpaVendorAdapter;
33+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
34+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
35+
import org.springframework.transaction.annotation.EnableTransactionManagement;
36+
import org.springframework.transaction.PlatformTransactionManager;
37+
38+
@Configuration
39+
@EnableTransactionManagement
40+
@PropertySource("classpath:spring/database.properties")
41+
public class DbConfig {
42+
43+
@Value("${jpa.showSql}")
44+
private String showSql;
45+
46+
@Value("${jpa.dialectClassName}")
47+
private String dialectClassName;
48+
49+
@Value("${hibernate.formatSql}")
50+
private String formatSql;
51+
52+
@Value("${hibernate.hbm2ddl.auto}")
53+
private String hbm2ddl;
54+
55+
@Autowired
56+
private DataSource dataSource;
57+
58+
@Bean
59+
public JpaVendorAdapter getJpaVendorAdapter() {
60+
final HibernateJpaVendorAdapter jpaVendorAdapter =
61+
new HibernateJpaVendorAdapter();
62+
63+
jpaVendorAdapter.setDatabasePlatform(dialectClassName);
64+
jpaVendorAdapter.setShowSql(Boolean.valueOf(showSql));
65+
66+
return jpaVendorAdapter;
67+
}
68+
69+
@Bean
70+
public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
71+
final LocalContainerEntityManagerFactoryBean entityManagerFactory =
72+
new LocalContainerEntityManagerFactoryBean();
73+
74+
entityManagerFactory.setJpaVendorAdapter(getJpaVendorAdapter());
75+
entityManagerFactory.setDataSource(dataSource);
76+
77+
final Map<String, String> jpaProperties = new HashMap<String, String>();
78+
jpaProperties.put("hibernate.format_sql", formatSql);
79+
jpaProperties.put("hibernate.connection.charset", "UTF-8");
80+
jpaProperties.put("hibernate.hbm2ddl.auto", hbm2ddl);
81+
entityManagerFactory.setJpaPropertyMap(jpaProperties);
82+
83+
return entityManagerFactory;
84+
}
85+
86+
@Bean
87+
public PlatformTransactionManager getTransactionManager() {
88+
final JpaTransactionManager transactionManager =
89+
new JpaTransactionManager();
90+
91+
transactionManager.setEntityManagerFactory(getEntityManagerFactory().getObject());
92+
93+
return transactionManager;
94+
}
95+
96+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (C) 2011 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
19+
package ru.mystamps.web.config;
20+
21+
import javax.sql.DataSource;
22+
23+
import org.apache.commons.dbcp.BasicDataSource;
24+
25+
import org.springframework.beans.factory.annotation.Value;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.context.annotation.Profile;
29+
import org.springframework.context.annotation.PropertySource;
30+
31+
@Configuration
32+
@Profile("dev")
33+
@PropertySource("classpath:spring/database.properties")
34+
public class DevDataSource {
35+
36+
@Value("${db.driverClassName}")
37+
private String driverClassName;
38+
39+
@Value("${db.url}")
40+
private String url;
41+
42+
@Value("${db.username}")
43+
private String username;
44+
45+
@Value("${db.password}")
46+
private String password;
47+
48+
@Bean(destroyMethod = "close")
49+
public DataSource getDataSource() {
50+
final BasicDataSource dataSource = new BasicDataSource();
51+
52+
dataSource.setDriverClassName(driverClassName);
53+
dataSource.setUrl(url);
54+
dataSource.setUsername(username);
55+
dataSource.setPassword(password);
56+
57+
return dataSource;
58+
}
59+
60+
}
61+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (C) 2011 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
19+
package ru.mystamps.web.config;
20+
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.ComponentScan;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.context.MessageSource;
25+
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
26+
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
27+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
28+
import org.springframework.web.servlet.config.annotation.ResourceConfigurer;
29+
import org.springframework.web.servlet.config.annotation.ViewControllerConfigurer;
30+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
31+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
32+
import org.springframework.web.servlet.ViewResolver;
33+
34+
import static ru.mystamps.web.SiteMap.INDEX_PAGE_URL;
35+
import static ru.mystamps.web.SiteMap.MAINTENANCE_PAGE_URL;
36+
import static ru.mystamps.web.SiteMap.RESTORE_PASSWORD_PAGE_URL;
37+
import static ru.mystamps.web.SiteMap.SUCCESSFUL_ACTIVATION_PAGE_URL;
38+
import static ru.mystamps.web.SiteMap.SUCCESSFUL_REGISTRATION_PAGE_URL;
39+
40+
@Configuration
41+
@EnableWebMvc
42+
@ComponentScan(basePackages = {
43+
"ru.mystamps.web.controller",
44+
"ru.mystamps.web.dao",
45+
"ru.mystamps.web.service",
46+
"ru.mystamps.web.validation"
47+
})
48+
public class MvcConfig extends WebMvcConfigurerAdapter {
49+
50+
@Override
51+
public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
52+
configurer.enable();
53+
}
54+
55+
@Override
56+
public void configureViewControllers(final ViewControllerConfigurer configurer) {
57+
configurer.mapViewName(INDEX_PAGE_URL, "site/index");
58+
configurer.mapViewName(MAINTENANCE_PAGE_URL, "site/maintenance");
59+
configurer.mapViewName(RESTORE_PASSWORD_PAGE_URL, "password/restore");
60+
configurer.mapViewName(SUCCESSFUL_ACTIVATION_PAGE_URL, "account/activation_successful");
61+
configurer.mapViewName(SUCCESSFUL_REGISTRATION_PAGE_URL, "account/activation_sent");
62+
}
63+
64+
@Override
65+
public void configureResourceHandling(final ResourceConfigurer configurer) {
66+
configurer.addPathMapping("/static/**").addResourceLocation("/WEB-INF/static/*");
67+
}
68+
69+
@Bean
70+
public ViewResolver getViewResolver() {
71+
final InternalResourceViewResolver viewResolver =
72+
new InternalResourceViewResolver();
73+
74+
viewResolver.setPrefix("/WEB-INF/pages/");
75+
viewResolver.setSuffix(".jsp");
76+
77+
return viewResolver;
78+
}
79+
80+
@Bean(name = "messageSource")
81+
MessageSource getMessageSource() {
82+
final ReloadableResourceBundleMessageSource messageSource =
83+
new ReloadableResourceBundleMessageSource();
84+
85+
messageSource.setBasename("classpath:ru/mystamps/i18n/Messages");
86+
messageSource.setDefaultEncoding("UTF-8");
87+
88+
return messageSource;
89+
}
90+
91+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2011 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
19+
package ru.mystamps.web.config;
20+
21+
import javax.sql.DataSource;
22+
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.context.annotation.Profile;
26+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
27+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
28+
29+
@Configuration
30+
@Profile("test")
31+
public class TestDataSource {
32+
33+
@Bean
34+
public DataSource getDataSource() {
35+
return new EmbeddedDatabaseBuilder()
36+
.setType(EmbeddedDatabaseType.HSQL)
37+
.addScript("classpath:hsql-scheme.sql")
38+
.addScript("classpath:init-data.sql")
39+
.addScript("classpath:test-data.sql")
40+
.build();
41+
}
42+
}
43+

0 commit comments

Comments
 (0)