├── README.md
├── annotations.pptx
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── javatechie
│ │ └── spring
│ │ ├── AnnotationCheatsheetApplication.java
│ │ ├── AppError.java
│ │ ├── EagerLoadingBean.java
│ │ ├── LazyLoadingBean.java
│ │ ├── TestBean.java
│ │ ├── advice
│ │ └── StudentExceptionHandler.java
│ │ ├── config
│ │ ├── BeanConfig.java
│ │ ├── DBConfig.java
│ │ └── MailProps.java
│ │ ├── controller
│ │ └── StudentController.java
│ │ ├── entity
│ │ ├── Courcse.java
│ │ └── Student.java
│ │ ├── exception
│ │ └── StudentNotFoundException.java
│ │ ├── repository
│ │ └── StudentRepository.java
│ │ └── service
│ │ ├── StudentService.java
│ │ ├── StudentServiceImpl.java
│ │ └── StudentServiceImplV2.java
└── resources
│ ├── application-dev.properties
│ ├── application-prod.properties
│ ├── application-stg.properties
│ ├── application.properties
│ └── custom.properties
└── test
└── java
└── com
└── javatechie
└── spring
└── AnnotationCheatsheetApplicationTests.java
/README.md:
--------------------------------------------------------------------------------
1 | # annotations-cheatsheet
2 |
3 | # Spring, Spring Boot Annotations Cheat sheet
4 |
5 | ### Spring Boot Main annotations
6 | - @SpringBootApplication
7 | - @ComponentScan
8 | - @EanbleAutoConfiguration
9 | - @Configuration
10 |
11 | ### Stereotype annotation
12 | - @Component
13 | - @Controller
14 | - @Service
15 | - @Repository
16 |
--------------------------------------------------------------------------------
/annotations.pptx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Java-Techie-jt/annotations-cheatsheet/a8489f968bd5ed3dd8a9f37a95c0918c3cca6194/annotations.pptx
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.6.4
9 |
10 |
11 | com.javatechie
12 | annotation-cheatsheet
13 | 0.0.1-SNAPSHOT
14 | annotation-cheatsheet
15 | Spring, Spring Boot Annotations Cheat sheet
16 |
17 | 1.8
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-starter-data-jpa
23 |
24 |
25 | org.springframework.boot
26 | spring-boot-starter-web
27 |
28 |
29 |
30 | com.h2database
31 | h2
32 | runtime
33 |
34 |
35 | org.projectlombok
36 | lombok
37 | true
38 |
39 |
40 | org.springframework.boot
41 | spring-boot-starter-test
42 | test
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 | org.springframework.boot
51 | spring-boot-maven-plugin
52 |
53 |
54 |
55 | org.projectlombok
56 | lombok
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/AnnotationCheatsheetApplication.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring;
2 |
3 | import com.javatechie.spring.entity.Student;
4 | import com.javatechie.spring.repository.StudentRepository;
5 | import org.springframework.boot.SpringApplication;
6 | import org.springframework.boot.autoconfigure.SpringBootApplication;
7 |
8 | import javax.annotation.PostConstruct;
9 | import java.util.stream.Collectors;
10 | import java.util.stream.Stream;
11 |
12 | @SpringBootApplication
13 | public class AnnotationCheatsheetApplication {
14 |
15 | private StudentRepository studentRepository;
16 |
17 | public AnnotationCheatsheetApplication(StudentRepository studentRepository) {
18 | this.studentRepository = studentRepository;
19 | }
20 |
21 | @PostConstruct
22 | public void initStudents() {
23 | studentRepository.saveAll(Stream.of(
24 | new Student(101, "Basant", 14, "Science"),
25 | new Student(102, "Santosh", 48, "Arts"),
26 | new Student(103, "Rajesh", 16, "Commerce"),
27 | new Student(104, "Sam", 12, "Arts"))
28 | .collect(Collectors.toList()));
29 | }
30 |
31 | public static void main(String[] args) {
32 | SpringApplication.run(AnnotationCheatsheetApplication.class, args);
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/AppError.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import org.springframework.http.HttpStatus;
6 | @Data
7 | @AllArgsConstructor
8 | public class AppError {
9 | private String errorCode;
10 | private String message;
11 | private HttpStatus httpStatus;
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/EagerLoadingBean.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring;
2 |
3 | import org.springframework.stereotype.Component;
4 |
5 | @Component
6 | public class EagerLoadingBean {
7 |
8 | public EagerLoadingBean() {
9 | System.out.println("EagerLoadingBean object created ..");
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/LazyLoadingBean.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring;
2 |
3 | import org.springframework.context.annotation.Lazy;
4 | import org.springframework.stereotype.Component;
5 |
6 | @Component
7 | @Lazy
8 | public class LazyLoadingBean {
9 |
10 | public LazyLoadingBean() {
11 | System.out.println("LazyLoadingBean object created ..");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/TestBean.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring;
2 |
3 | import org.springframework.stereotype.Component;
4 |
5 | public class TestBean {
6 |
7 |
8 | public void method(){
9 | System.out.println("TestBean method logic excuted ");
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/advice/StudentExceptionHandler.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring.advice;
2 |
3 | import com.javatechie.spring.AppError;
4 | import com.javatechie.spring.exception.StudentNotFoundException;
5 | import org.springframework.http.HttpStatus;
6 | import org.springframework.http.ResponseEntity;
7 | import org.springframework.web.bind.annotation.ExceptionHandler;
8 | import org.springframework.web.bind.annotation.RestControllerAdvice;
9 |
10 | import java.util.UUID;
11 |
12 | @RestControllerAdvice
13 | public class StudentExceptionHandler {
14 |
15 | @ExceptionHandler(StudentNotFoundException.class)
16 | public ResponseEntity handleException(StudentNotFoundException exception) {
17 | AppError error=new AppError(UUID.randomUUID().toString(),
18 | exception.getMessage(),
19 | HttpStatus.INTERNAL_SERVER_ERROR);
20 | return new ResponseEntity<>(error,HttpStatus.INTERNAL_SERVER_ERROR);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/config/BeanConfig.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring.config;
2 |
3 | import com.javatechie.spring.TestBean;
4 | import org.springframework.context.annotation.Bean;
5 | import org.springframework.context.annotation.Configuration;
6 | import org.springframework.web.client.RestTemplate;
7 |
8 | @Configuration
9 | public class BeanConfig {
10 |
11 | @Bean
12 | public TestBean testBean(){
13 | return new TestBean();
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/config/DBConfig.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring.config;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.context.annotation.Bean;
5 | import org.springframework.context.annotation.Configuration;
6 | import org.springframework.context.annotation.Profile;
7 |
8 | @Configuration
9 | public class DBConfig {
10 |
11 | @Value("${db.driverClass}")
12 | private String driverClass;
13 | @Value("${db.url}")
14 | private String url;
15 | @Value("${db.username}")
16 | private String username;
17 | @Value("${db.password}")
18 | private String password;
19 |
20 | @Bean
21 | @Profile("prod")
22 | public String dataSourceProps() {
23 | System.out.println(driverClass + " : " + url + " : " + username + " : " + password);
24 | return "dataSource connection for dev";
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/config/MailProps.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring.config;
2 |
3 | import lombok.Data;
4 | import org.springframework.boot.context.properties.ConfigurationProperties;
5 | import org.springframework.stereotype.Component;
6 |
7 | @ConfigurationProperties(prefix = "mail")
8 | @Data
9 | @Component
10 | public class MailProps {
11 |
12 | private String from;
13 | private String host;
14 | private String port;
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/controller/StudentController.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring.controller;
2 |
3 | import com.javatechie.spring.LazyLoadingBean;
4 | import com.javatechie.spring.TestBean;
5 | import com.javatechie.spring.config.MailProps;
6 | import com.javatechie.spring.entity.Student;
7 | import com.javatechie.spring.exception.StudentNotFoundException;
8 | import com.javatechie.spring.service.StudentService;
9 | import org.springframework.beans.factory.annotation.Autowired;
10 | import org.springframework.beans.factory.annotation.Qualifier;
11 | import org.springframework.beans.factory.annotation.Value;
12 | import org.springframework.context.annotation.PropertySource;
13 | import org.springframework.context.annotation.Scope;
14 | import org.springframework.http.ResponseEntity;
15 | import org.springframework.web.bind.annotation.*;
16 |
17 | import java.util.List;
18 | import java.util.Optional;
19 |
20 | @RestController
21 | @RequestMapping("/students")
22 | @PropertySource("classpath:custom.properties")
23 | @Scope("prototype")
24 | public class StudentController {
25 |
26 | @Autowired
27 | private StudentService studentService;
28 |
29 | @Autowired
30 | private TestBean testBean;
31 |
32 | @Autowired
33 | private LazyLoadingBean lazyLoadingBean;
34 |
35 | @Value("${mail.from}")
36 | private String from;
37 | @Value("${mail.host}")
38 | private String host;
39 | @Value("${mail.port}")
40 | private String port;
41 | @Value("${message}")
42 | private String message;
43 |
44 | @Autowired
45 | private MailProps mailProps;
46 |
47 | public StudentController() {
48 | System.out.println("controller object created ....");
49 | }
50 |
51 | @PostMapping("/save")
52 | public ResponseEntity addStudent(@RequestBody Student student) {
53 | return ResponseEntity.ok(studentService.addStudent(student));
54 | }
55 |
56 | @GetMapping("/{id}")
57 | public ResponseEntity> getStudent(@PathVariable int id) throws StudentNotFoundException {
58 | Optional student = studentService.getStudent(id);
59 | if (student.isPresent()) {
60 | return ResponseEntity.ok(student);
61 | } else {
62 | throw new StudentNotFoundException("student not found with id " + id);
63 | }
64 |
65 | }
66 |
67 | @GetMapping("/all")
68 | public ResponseEntity> getStudents() {
69 | System.out.println("mail properties : "+ mailProps);
70 | return ResponseEntity.ok(studentService.getStudents());
71 | }
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/entity/Courcse.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring.entity;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 | import javax.persistence.Entity;
8 | import javax.persistence.Id;
9 | import javax.persistence.ManyToOne;
10 | import javax.persistence.Table;
11 |
12 | @Entity
13 | @Table(name = "STUDENT_TBL")
14 | @Data
15 | @AllArgsConstructor
16 | @NoArgsConstructor
17 | public class Courcse {
18 | @Id
19 | private int id;
20 |
21 | private String courseName;
22 |
23 | private Student student;
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/entity/Student.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring.entity;
2 |
3 |
4 | import lombok.AllArgsConstructor;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 | import org.hibernate.annotations.GeneratorType;
8 |
9 | import javax.persistence.*;
10 | import java.util.List;
11 |
12 | @Entity
13 | @Table(name = "STUDENT_TBL")
14 | @Data
15 | @AllArgsConstructor
16 | @NoArgsConstructor
17 | public class Student {
18 |
19 | @Id
20 | @Column(name = "STUDENT_ID")
21 | private int id;
22 | @Column(name = "STUDENT_NAME")
23 | private String name;
24 | @Column(name = "ROLL_NO")
25 | private int rollNo;
26 | @Column(name = "DEPT")
27 | private String dept;
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/exception/StudentNotFoundException.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring.exception;
2 |
3 | public class StudentNotFoundException extends Exception{
4 |
5 | public StudentNotFoundException(String message) {
6 | super(message);
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/repository/StudentRepository.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring.repository;
2 |
3 | import com.javatechie.spring.entity.Student;
4 | import org.springframework.data.jpa.repository.JpaRepository;
5 | import org.springframework.stereotype.Component;
6 | import org.springframework.stereotype.Controller;
7 | import org.springframework.stereotype.Repository;
8 | import org.springframework.stereotype.Service;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 |
12 | @Repository
13 | public interface StudentRepository extends JpaRepository {
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/service/StudentService.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring.service;
2 |
3 | import com.javatechie.spring.entity.Student;
4 |
5 | import java.util.List;
6 | import java.util.Optional;
7 |
8 |
9 | public interface StudentService {
10 |
11 | public Student addStudent(Student student);
12 |
13 | public Optional getStudent(int id);
14 |
15 | public List getStudents();
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/service/StudentServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring.service;
2 |
3 | import com.javatechie.spring.entity.Student;
4 | import com.javatechie.spring.repository.StudentRepository;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.context.annotation.Primary;
7 | import org.springframework.stereotype.Service;
8 | import org.springframework.transaction.annotation.Isolation;
9 | import org.springframework.transaction.annotation.Propagation;
10 | import org.springframework.transaction.annotation.Transactional;
11 |
12 | import java.util.List;
13 | import java.util.Optional;
14 |
15 | @Service
16 | @Primary
17 | public class StudentServiceImpl implements StudentService{
18 |
19 | @Autowired
20 | private StudentRepository repository;
21 |
22 |
23 | @Override
24 | public Student addStudent(Student student) {
25 | return repository.save(student);
26 | }
27 |
28 | @Override
29 | public Optional getStudent(int id) {
30 | return repository.findById(id);
31 | }
32 |
33 | @Override
34 | public List getStudents() {
35 | return repository.findAll();
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/spring/service/StudentServiceImplV2.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring.service;
2 |
3 | import com.javatechie.spring.entity.Student;
4 | import org.springframework.context.annotation.Primary;
5 | import org.springframework.stereotype.Service;
6 |
7 | import java.util.List;
8 | import java.util.Optional;
9 |
10 | @Service
11 | public class StudentServiceImplV2 implements StudentService{
12 |
13 | @Override
14 | public Student addStudent(Student student) {
15 | return null;
16 | }
17 |
18 | @Override
19 | public Optional getStudent(int id) {
20 | return Optional.empty();
21 | }
22 |
23 | @Override
24 | public List getStudents() {
25 | return null;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/resources/application-dev.properties:
--------------------------------------------------------------------------------
1 | # DEVELOPMENT DB config properties
2 |
3 | db.driverClass= dev db driver class
4 | db.url= dev db url
5 | db.username= dev db username
6 | db.password=dev db encrypted password
--------------------------------------------------------------------------------
/src/main/resources/application-prod.properties:
--------------------------------------------------------------------------------
1 | # PROD DB config properties
2 |
3 | db.driverClass= prod db driver class
4 | db.url= prod db url
5 | db.username= prod db username
6 | db.password=prod db encrypted password
--------------------------------------------------------------------------------
/src/main/resources/application-stg.properties:
--------------------------------------------------------------------------------
1 | # STAGE DB config properties
2 |
3 | db.driverClass= stg db driver class
4 | db.url= stg db url
5 | db.username= stg db username
6 | db.password=stg db encrypted password
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.h2.console.enabled=true
2 | spring.datasource.url=jdbc:h2:mem:testdb
3 | spring.profiles.active=prod
4 | #SMTP Configuration
5 |
6 | mail.from =test@test.com
7 | mail.host =test@test.com
8 | mail.port =25
9 |
--------------------------------------------------------------------------------
/src/main/resources/custom.properties:
--------------------------------------------------------------------------------
1 | message= loading from external props
--------------------------------------------------------------------------------
/src/test/java/com/javatechie/spring/AnnotationCheatsheetApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.spring;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest
7 | class AnnotationCheatsheetApplicationTests {
8 |
9 | @Test
10 | void contextLoads() {
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------