'Access Value from application.properties to Entity class or any other normal Java Class
So my problem is not very genuine it is already asked by few peoples but i have not got a clear idea so asking exact problem here.
/*This class is the implementation of all the methods of...Service interface.*/
@Service
@PropertySource("classpath:applicationText.properties")
public class TaskServicesImpl implements TaskServices {
@Autowired
private TaskRepository taskDAO;
@Autowired
private Environment env;
@Override
public boolean deleteTask(long taskId) throws UnsupportedOperationException {
Task entity;
try {
entity = taskDAO.findById(taskId).get();
} catch (NoSuchElementException ex) {
throw new NoSuchElementException(env.getProperty("delete.invalidTaskId"));
}
if (entity.getIsTaskActive().equals(env.getProperty("active.yes"))) {
entity.setIsTaskActive(env.getProperty("active.no"));
taskDAO.save(entity);
return true;
} else {
throw new UnsupportedOperationException(env.getProperty("delete.alreadyDeletedTask"));
}
}
}
Here Environment env working perfectly but in my pojo
@Entity
@Table(name = "task")
@PropertySource("classpath:applicationText.properties")
@Component
public class Task {
@Autowired
private Environment env;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long taskId;
@NotBlank(message = "Name can not be empty.")
private String taskName;
/*
* @YesOrNoOnly will validate that It will accept values only Yes or No
*/
@NotBlank(message = "Task Active should not be empty.")
@YesOrNoOnly
@JsonProperty(access = Access.WRITE_ONLY)
@Value("${active.yes}")
private String isTaskActive;
I have another Java Normal Class file which is also not managed by spring (there is no annotation like @Service,@RestController etc) where I am not able to get application.properties values.
Note: I have no xml file I am only working with annotation based and application.properties file
Till Now tried things @Value Not working Environment env; env.getProperty("application.properties value") @Component putted above the entity class but not working
Request: Please give some understandable answer because I am new in spring boot so try to use some non-technical words otherwise no benefit to ask this question again on stackoverflow.
Thanks in advance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
