简体   繁体   中英

Autowire Bean with constructor values

I have a connection class that I am trying to @Autowire which requires a timeout parameter

    @Component
    ClientWrapper {
        ...
        ...
        @Autowired
        ClientWrapper(@Value("#{(5*1000)}")int timeout){  // Compiles just fine.
             this.timeout = timeout;
        }
        ...
        ...  // set connection up 
    }

I have a second class that is trying to use the ClientWrapper to make a call. So far I have been unable to figure out how to pass in the timeout parameter.

public class testExternalCall {
   @Autowired
   @Qualified("#{new Integer(10000)}")// This is where I need the guidance.  Not sure how to pass 10000 in as a parameter to the constructor
   ClientWrapper client;

   List<Car> cars = client.getAutos();
}

Whenever I startup the app Spring is telling me that it cannot find the dependency (No Qualifying Bean of type ClientWrapper)

Any thoughts on how to fix this issue? I've looked all over but have not found anything that has worked.

Thanks

are you trying to override the value of timeout ?

well this will not work as indeed, no bean with the @Qualifier you indicated exists.

you should try this :

@Autowired
ClientWrapper(@Value("#{(5*1000)}")int timeout){  // Compiles just fine.
    @Value("config['timeout.value'] ?: '5*10000'")
    int timeout;
}

then you only have to start your application with an application configuration file with a different value.

You can also use @ConfigurationProperties(prefix="timeout")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM