Skip to content

Commit 26c9f9f

Browse files
committed
refactor: move inclusion of category_dao_queries.properties to the appropriate package config
We also switched to using Environment.getRequiredProperty() because @value doesn't work without PropertySourcesPlaceholderConfigurer bean (that we are going to remove soon). Relate to #927
1 parent 59c9c83 commit 26c9f9f

File tree

3 files changed

+40
-50
lines changed

3 files changed

+40
-50
lines changed

Diff for: src/main/java/ru/mystamps/web/config/ApplicationContext.java

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholder
4343
PropertySourcesPlaceholderConfigurer configurer =
4444
new PropertySourcesPlaceholderConfigurer();
4545
configurer.setLocations(
46-
new ClassPathResource("sql/category_dao_queries.properties"),
4746
new ClassPathResource("sql/country_dao_queries.properties"),
4847
new ClassPathResource("sql/collection_dao_queries.properties"),
4948
new ClassPathResource("sql/image_dao_queries.properties"),

Diff for: src/main/java/ru/mystamps/web/feature/category/CategoryConfig.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.boot.web.client.RestTemplateBuilder;
2323
import org.springframework.context.annotation.Bean;
2424
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.context.annotation.PropertySource;
2526
import org.springframework.core.env.Environment;
2627
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
2728

@@ -73,13 +74,15 @@ public CategoryService categoryService() {
7374
}
7475

7576
@RequiredArgsConstructor
77+
@PropertySource("classpath:sql/category_dao_queries.properties")
7678
public static class Daos {
7779

7880
private final NamedParameterJdbcTemplate jdbcTemplate;
81+
private final Environment env;
7982

8083
@Bean
8184
public CategoryDao categoryDao() {
82-
return new JdbcCategoryDao(jdbcTemplate);
85+
return new JdbcCategoryDao(env, jdbcTemplate);
8386
}
8487

8588
}

Diff for: src/main/java/ru/mystamps/web/feature/category/JdbcCategoryDao.java

+36-48
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
*/
1818
package ru.mystamps.web.feature.category;
1919

20-
import lombok.RequiredArgsConstructor;
2120
import org.apache.commons.lang3.Validate;
22-
import org.springframework.beans.factory.annotation.Value;
21+
import org.springframework.core.env.Environment;
2322
import org.springframework.dao.EmptyResultDataAccessException;
2423
import org.springframework.jdbc.core.ResultSetExtractor;
2524
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
@@ -38,61 +37,50 @@
3837
import java.util.List;
3938
import java.util.Map;
4039

41-
@RequiredArgsConstructor
4240
@SuppressWarnings({ "PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods", "PMD.TooManyFields" })
4341
public class JdbcCategoryDao implements CategoryDao {
4442

4543
private static final ResultSetExtractor<Map<String, Integer>> NAME_COUNTER_EXTRACTOR =
4644
new MapStringIntegerResultSetExtractor("name", "counter");
4745

4846
private final NamedParameterJdbcTemplate jdbcTemplate;
49-
50-
@Value("${category.create}")
51-
private String addCategorySql;
52-
53-
@Value("${category.count_all_categories}")
54-
private String countAllSql;
55-
56-
@Value("${category.count_categories_by_slug}")
57-
private String countBySlugSql;
58-
59-
@Value("${category.count_categories_by_name}")
60-
private String countByNameSql;
61-
62-
@Value("${category.count_categories_by_name_ru}")
63-
private String countByNameRuSql;
64-
65-
@Value("${category.count_categories_of_collection}")
66-
private String countCategoriesOfCollectionSql;
67-
68-
@Value("${category.count_categories_added_since}")
69-
private String countCategoriesAddedSinceSql;
70-
71-
@Value("${category.count_untranslated_names_since}")
72-
private String countUntranslatedNamesSinceSql;
73-
74-
@Value("${category.count_stamps_by_categories}")
75-
private String countStampsByCategoriesSql;
76-
77-
@Value("${category.find_ids_by_names}")
78-
private String findIdsByNamesSql;
79-
80-
@Value("${category.find_ids_by_name_pattern}")
81-
private String findIdsByNamePatternSql;
82-
83-
@Value("${category.find_all_categories_names_with_slug}")
84-
private String findCategoriesNamesWithSlugSql;
85-
86-
@Value("${category.find_category_link_info_by_slug}")
87-
private String findLinkEntityBySlugSql;
88-
47+
private final String addCategorySql;
48+
private final String countAllSql;
49+
private final String countBySlugSql;
50+
private final String countByNameSql;
51+
private final String countByNameRuSql;
52+
private final String countCategoriesOfCollectionSql;
53+
private final String countCategoriesAddedSinceSql;
54+
private final String countUntranslatedNamesSinceSql;
55+
private final String countStampsByCategoriesSql;
56+
private final String findIdsByNamesSql;
57+
private final String findIdsByNamePatternSql;
58+
private final String findCategoriesNamesWithSlugSql;
59+
private final String findLinkEntityBySlugSql;
8960
@SuppressWarnings("PMD.LongVariable")
90-
@Value("${category.find_categories_with_parent_names}")
91-
private String findCategoriesWithParentNamesSql;
92-
61+
private final String findCategoriesWithParentNamesSql;
9362
@SuppressWarnings("PMD.LongVariable")
94-
@Value("${category.find_from_last_created_series_by_user}")
95-
private String findFromLastCreatedSeriesByUserSql;
63+
private final String findFromLastCreatedSeriesByUserSql;
64+
65+
@SuppressWarnings("checkstyle:linelength")
66+
public JdbcCategoryDao(Environment env, NamedParameterJdbcTemplate jdbcTemplate) {
67+
this.jdbcTemplate = jdbcTemplate;
68+
this.addCategorySql = env.getRequiredProperty("category.create");
69+
this.countAllSql = env.getRequiredProperty("category.count_all_categories");
70+
this.countBySlugSql = env.getRequiredProperty("category.count_categories_by_slug");
71+
this.countByNameSql = env.getRequiredProperty("category.count_categories_by_name");
72+
this.countByNameRuSql = env.getRequiredProperty("category.count_categories_by_name_ru");
73+
this.countCategoriesOfCollectionSql = env.getRequiredProperty("category.count_categories_of_collection");
74+
this.countCategoriesAddedSinceSql = env.getRequiredProperty("category.count_categories_added_since");
75+
this.countUntranslatedNamesSinceSql = env.getRequiredProperty("category.count_untranslated_names_since");
76+
this.countStampsByCategoriesSql = env.getRequiredProperty("category.count_stamps_by_categories");
77+
this.findIdsByNamesSql = env.getRequiredProperty("category.find_ids_by_names");
78+
this.findIdsByNamePatternSql = env.getRequiredProperty("category.find_ids_by_name_pattern");
79+
this.findCategoriesNamesWithSlugSql = env.getRequiredProperty("category.find_all_categories_names_with_slug");
80+
this.findLinkEntityBySlugSql = env.getRequiredProperty("category.find_category_link_info_by_slug");
81+
this.findCategoriesWithParentNamesSql = env.getRequiredProperty("category.find_categories_with_parent_names");
82+
this.findFromLastCreatedSeriesByUserSql = env.getRequiredProperty("category.find_from_last_created_series_by_user");
83+
}
9684

9785
@Override
9886
public Integer add(AddCategoryDbDto category) {

0 commit comments

Comments
 (0)