Take value from an application.properties property - Spring Boot

Asked

Viewed 3,380 times

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

Hierarquia do projeto

  • 1

    The class is a Spring bean, meaning it is noted with @Component, @Service?

  • 1

    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.

  • In which directory is the application.properties file?

  • 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

  • 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 directory resources; 4° Unfortunately nn I can post the repository in git of this project; 4.5° I am connecting to the database with JDBC pure int I’m making via java code, in a class ConnectionFactory and tals;

  • @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?

  • @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

  • @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?

Show 3 more comments

1 answer

2


After my research, although ngm aqi gave me the answer, I was able to find, and I will answer here in case someone else has this problem, the class was like this:

@Component
@ConfigurationProperties(prefix = "spring.datasource") //aqi no prefix eh pra colocar qual o caminho q ta as propriedades
public class ConnectionFactory {

    private String username;
    private String password;
    private String url;
    private String driver_class_name;

    //getters e setters
}

If you create your attributes with the name msmo of the attributes q vc placed the value la in the file .properties Spring msmo already connects them under the covers, and just use the getters to get the values. And if you want to use in another class just create a property there like this:

@Autowired
private ConnectionFactory connection;

and use this property to take the values.

That’s it, vlw flw!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.