我无法以任何方式从 spring 引导中的 application.properties 获取值
I can't get value from application.properties in spring boot in any way
我试过各种方法。
@Value("${app.host})
@AutoWired
Environment environment
我可以从其他文件中的 application.properties 中获取值,但是当我尝试从线程任务中获取它时在我实现 Callable Interface 的地方,我得到的值为 null。
这是我的application.properties
app.host = "xyz.com"
服务
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<FTPClient> result = executorService.submit(new FtpConnector());
FtpConnector.java
import java.util.concurrent.Callable;
@Service
@PropertySource("classpath:application.properties")
public class FtpConnector implements Callable<FTPClient>{
FTPClient ftpClient = new FTPClient();
Logger logger = LoggerFactory.getLogger(FtpConnector.class);
@Autowired
FtpClient client;
@Value("${app.host}")
private String host;
@Override
public FTPClient call() throws Exception {
logger.info("Host from connecter {}",host);
}
输出
2022-03-02 20:44:03.831 INFO 15135 --- [ool-13-thread-1] t.a.tech.ftpnew.service.FtpConnector : Host from connecter null
有没有人有任何想法或解决方案来获取线程中 application.properties 的值。 ?
当您使用 new FtpConnector()
时,它不会进行任何依赖注入,因此 FtpClient
和 host
不会被注入。执行以下操作;
@Autowired
private FtpConnector ftpConnector;
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<FTPClient> result = executorService.submit(ftpConnector);
我试过各种方法。
@Value("${app.host})
@AutoWired
Environment environment
我可以从其他文件中的 application.properties 中获取值,但是当我尝试从线程任务中获取它时在我实现 Callable Interface 的地方,我得到的值为 null。
这是我的application.properties
app.host = "xyz.com"
服务
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<FTPClient> result = executorService.submit(new FtpConnector());
FtpConnector.java
import java.util.concurrent.Callable;
@Service
@PropertySource("classpath:application.properties")
public class FtpConnector implements Callable<FTPClient>{
FTPClient ftpClient = new FTPClient();
Logger logger = LoggerFactory.getLogger(FtpConnector.class);
@Autowired
FtpClient client;
@Value("${app.host}")
private String host;
@Override
public FTPClient call() throws Exception {
logger.info("Host from connecter {}",host);
}
输出
2022-03-02 20:44:03.831 INFO 15135 --- [ool-13-thread-1] t.a.tech.ftpnew.service.FtpConnector : Host from connecter null
有没有人有任何想法或解决方案来获取线程中 application.properties 的值。 ?
当您使用 new FtpConnector()
时,它不会进行任何依赖注入,因此 FtpClient
和 host
不会被注入。执行以下操作;
@Autowired
private FtpConnector ftpConnector;
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<FTPClient> result = executorService.submit(ftpConnector);