Skip to content

Commit bd1ffc8

Browse files
committed
CategoryConfig: group Spring configuration into a single config.
Addressed to #927 No functional changes.
1 parent 0f37632 commit bd1ffc8

File tree

5 files changed

+90
-32
lines changed

5 files changed

+90
-32
lines changed

src/main/java/ru/mystamps/web/config/ControllersConfig.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,25 @@
2121
import org.springframework.context.MessageSource;
2222
import org.springframework.context.annotation.Bean;
2323
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.context.annotation.Import;
2425
import org.springframework.context.annotation.Profile;
2526

2627
import lombok.RequiredArgsConstructor;
2728

2829
// CheckStyle: ignore AvoidStarImportCheck for next 1 line
2930
import ru.mystamps.web.controller.*; // NOPMD: UnusedImports
30-
import ru.mystamps.web.feature.category.CategoryController;
31+
import ru.mystamps.web.feature.category.CategoryConfig;
32+
import ru.mystamps.web.feature.category.CategoryService;
3133

3234
@Configuration
3335
@RequiredArgsConstructor
36+
@Import(CategoryConfig.Controllers.class)
3437
public class ControllersConfig {
3538

3639
private final ServicesConfig servicesConfig;
3740
private final MessageSource messageSource;
3841
private final ApplicationEventPublisher eventPublisher;
42+
private final CategoryService categoryService;
3943

4044
@Bean
4145
public AccountController getAccountController() {
@@ -45,14 +49,6 @@ public AccountController getAccountController() {
4549
);
4650
}
4751

48-
@Bean
49-
public CategoryController getCategoryController() {
50-
return new CategoryController(
51-
servicesConfig.getCategoryService(),
52-
servicesConfig.getSeriesService()
53-
);
54-
}
55-
5652
@Bean
5753
public CountryController getCountryController() {
5854
return new CountryController(
@@ -64,7 +60,7 @@ public CountryController getCountryController() {
6460
@Bean
6561
public CollectionController getCollectionController() {
6662
return new CollectionController(
67-
servicesConfig.getCategoryService(),
63+
categoryService,
6864
servicesConfig.getCollectionService(),
6965
servicesConfig.getCountryService(),
7066
servicesConfig.getSeriesService(),
@@ -103,7 +99,7 @@ public ReportController getReportController() {
10399
@Bean
104100
public SeriesController getSeriesController() {
105101
return new SeriesController(
106-
servicesConfig.getCategoryService(),
102+
categoryService,
107103
servicesConfig.getCollectionService(),
108104
servicesConfig.getCountryService(),
109105
servicesConfig.getSeriesService(),
@@ -128,7 +124,7 @@ public SeriesImportController getSeriesImportController() {
128124
@Bean
129125
public SiteController getSiteController() {
130126
return new SiteController(
131-
servicesConfig.getCategoryService(),
127+
categoryService,
132128
servicesConfig.getCollectionService(),
133129
servicesConfig.getCountryService(),
134130
servicesConfig.getSeriesService(),

src/main/java/ru/mystamps/web/config/DaoConfig.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
// CheckStyle: ignore AvoidStarImportCheck for next 2 lines
2929
import ru.mystamps.web.dao.*; // NOPMD: UnusedImports
3030
import ru.mystamps.web.dao.impl.*; // NOPMD: UnusedImports
31-
import ru.mystamps.web.feature.category.CategoryDao;
32-
import ru.mystamps.web.feature.category.JdbcCategoryDao;
3331

3432
@Configuration
3533
@PropertySource("classpath:/sql/stamps_catalog_dao_queries.properties")
@@ -39,11 +37,6 @@ public class DaoConfig {
3937
private final NamedParameterJdbcTemplate jdbcTemplate;
4038
private final Environment env;
4139

42-
@Bean
43-
public CategoryDao getCategoryDao() {
44-
return new JdbcCategoryDao(jdbcTemplate);
45-
}
46-
4740
@Bean
4841
public CountryDao getCountryDao() {
4942
return new JdbcCountryDao(jdbcTemplate);

src/main/java/ru/mystamps/web/config/MvcConfig.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import ru.mystamps.web.Url;
4949
import ru.mystamps.web.controller.converter.LinkEntityDtoGenericConverter;
5050
import ru.mystamps.web.controller.interceptor.DownloadImageInterceptor;
51+
import ru.mystamps.web.feature.category.CategoryService;
5152
import ru.mystamps.web.support.spring.security.CurrentUserArgumentResolver;
5253

5354
@Configuration
@@ -60,12 +61,13 @@
6061
public class MvcConfig extends WebMvcConfigurerAdapter {
6162

6263
private final ServicesConfig servicesConfig;
64+
private final CategoryService categoryService;
6365

6466
@Override
6567
public void addFormatters(FormatterRegistry registry) {
6668
registry.addConverter(
6769
new LinkEntityDtoGenericConverter(
68-
servicesConfig.getCategoryService(),
70+
categoryService,
6971
servicesConfig.getCountryService()
7072
)
7173
);

src/main/java/ru/mystamps/web/config/ServicesConfig.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@
2525
import org.springframework.context.MessageSource;
2626
import org.springframework.context.annotation.Bean;
2727
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.context.annotation.Import;
2829
import org.springframework.core.env.Environment;
2930
import org.springframework.mail.javamail.JavaMailSender;
3031

3132
import lombok.RequiredArgsConstructor;
3233

33-
// CheckStyle: ignore AvoidStarImportCheck for next 3 lines
34+
import ru.mystamps.web.feature.category.CategoryConfig;
3435
import ru.mystamps.web.feature.category.CategoryService;
35-
import ru.mystamps.web.feature.category.CategoryServiceImpl;
36+
// CheckStyle: ignore AvoidStarImportCheck for next 1 line
3637
import ru.mystamps.web.service.*; // NOPMD: UnusedImports
3738
import ru.mystamps.web.support.spring.security.SecurityConfig;
3839

3940
@Configuration
41+
@Import(CategoryConfig.Services.class)
4042
@RequiredArgsConstructor
4143
@SuppressWarnings("PMD.CouplingBetweenObjects")
4244
public class ServicesConfig {
@@ -48,6 +50,7 @@ public class ServicesConfig {
4850
private final Environment env;
4951
private final MessageSource messageSource;
5052
private final ApplicationEventPublisher eventPublisher;
53+
private final CategoryService categoryService;
5154

5255
@Bean
5356
public SuspiciousActivityService getSuspiciousActivityService() {
@@ -62,14 +65,6 @@ public CountryService getCountryService() {
6265
);
6366
}
6467

65-
@Bean
66-
public CategoryService getCategoryService() {
67-
return new CategoryServiceImpl(
68-
LoggerFactory.getLogger(CategoryServiceImpl.class),
69-
daoConfig.getCategoryDao()
70-
);
71-
}
72-
7368
@Bean
7469
public CollectionService getCollectionService() {
7570
return new CollectionServiceImpl(
@@ -82,7 +77,7 @@ public CollectionService getCollectionService() {
8277
public CronService getCronService() {
8378
return new CronServiceImpl(
8479
LoggerFactory.getLogger(CronServiceImpl.class),
85-
getCategoryService(),
80+
categoryService,
8681
getCountryService(),
8782
getCollectionService(),
8883
getSeriesService(),
@@ -196,7 +191,7 @@ public SeriesInfoExtractorService getSeriesInfoExtractorService() {
196191
LoggerFactory.getLogger(TimedSeriesInfoExtractorService.class),
197192
new SeriesInfoExtractorServiceImpl(
198193
LoggerFactory.getLogger(SeriesInfoExtractorServiceImpl.class),
199-
getCategoryService(),
194+
categoryService,
200195
getCountryService(),
201196
getTransactionParticipantService()
202197
)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (C) 2009-2018 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+
package ru.mystamps.web.feature.category;
19+
20+
import org.slf4j.LoggerFactory;
21+
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
25+
26+
import lombok.RequiredArgsConstructor;
27+
28+
import ru.mystamps.web.service.SeriesService;
29+
30+
/**
31+
* Spring configuration that is required for using categories in an application.
32+
*
33+
* The beans are grouped into two classes to make possible to register a controller
34+
* and the services in the separated application contexts.
35+
*/
36+
@Configuration
37+
public class CategoryConfig {
38+
39+
@RequiredArgsConstructor
40+
public static class Controllers {
41+
42+
private final CategoryService categoryService;
43+
private final SeriesService seriesService;
44+
45+
@Bean
46+
public CategoryController categoryController() {
47+
return new CategoryController(categoryService, seriesService);
48+
}
49+
50+
}
51+
52+
@RequiredArgsConstructor
53+
public static class Services {
54+
55+
private final NamedParameterJdbcTemplate jdbcTemplate;
56+
57+
@Bean
58+
public CategoryService categoryService() {
59+
return new CategoryServiceImpl(
60+
LoggerFactory.getLogger(CategoryServiceImpl.class),
61+
categoryDao()
62+
);
63+
}
64+
65+
@Bean
66+
public CategoryDao categoryDao() {
67+
return new JdbcCategoryDao(jdbcTemplate);
68+
}
69+
70+
}
71+
72+
}

0 commit comments

Comments
 (0)