-1

i have properties like this

connection.local=0.0.0.0 i write it on application.properties and at application-local.properties it is correct or not ?

but when i want to get this value with annotation

i use plain java for build apps. and use spring context for get value and annotations

 @Component
@Scope("singleton")
@Slf4j
@Configuration

public class SocketEngine extends Thread {
    
    /**
     * This is to make sure that the server is running and trying even when
     * idxdatafeed disconnects
     */
    @Value("${connection.local}")
    private String connectionLocalhost;
  
    public void run() {

        while (true) {
            Socket server = null;
            String firstData="xvabv";
            try {
                log.info("Connecting to server " + connectionLocalhost+"!");
                server = new Socket(connectionLocalhost, 9010);
                server.setSoTimeout(10000);
                PrintWriter writer= new PrintWriter(server.getOutputStream());

i got value of connectionLocal is null why like that ?

bigger
  • 1
  • 1
  • where did you put your `connection.local` property? – haoyu wang Nov 03 '20 at 03:05
  • i write it on application.properties and at application-local.properties it is correct or not ? @haoyuwang – bigger Nov 03 '20 at 03:14
  • Where did you use `connectionLocalhost`? – Chayne P. S. Nov 03 '20 at 03:35
  • Make sure your appliaction.properties is on the root path of your application. And you get the `SocketEngine` instance with `BeanFactory.getBean` method. – haoyu wang Nov 03 '20 at 03:36
  • Switch to constructor injection instead of field injection, and you will probably instantly find your problem. [My suspicion.](https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null/) Note that you (1) shouldn't extend `Thread` period, (2) shouldn't directly use threads in most cases (use an executor instead), and (3) _appear_ to be reinventing client code that Spring probably already provides for you. – chrylis -cautiouslyoptimistic- Nov 03 '20 at 04:04
  • yes my application.properties are in src/main/resources so it is on root – bigger Nov 03 '20 at 04:11

2 Answers2

0

You may solve your problem by injecting the value in the constructor:

 public SocketEngine(@Value("${connection.local}") String connectionLocalhost) {
    this.connectionLocalhost = connectionLocalhost;

    run();
 }

This should resolve the problem. But really I don't why spring here acts like that (tested). Also you don't need the annotation @Compnent because @Configuration includes it.

The official approach is to define the following method in your configuration class:

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

But it didn't work, and that's why I'm confused!

Bassem Adas
  • 236
  • 2
  • 9
0

I solved the problem , add like below code.

@PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true)
4b0
  • 21,981
  • 30
  • 95
  • 142
bigger
  • 1
  • 1