0
I’m a starter at Spring Boot.
I have a project in Spring Boot q has a application.properties
and inside there is a property with a value (spring.datasource.username=user
) and I want to take this value in a java class of mine, I will use this value to make the connection with the bank in jdbc, but nn I can get the value, it always comes null, I searched the internet and found many examples using something like:
@Value("${spring.datasource.username}")
private String username;
or
@Value("#{environment['spring.datasource.username']}")
private String username;
but both cases my username property is always null. Someone can help me?
Edit 1:
In red is the class I want to receive the property value spring.datasource.username
that is in the application.properties
;
In yellow is the file application.properties
The class is a Spring bean, meaning it is noted with
@Component
,@Service
?– StatelessDev
Put in the question the contents of your application.properties file, the image of the project structure to see where that file is and the contents of the class that recovers the property.
– Giuliana Bezerra
In which directory is the application.properties file?
– renanvm
It would be interesting to post the repository in git, so it would be easier to see the organization of directories. However, application.properties already provides this connection to the database. There is no need to do this via Java code
– Jhonatan Mota
1° The class in question nn is annotated neither with @Bean nor with @Component but I will test this agr; 2°
application.properties
nn has no code just that q I spoke on the question; 3°application.properties
is in a directoryresources
; 4° Unfortunately nn I can post the repository in git of this project; 4.5° I am connecting to the database withJDBC
pure int I’m making via java code, in a classConnectionFactory
and tals;– Bruno
@Statelessdev I tried to add the notes and tbm nn turned up nothing, does anyone have any idea how I can recover a value from a property of the
application.properties
?– Bruno
@Next Bruno, the properties are accessible by any bean, however, it must be scanned by the primary bean, you can do this via constructor of some class, and create a bean of the class you need
– Weslley Barbosa
@Weslleybarbosa I nn managed to understand 100% of your comment, can explain more to me, like, what is the "primary" bean and how to scan the properties in it?
– Bruno