I am building a webapp which needs to access application.properties through Environment object.Have a look at my code below.
@Configuration
@ComponentScan(basePackages = "com.asn.myutilities")
@PropertySource(value = { "classpath:application.properties" })
public class AppConfig {
/*
* PropertySourcesPlaceHolderConfigurer Bean only required for @Value("{}")
* annotations. Remove this bean if you are not using @Value annotations for
* injecting properties.
*/
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
public interface AuthService {
void readValues();
}
@Service("authService")
public class AuthServiceImpl implements AuthService {
@Autowired
private Environment environment;
public void readValues() {
System.out.println("Getting property via Spring Environment :"
+ environment.getProperty("userProfileServiceUserName"));
}
}
@Component
public class DelegatorUtil {
@Autowired
static AuthService authService; // coming as null
public static final String serviceCall(HttpServletRequest request,
String baseUri, RequestMethod requestMethod)
throws Exception {
String responseBody = "";
String[] parts = baseUri.split("/");
String microServiceEndPoint = parts[3];
authService.readValues(); // NPE
}
}
Can any one answer my question why NPE coming for the auto wired object in DelegatorUtil class? I tried many ways but not able to figure out how to solve this issue.
Many Thanks