Home > Enterprise >  Contents of property source files are not recognized in some classes
Contents of property source files are not recognized in some classes

Time:02-01

@Value anotation does not get the property from properties file when called from a specific class.

PropertiesConfig:

@Configuration
@PropertySource(value = "classpath:s_app.properties")
@PropertySource(value = "classpath:sDBpools.properties")
public class PropertiesConfig {
}

When I call property from ProjectInfoServiceImpl class it returns but not works from JwtTokenUtil class.

secret returns null here:

@Component
@Log4j2
public class JwtTokenUtil implements Serializable {

    private static final long serialVersionUID = -1L;

    private static final int EXPIRATION_MILLISECONDS = 60 * 60 * 1000;

    @Value("${info.app.jwt.secret}")
    private String secret;

    public  String generateToken(User user){
        Map<String, Object> claims = new HashMap<>();
        claims.put("authorities", user.getAuthorities());

        return generateToken(user.getUsername(), claims);
    }

    public  String generateToken(UserDetails userDetails){
        Map<String, Object> claims = new HashMap<>();
        claims.put("authorities", userDetails.getAuthorities());

        return generateToken(userDetails.getUsername(), claims);
    }

    public  String generateToken(String username, Map<String, Object> claims) {

        return Jwts.builder()
                .setSubject(username)
                .setClaims(claims)
                .setExpiration(new Date((System.currentTimeMillis()   EXPIRATION_MILLISECONDS)))
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256, secret)
                .compact();
    }

    public  boolean validateToken(String username, String token){
        return (username.equals(getUsernameFromToken(token)) && !isExpired(token));
    }

    public  String getUsernameFromToken(String token) {
        return getClaimFromToken(token, Claims::getSubject);
    }

    public  Date getExpirationdateFromToken(String token){
        return getClaimFromToken(token, Claims::getExpiration);
    }

    public  boolean isExpired(String token){
        return !(new Date().before(getExpirationdateFromToken(token)));
    }

    public  <T> T getClaimFromToken(String token, Function<Claims, T> claimsResolver) {
        final Claims claims = getAllClaimsFromToken(token);
        return claimsResolver.apply(claims);
    }

    private  Claims getAllClaimsFromToken(String token) {
        return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
    }

}

But in here it returns as expected:

@Service
@Log4j2
public class ProjectInfoServiceImpl implements ProjectInfoService {

    @Value("${info.app.jwt.secret}")
    private String secret;

    @Value("${s.version}")
    private String version;

    @Value("${s.deployment.profile}")
    private String deploymentProfile;

    @Value("${s.deployment.environment}")
    private String deploymentEnvironment;

    @Override
    public ProjectInfoDTO getProjectInfo() {
        ProjectInfoDTO projectInfoDTO = new ProjectInfoDTO();
        projectInfoDTO.setVersion(version);
        projectInfoDTO.setDeploymentProfile(deploymentProfile);
        projectInfoDTO.setDeploymentEnvironment(deploymentEnvironment);
        return projectInfoDTO;
    }
}

Here is folder structure:

enter image description here

Besides actuator does not get info from info.app.version property of s_properties file. /info endpoint returns no information since it cant find the property.

CodePudding user response:

Problem solved with the approved answer at below question. I had manually instantiated JwtTokenUtil class instead of autowiring it in the caller class. Probably I was not reaching proxy object at all.

Unable to read properties using @Value in SpringBoot application

  •  Tags:  
  • Related