Этот класс не может читать значения из файлов свойств для моего приложения с весенней загрузкой -
Это структура моего проекта -
Я могу получить доступ к значениям свойств из application-dev.properties
и config.properties
в моем HomeController.java
классе.
Но я получаю значения как null
в моем ClientUtility.java
классе
HomeControlller. java
@RestController
public class HomeController {
@Autowired
private CustomerService customerService;
@Autowired
private PropertyService propertyService;
@Value("${customer.auth.key}")
private String customerAuthKey;
@Autowired
private EntityToDtoMapper mapper;
@GetMapping(path="/customer/{id}",produces= {"application/xml"})
public ResponseEntity<CustomerDto> getCustomer(@PathVariable("id")int id ,@RequestHeader("authKey") String language){
System.out.println(propertyService.getKeytoAddCustomer());
if(language.equals(customerAuthKey)) {
CustomerDto customerDto=customerService.getCustomer(id);
return new ResponseEntity<>(customerDto, HttpStatus.OK);
}
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
}
ClientUtility. java
@Component
public class ClientUtility {
@Value("${customer.auth.key}")
private String customerAuthKey;
@Autowired
private PropertyService propertyService;
public void getCustomers() {
String url = "http://localhost:8080/customer/1";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
// add basic authentication header
headers.set("authKey", "6AE-BH3-24F-67FG-76G-345G-AGF6H");
System.out.println(customerAuthKey);
System.out.println(propertyService.getKeytoAddCustomer());
// build the request
HttpEntity<CustomerDto> request = new HttpEntity<CustomerDto>(headers);
ResponseEntity<CustomerDto> response = restTemplate.exchange(url, HttpMethod.GET, request, CustomerDto.class);
if (response.getStatusCode() == HttpStatus.OK) {
System.out.println("Request Successful.");
System.out.println(response.getBody().getFirstName());
} else {
System.out.println("Request Failed");
System.out.println(response.getStatusCode());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ClientUtility clientUtility = new ClientUtility();
clientUtility.getCustomers();
}
}
}
Вывод
null
Exception in thread "main" java.lang.NullPointerException
at com.spring.liquibase.demo.utility.ClientUtility.getCustomers(ClientUtility.java:33)
at com.spring.liquibase.demo.utility.ClientUtility.main(ClientUtility.java:53)
application.properties
spring.profiles.active=dev
logging.level.org.springframework.web=INFO
logging.level.com=DEBUG
local.server.port=8080
application-dev.properties
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:mysql://localhost:3306/liqbtest?useSSL=false
spring.datasource.username=liqbtest
spring.datasource.password=liqbtest
# Hibernate
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
customer.auth.key = 6AE-BH3-24F-67FG-76G-345G-AGF6H
config.properties
auth.key.to.add.customer=6AE-BH3-24F-67FG-76G-345G-AGF6H
PropertyService.class
package com.spring.liquibase.demo.utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@PropertySource("classpath:config.properties")
public class PropertyService {
@Autowired
private Environment env;
public String getKeytoAddCustomer() {
return env.getProperty("auth.key.to.add.customer");
}
}