Monday, January 13, 2025
HomeJavaWhat's @PropertySource Annotation in Java and Spring? PropertySource Instance Spring Boot

What’s @PropertySource Annotation in Java and Spring? PropertySource Instance Spring Boot


Good day Java builders, in case you are utilizing Spring Framework or Spring Boot in your new mission and questioning what’s property supply in Spring
Framework
and learn how to use
@PropertySource annotation you then
have come to the proper place. Earlier, I’ve shared the most effective
books and
on-line programs to be taught Spring Framework
and on this article, I’ll train you learn how to use
@PropertySource annotation in Spring
to learn Setting variables and inject properties utilizing
@Worth annotation. Within the Spring
utility, one of many recordsdata which are wanted to offer properties to the
Spring setting is the
@Configuration lessons. 

Spring @PropertySource annotation
is used for this and this tutorial will clarify learn how to use this to inject
properties with @Value. The
@PropertySource annotation provides a
PropertySource to Spring’s Setting in a easy and declarative method.
@Configuration lessons should be used
together with this class.

1. What’s Spring @PropertySource Annotation?

The @PropertySource is a helpful
annotation for including PropertySource to Spring’s Setting and injecting
properties into class attributes through
@Worth. This
@PropertySource annotation provides a
PropertySource to Spring’s Setting in a easy and declarative method.
@Configuration lessons should be used
together with this class.

We are going to talk about the utilization of the @PropertySource file with an instance beneath.

 

Spring @PropertySource annotation with a easy instance

On this instance, we’re studying database configuration from file
config.properties file and set these property values to
DataConfig class utilizing the
Setting.

The pom.xml file.

<?xml model="1.0" encoding="UTF-8"?>
<mission xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <mum or dad>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <model>2.5.0</model>
      <relativePath />
      
   </mum or dad>
   <groupId>com.codexlabs</groupId>
   <artifactId>spring-cloud-config-server</artifactId>
   <model>0.0.1-SNAPSHOT</model>
   <title>spring-database-server</title>
   <description>Demo mission for Spring Boot</description>
   <properties>
      <java.model>11</java.model>
      <spring-cloud.model>2020.0.3</spring-cloud.model>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>check</scope>
      </dependency>
   </dependencies>
   <construct>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </construct>
</mission>

1. Create an utility.properties file.
Let’s create a config.properties file within the spring utility and retailer the
database properties in there.

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student_db
jdbc.username=admin
jdbc.password=admin123@
app.title=My utility
app.model=1.1

Let’s create a config.properties file within the spring utility and retailer the
database properties in there.

1.2 Create the DataConfig.java file
The @PropertySource injects the
properties of database username, password, and app title from the
utility.properties into the spring.

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.beans.manufacturing facility.InitializingBean;
import org.springframework.beans.manufacturing facility.annotation.Autowired;
import org.springframework.beans.manufacturing facility.annotation.Worth;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Setting;

@Configuration
@PropertySource(worth = "utility.properties", ignoreResourceNotFound = true)
public class DataConfig implements InitializingBean {


    @Worth("${jdbc.url}")
    non-public String url;

    @Worth("${jdbc.username}")
    non-public String username;

    @Worth("${jdbc.password}")
    non-public String password;
    @Worth("${app.title}")
    non-public String appName;
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println(url);
        System.out.println(username);
        System.out.println(password);
        System.out.println(appName);
    }


}

1.3 Predominant class Software.java
We use this Software.java
as a default class which has an entry level of the static important methodology.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Software {

    public static void important(String[] args) {
        SpringApplication.run(Software.class, args);
    }
}

The output of the above program will likely be proven beneath.

jdbc:mysql:
admin
admin123@
My utility

Within the above utility, We get the properties from the
utility.properties file utilizing the
getProperty() methodology. 

@Worth("${jdbc.url}")
non-public String url;

@Worth("${jdbc.username}")
non-public String username;

@Worth("${jdbc.password}")
non-public String password;

@Worth("${app.title}")
non-public String appName;

The primary characteristic about properties and
@PropertySource is that you may
override it in numerous methods – for instance with setting variables or
utility switches. utility and see you within the subsequent
tutorial.  

That is all about @PropertySource annotation in Spring Framework. So
on this tutorial, we mentioned learn how to use @PropertySource annotation to
work with properties in Spring. We have now additionally seen a few examples of
@worth Annotation to learn the properties from utility.properties
file. 

Different Java and Spring Tutorial you could like

  • How Spring MVC works internally? (reply)
  • The way to add a file within the Spring MVC utility? (instance)
  • 5 Programs to be taught Spring Cloud for Microservices (programs)
  • Distinction between @Autowired and @Inject in Spring? (reply)
  • Spring Information JPA @Question Instance (question instance)
  • High 5 Frameworks Java Developer Ought to Know (frameworks)
  • Distinction between @Part@Service, and @Controller in Spring (reply)
  • 10 Superior Spring Boot Programs for Java builders (programs)
  • 20+ Spring MVC Interview Questions for Programmers (reply)
  • Distinction between @RequestParam and @PathVariable in Spring (reply)
  • High 7  Programs to be taught Microservices in Java (programs)
  • @SpringBootApplication vs @EnableAutoConfiguration? (reply)
  • 10 Spring MVC annotations Java developer ought to be taught (annotations)
  • 5 Programs to Be taught Spring Safety for Java programmers (programs)
  • Spring Information JPA Repository (JpaReposistory instance)
  • High 5 Spring Boot Annotations Java Builders ought to know (learn)
  • 15 Spring Boot Interview Questions for Java Builders (questions)
  • High 5 Programs to Be taught and Grasp Spring Cloud (programs)
  • High 5 Spring Cloud annotations Java programmer ought to be taught (cloud)

Thanks for studying this text to this point. When you discover this
Spring @PropertySource Annotation Instance then please share it with
your pals and colleagues. When you’ve got any questions or suggestions then
please drop a word.

P. S. – In case you are a Java newbie and need to be taught the Core
Spring Framework from scratch, and searching for some finest on-line assets
then you may as well take a look at these
free Spring Core and MVC programs for rookies. This listing comprises free Udemy and Pluralsight programs to be taught Spring
MVC from scratch.     

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments