I am trying to @Autowire
a @Configuration
class inside a @Service
class. basically my @Configuration
class contains mapping to my custom .properties
file. When i try to autowire my configuration class inside my service class, BeanCreationException
occurs. I am not sure what happen. Just followed the guide on creating Property classes from spring. There must be something i missed out.
Also, when i try to autowire @Configuration
class to another @Configuration
class, it runs smoothly
Currently, i know that, prop
is always null because when i remove prop.getUploadFileLocation()
call, everything will be fine. There must be something wrong during autowiring.
Here is my Service class
@Service
public class ImageService {
public static Logger logger = Logger.getLogger(ImageService.class.getName());
@Autowired
MyProperties prop;
private final String FILE_UPLOAD_LOCATION = prop.getUploadFileLocation() +"uploads/images/";
public void upload(String base64ImageFIle) throws IOException {
logger.info(FILE_UPLOAD_LOCATION);
}
}
Here is my Configuration class
@Data
@Configuration
@ConfigurationProperties (prefix = "my")
public class MyProperties {
private String resourceLocation;
private String resourceUrl;
public String getUploadFileLocation() {
return getResourceLocation().replace("file:///", "");
}
public String getBaseResourceUrl() {
return getResourceUrl().replace("**", "");
}
}
And here is where i can successfully use MyProperties
@Configuration
public class StaticResourceConfiguration implements WebMvcConfigurer {
@Autowired
MyProperties prop;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(prop.getResourceUrl())
.addResourceLocations(prop.getResourceLocation());
}
}