Spring Annatation의 종류와 그 역할에 대해 이해한다.

계속해서 추가할 예정!


@Component

@Controller, @Service, @Repository

@RequestMapping

@Controller
@RequestMapping("/home") // 1) Class Level
public class HomeController {
    /* an HTTP GET for /home */ 
    @RequestMapping(method = RequestMethod.GET) // 2) Handler Level
    public String getAllEmployees(Model model) {
        ...
    }
    /* an HTTP POST for /home/employees */ 
    @RequestMapping(value = "/employees", method = RequestMethod.POST) 
    public String addEmployee(Employee employee) {
        ...
    }
}

@RestController

@Required

<!-- Definition for student bean -->
<bean id = "student" class = "com.tutorialspoint.Student">
    <property name = "name" value = "Zara" />
    <property name = "age"  value = "11"/>
</bean>

@Autowired

@Qualifier

@Resource


[Data Validation]

@Vaild


[Configuration]

@Configuration

@EnableWebSecurity

@SpringBootApplication

@EnableWebMvc


@RestControllerAdvice

@ExceptionHandler

@ResponseStatus


[Parameter를 받는 방법]

@RequestParam

@PathVariable

참고 @RequestParam와 @PathVariable 동시 사용 예제

@GetMapping("/user/{userId}/invoices")
public List<Invoice> listUsersInvoices(@PathVariable("userId") int user,
	                                  @RequestParam(value = "date", required = false) Date dateOrNull) {
}

@RequestBody

@ModelAttribute


[공용 생성일/수정일을 위한 Annotation]

@EnableJpaAuditing

@MappedSuperclass

@EntityListeners(AuditingEntityListener.class)

@CreatedDate

@LastModifiedDate

@Transactional


[JPA에서 제공하는 Annotation]

@Table

@Entity

@Id

@GeneratedValue

@Column


[Lombok Library Annotation]

@NoArgsConstructor

@AllArgsConstructor

@RequiredArgsConstructor

@Getter

@Setter

@ToString

@EqualsAndHashCode

User user1 = new User();
user1.setId(1L);
user1.setUsername("user");
user1.setPassword("pass");

User user2 = new User();
user1.setId(2L); // 부모 클래스의 필드가 다름
user2.setUsername("user");
user2.setPassword("pass");

user1.equals(user2);
// callSuper = true 이면 false, callSuper = false 이면 true

@Builder

@Data


[Java Config를 위한 Annotation]

@Configuration

@EnableTransactionManagement

@PropertySource(“classpath:application-properties.xml”)


[Spring AOP를 위한 Annotation]

@EnableAspectJAutoProxy

@Aspect

@Aspect
public class Logger {
    @Pointcut("execution( void spring.aop.*.sound())")
    private void selectSound() {}  // signature

    @Before("selectSound()")
    public void aboutToSound() {
        System.out.println("before advice: about to sound() ");
    }

    @After("selectSound()")
    public void afterSound() {
        System.out.println("after advice: after sound() ");
    }
}

@PointCut

@Before (이전)

@After (이후)

@Around (메소드 실행 전후)

@AfterReturning (정상적 반환 이후)

@AfterThrowing (예외 발생 이후)


관련된 Post

References