Skip to content

Commit e4fa25a

Browse files
committed
refactor: move inclusion of country_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 26c9f9f commit e4fa25a

File tree

3 files changed

+42
-53
lines changed

3 files changed

+42
-53
lines changed

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/country_dao_queries.properties"),
4746
new ClassPathResource("sql/collection_dao_queries.properties"),
4847
new ClassPathResource("sql/image_dao_queries.properties"),
4948
new ClassPathResource("sql/user_dao_queries.properties"),

src/main/java/ru/mystamps/web/feature/country/CountryConfig.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 CountryService countryService() {
7374
}
7475

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

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

8083
@Bean
8184
public CountryDao countryDao() {
82-
return new JdbcCountryDao(jdbcTemplate);
85+
return new JdbcCountryDao(env, jdbcTemplate);
8386
}
8487

8588
}

src/main/java/ru/mystamps/web/feature/country/JdbcCountryDao.java

+38-51
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
*/
1818
package ru.mystamps.web.feature.country;
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;
@@ -37,65 +36,53 @@
3736
import java.util.List;
3837
import java.util.Map;
3938

40-
@RequiredArgsConstructor
4139
@SuppressWarnings({ "PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods", "PMD.TooManyFields" })
4240
public class JdbcCountryDao implements CountryDao {
4341

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

4745
private final NamedParameterJdbcTemplate jdbcTemplate;
48-
49-
@Value("${country.create}")
50-
private String addCountrySql;
51-
52-
@Value("${country.count_all_countries}")
53-
private String countAllSql;
54-
55-
@Value("${country.count_countries_by_slug}")
56-
private String countBySlugSql;
57-
58-
@Value("${country.count_countries_by_name}")
59-
private String countByNameSql;
60-
61-
@Value("${country.count_countries_by_name_ru}")
62-
private String countByNameRuSql;
63-
64-
@Value("${country.count_countries_of_collection}")
65-
private String countCountriesOfCollectionSql;
66-
67-
@Value("${country.count_countries_added_since}")
68-
private String countCountriesAddedSinceSql;
69-
70-
@Value("${country.count_untranslated_names_since}")
71-
private String countUntranslatedNamesSinceSql;
72-
73-
@Value("${country.count_stamps_by_countries}")
74-
private String countStampsByCountriesSql;
75-
76-
@Value("${country.find_ids_by_names}")
77-
private String findIdsByNamesSql;
78-
79-
@Value("${country.find_ids_by_name_pattern}")
80-
private String findIdsByNamePatternSql;
81-
82-
@Value("${country.find_all_countries_names_with_slug}")
83-
private String findCountriesNamesWithSlugSql;
84-
85-
@Value("${country.find_country_link_info_by_slug}")
86-
private String findCountryLinkEntityBySlugSql;
87-
46+
private final String addCountrySql;
47+
private final String countAllSql;
48+
private final String countBySlugSql;
49+
private final String countByNameSql;
50+
private final String countByNameRuSql;
51+
private final String countCountriesOfCollectionSql;
52+
private final String countCountriesAddedSinceSql;
53+
private final String countUntranslatedNamesSinceSql;
54+
private final String countStampsByCountriesSql;
55+
private final String findIdsByNamesSql;
56+
private final String findIdsByNamePatternSql;
57+
private final String findCountriesNamesWithSlugSql;
58+
private final String findCountryLinkEntityBySlugSql;
8859
@SuppressWarnings("PMD.LongVariable")
89-
@Value("${country.find_from_last_created_series_by_user}")
90-
private String findFromLastCreatedSeriesByUserSql;
91-
60+
private final String findFromLastCreatedSeriesByUserSql;
9261
@SuppressWarnings("PMD.LongVariable")
93-
@Value("${country.find_popular_country_from_user_collection}")
94-
private String findPopularCountryInCollectionSql;
95-
62+
private final String findPopularCountryInCollectionSql;
9663
@SuppressWarnings("PMD.LongVariable")
97-
@Value("${country.find_last_country_created_by_user}")
98-
private String findLastCountryCreatedByUserSql;
64+
private final String findLastCountryCreatedByUserSql;
65+
66+
@SuppressWarnings("checkstyle:linelength")
67+
public JdbcCountryDao(Environment env, NamedParameterJdbcTemplate jdbcTemplate) {
68+
this.jdbcTemplate = jdbcTemplate;
69+
this.addCountrySql = env.getRequiredProperty("country.create");
70+
this.countAllSql = env.getRequiredProperty("country.count_all_countries");
71+
this.countBySlugSql = env.getRequiredProperty("country.count_countries_by_slug");
72+
this.countByNameSql = env.getRequiredProperty("country.count_countries_by_name");
73+
this.countByNameRuSql = env.getRequiredProperty("country.count_countries_by_name_ru");
74+
this.countCountriesOfCollectionSql = env.getRequiredProperty("country.count_countries_of_collection");
75+
this.countCountriesAddedSinceSql = env.getRequiredProperty("country.count_countries_added_since");
76+
this.countUntranslatedNamesSinceSql = env.getRequiredProperty("country.count_untranslated_names_since");
77+
this.countStampsByCountriesSql = env.getRequiredProperty("country.count_stamps_by_countries");
78+
this.findIdsByNamesSql = env.getRequiredProperty("country.find_ids_by_names");
79+
this.findIdsByNamePatternSql = env.getRequiredProperty("country.find_ids_by_name_pattern");
80+
this.findCountriesNamesWithSlugSql = env.getRequiredProperty("country.find_all_countries_names_with_slug");
81+
this.findCountryLinkEntityBySlugSql = env.getRequiredProperty("country.find_country_link_info_by_slug");
82+
this.findFromLastCreatedSeriesByUserSql = env.getRequiredProperty("country.find_from_last_created_series_by_user");
83+
this.findPopularCountryInCollectionSql = env.getRequiredProperty("country.find_popular_country_from_user_collection");
84+
this.findLastCountryCreatedByUserSql = env.getRequiredProperty("country.find_last_country_created_by_user");
85+
}
9986

10087
@Override
10188
public Integer add(AddCountryDbDto country) {

0 commit comments

Comments
 (0)