Skip to content

Commit fd597d0

Browse files
committed
refactor: move inclusion of {user,s_activation}_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 6b01bf3 commit fd597d0

File tree

4 files changed

+38
-38
lines changed

4 files changed

+38
-38
lines changed

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

-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholder
4343
PropertySourcesPlaceholderConfigurer configurer =
4444
new PropertySourcesPlaceholderConfigurer();
4545
configurer.setLocations(
46-
new ClassPathResource("sql/user_dao_queries.properties"),
47-
new ClassPathResource("sql/users_activation_dao_queries.properties"),
4846
new ClassPathResource("sql/series_dao_queries.properties"),
4947
new ClassPathResource("sql/series_sales_dao_queries.properties"),
5048
new ClassPathResource("sql/suspicious_activity_dao_queries.properties"),

Diff for: src/main/java/ru/mystamps/web/feature/account/AccountConfig.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.springframework.context.annotation.Bean;
2323
import org.springframework.context.annotation.Configuration;
2424
import org.springframework.context.annotation.Lazy;
25+
import org.springframework.context.annotation.PropertySource;
26+
import org.springframework.core.env.Environment;
2527
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
2628
import org.springframework.security.crypto.password.PasswordEncoder;
2729
import ru.mystamps.web.feature.collection.CollectionService;
@@ -50,8 +52,13 @@ public AccountController accountController() {
5052
}
5153

5254
@RequiredArgsConstructor
55+
@PropertySource({
56+
"classpath:sql/user_dao_queries.properties",
57+
"classpath:sql/users_activation_dao_queries.properties"
58+
})
5359
public static class Services {
5460

61+
private final Environment env;
5562
private final NamedParameterJdbcTemplate jdbcTemplate;
5663

5764
@Bean
@@ -84,12 +91,12 @@ public UsersActivationService usersActivationService(
8491

8592
@Bean
8693
public UserDao userDao() {
87-
return new JdbcUserDao(jdbcTemplate);
94+
return new JdbcUserDao(env, jdbcTemplate);
8895
}
8996

9097
@Bean
9198
public UsersActivationDao usersActivationDao() {
92-
return new JdbcUsersActivationDao(jdbcTemplate);
99+
return new JdbcUsersActivationDao(env, jdbcTemplate);
93100
}
94101

95102
}

Diff for: src/main/java/ru/mystamps/web/feature/account/JdbcUserDao.java

+12-14
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
*/
1818
package ru.mystamps.web.feature.account;
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.namedparam.MapSqlParameterSource;
2524
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
@@ -32,22 +31,21 @@
3231
import java.util.HashMap;
3332
import java.util.Map;
3433

35-
@RequiredArgsConstructor
3634
public class JdbcUserDao implements UserDao {
3735

3836
private final NamedParameterJdbcTemplate jdbcTemplate;
37+
private final String countByLoginSql;
38+
private final String countActivatedSinceSql;
39+
private final String findUserDetailsByLoginSql;
40+
private final String addUserSql;
3941

40-
@Value("${user.count_users_by_login}")
41-
private String countByLoginSql;
42-
43-
@Value("${user.count_activated_since}")
44-
private String countActivatedSinceSql;
45-
46-
@Value("${user.find_user_details_by_login}")
47-
private String findUserDetailsByLoginSql;
48-
49-
@Value("${user.create}")
50-
private String addUserSql;
42+
public JdbcUserDao(Environment env, NamedParameterJdbcTemplate jdbcTemplate) {
43+
this.jdbcTemplate = jdbcTemplate;
44+
this.countByLoginSql = env.getRequiredProperty("user.count_users_by_login");
45+
this.countActivatedSinceSql = env.getRequiredProperty("user.count_activated_since");
46+
this.findUserDetailsByLoginSql = env.getRequiredProperty("user.find_user_details_by_login");
47+
this.addUserSql = env.getRequiredProperty("user.create");
48+
}
5149

5250
@Override
5351
public long countByLogin(String login) {

Diff for: src/main/java/ru/mystamps/web/feature/account/JdbcUsersActivationDao.java

+17-20
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
*/
1818
package ru.mystamps.web.feature.account;
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.jdbc.core.namedparam.NamedParameterJdbcTemplate;
2423

2524
import java.util.Collections;
@@ -29,28 +28,26 @@
2928
import java.util.Map;
3029

3130
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
32-
@RequiredArgsConstructor
3331
public class JdbcUsersActivationDao implements UsersActivationDao {
3432

3533
private final NamedParameterJdbcTemplate jdbcTemplate;
34+
private final String findByActivationKeySql;
35+
private final String findOlderThanDateSql;
36+
private final String countByActivationKeySql;
37+
private final String countCreatedSinceSql;
38+
private final String removeByActivationKeySql;
39+
private final String addActivationKeySql;
3640

37-
@Value("${users_activation.find_by_activation_key}")
38-
private String findByActivationKeySql;
39-
40-
@Value("${users_activation.find_older_than}")
41-
private String findOlderThanDateSql;
42-
43-
@Value("${users_activation.count_by_activation_key}")
44-
private String countByActivationKeySql;
45-
46-
@Value("${users_activation.count_created_since}")
47-
private String countCreatedSinceSql;
48-
49-
@Value("${users_activation.remove_by_activation_key}")
50-
private String removeByActivationKeySql;
51-
52-
@Value("${users_activation.create}")
53-
private String addActivationKeySql;
41+
@SuppressWarnings("checkstyle:linelength")
42+
public JdbcUsersActivationDao(Environment env, NamedParameterJdbcTemplate jdbcTemplate) {
43+
this.jdbcTemplate = jdbcTemplate;
44+
this.findByActivationKeySql = env.getRequiredProperty("users_activation.find_by_activation_key");
45+
this.findOlderThanDateSql = env.getRequiredProperty("users_activation.find_older_than");
46+
this.countByActivationKeySql = env.getRequiredProperty("users_activation.count_by_activation_key");
47+
this.countCreatedSinceSql = env.getRequiredProperty("users_activation.count_created_since");
48+
this.removeByActivationKeySql = env.getRequiredProperty("users_activation.remove_by_activation_key");
49+
this.addActivationKeySql = env.getRequiredProperty("users_activation.create");
50+
}
5451

5552
@Override
5653
public UsersActivationDto findByActivationKey(String activationKey) {

0 commit comments

Comments
 (0)