✍️
Notes.md
  • Table of contents
  • React.Js
    • React Hooks
    • Old :- React : Using Classes
  • Blockchain
    • Solidity
    • Custom ERC20 token
    • Contract
  • Tools and Tech
    • Docker
    • Git version Control
  • Java
    • Data & Data Types
    • IO in Java
    • Data Structures
      • Array in Java
      • Collections in Java
      • Map in Java
      • Enums in Java
      • Linked List in Java
      • List in Java
      • Queues & Stacks
      • Set in Java
      • TreeSet and TreeMap
    • Object Oriented Programming
      • Object Class Methods and Constructor
      • Immutable Class & Objects
      • Constructors
      • Visibility
      • Generics
    • Threads in Java
    • Useful Stuff Java
      • Lambda & Stream
    • Keywords in Java
      • Annotations
      • Comparators
      • Packages in Java
    • Miscellaneous
    • Articles to refer to
  • Golang
    • Competitive Programming in Go
    • Testing simple web server
    • Learning Go : Part 1
    • Maps vs slices
    • Golang Garbage Collector 101
    • Things Golang do differently
    • Go Things
  • Linux
    • Shell programming
    • Linux Commands Part 1 - 4
    • Linux Commands Part 5 - 8
    • Linux Commands Part 9 - 10
  • Software Design
    • Solid Design
    • OOPS
    • Design Patterns
      • Creational Design Pattern
        • Builder DP
        • Factory DP
        • Singleton DP
      • Adapter DP
      • Bridge DP
      • Iterator DP
      • State DP
      • Strategy DP
      • Behavioral Design Pattern
        • Observer DP
      • Structural Design Pattern
        • Facade DP
  • Cloud
    • Google Cloud Platform
      • GCP Core Infrastructure
      • Cloud Networking
  • Spring Boot
    • Spring Basics
      • Spring Beans
      • Important Annotations
      • Important Spring Things
      • Maven Things
      • Spring A.O.P
    • Spring Boot Controller
      • Response Entity Exception Handling
    • Spring Things
    • Spring MVC
    • Spring Data
      • Redis
      • Spring Data JPA
      • JDBC
    • Apache Camel
  • Miscellaneous
    • Troubleshooting and Debugging
Powered by GitBook
On this page
  • @SpringBootTest & @Test
  • @Component
  • @Service
  • @Repository
  • @Autowired
  • @Primary
  • @EnableConfigurationProperties
  • @ConfigurationProperties("secret")

Was this helpful?

  1. Spring Boot
  2. Spring Basics

Important Annotations

@SpringBootTest & @Test

@SpringBootTest
public class CourseRepositoryTest {

	@Autowired
	private CourseRepository courseRepository;

	@Test
	public void saveCourse() {
		List<Course> courses = courseRepository.findAll();
		System.out.println("courses = " + courses);
	}

@Component

  • It is an annotation that allows Spring to automatically detect our custom beans

  • Can only be annotate on a class

  • @Service and @Repository are special cases of @Component.

@Service

Service Components are the class file which contains @Service annotation. These class files are used to write business logic in a different layer, between @RestController class and @Repository

@Repository

@Repository annotates classes at the persistence layer, which will act as a database repository.

@Autowired

  • Enables automatic dependency injection. In other words, by declaring all the bean dependencies in a Spring configuration file, Spring container can auto wire relationships between collaborating beans. This is called Spring bean auto-wiring.

@Component
public class FooService {  
    @Autowired
    private IDependency dependency;
}

@Primary

  • There's another annotation called @Primary that we can use to decide which bean to inject when ambiguity is present regarding dependency injection.

  • This annotation defines a preference when multiple beans of the same type are present.

@EnableConfigurationProperties

@EnableConfigurationProperties(SecretConfigProp.class)

To load config from Class, refer below annotation

@ConfigurationProperties("secret")

@ConfigurationProperties("secret")
public record SecretConfigProp(String username, String password, String authToken) {
}

```spring-boot-properties
secret.authToken:my_auth_token
secret.username:myusername

spring.config.import:optional:secrets.properties
```

```java-properties
secret.password=password
```

PreviousSpring BeansNextImportant Spring Things

Last updated 2 years ago

Was this helpful?