Java Spring не удается запустить @PropertySource
У меня три класса. Пытаюсь подключить @PropertySource в проекте, но в файле application.properties данные не активны и spring их не подхватывает. Подскажите пожалуйста, в чем моя ошибка?

Main
package reversbot;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import reversbot.services.VkBot;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext (InjectionContext.class);
VkBot vkBot = context.getBean(VkBot.class);
vkBot.hello();
}
}
InjectionContext
package reversbot;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
import reversbot.services.VkBot;
@Configuration
@EnableScheduling
@PropertySource(value = "classpath:resources/application.properties", ignoreResourceNotFound = true)
public class InjectionContext {
@Bean
public VkBot vkBot() {
return new VkBot();
}
}
VkBot
package reversbot.services;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@Service
public class VkBot {
@Value("$(ide)")
String ide;
@Value("$(token)")
String token;
@Scheduled(fixedDelay = 5000)
@PostConstruct
public void hello(){
System.out.println ("Hello BOSS!!!!");
System.out.println("ide = " + ide);
System.out.println("token = " + token);
System.out.println(" ");
}
}
ide = login
token = 12345678
Ответы (2 шт):
@PropertySource("application.properties") этого достаточно, но редактор может не подхватить или не сразу подхватить, при первом подключение проперти файлов, но это уже другой вопрос, проверьте есть ли реальный доступ
1. Внедрение значений свойств
- не
@Value("$(ide)"), а@Value("${ide}") - не
@Value("$(token)"), a@Value("${token}")
Нужно использовать фигурные скобки, а не круглые
@Service
public class VkBot {
@Value("${ide}")
String ide;
@Value("${token}")
String token;
// ...
2. Путь к файлу со свойствами
Папка resources и так находится в classpath, поэтому указывать указывать classpath:resources/application.properties не стоит.
Когда вы делаете это, то фактически пытаетесь сослаться на следующий файл:
src/main/resources/resources/application.properties
Которого Spring не находит.
Поэтому лишнее упоминание папки resources стоит убрать
@PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true)
Итого
InjectionContext
package reversbot;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
import reversbot.services.VkBot;
@Configuration
@EnableScheduling
@PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true)
public class InjectionContext {
@Bean
public VkBot vkBot() {
return new VkBot();
}
}
VkBot
package reversbot.services;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@Service
public class VkBot {
@Value("${ide}")
String ide;
@Value("${token}")
String token;
@Scheduled(fixedDelay = 5000)
@PostConstruct
public void hello(){
System.out.println ("Hello BOSS!!!!");
System.out.println("ide = " + ide);
System.out.println("token = " + token);
System.out.println(" ");
}
}

