CheatSheet

:eyes: μ•Œμ•„λ‘λ©΄ μœ μš©ν•˜κ²Œ μ‚¬μš©ν•  수 μžˆλŠ” 개발 κ΄€λ ¨ λ¬Έμ„œ 및 μƒ˜ν”Œ μ½”λ“œλ₯Ό μž‘μ„±ν•˜λŠ” ν”„λ‘œμ νŠΈμž…λ‹ˆλ‹€.

View project on GitHub

πŸ“ org.springframework.context.annotation

@Bean

λ©”μ†Œλ“œκ°€ Spring μ»¨ν…Œμ΄λ„ˆκ°€ 관리 ν•  Bean을 생성 함을 λ‚˜νƒ€λƒ…λ‹ˆλ‹€.

@Target(value={METHOD,ANNOTATION_TYPE})
@Retention(value=RUNTIME)
@Documented
public @interface Bean

example

@Bean({"b1", "b2"}) // bean available as 'b1' and 'b2', but not 'myBean'
public MyBean myBean() {
    // instantiate and configure MyBean obj
    return obj;
}

@Configuration

ν΄λž˜μŠ€κ°€ ν•˜λ‚˜ μ΄μƒμ˜ @Bean λ©”μ†Œλ“œλ₯Ό μ„ μ–Έν•˜κ³  Spring μ»¨ν…Œμ΄λ„ˆκ°€ λŸ°νƒ€μž„μ— Bean μ •μ˜ 및 μ„œλΉ„μŠ€ μš”μ²­μ„ μƒμ„±ν•˜λ„λ‘ 처리 ν•  수 μžˆμŒμ„ λ‚˜νƒ€λƒ…λ‹ˆλ‹€.

@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Component
public @interface Configuration

example ```java @Configuration public class AppConfig {

@Bean
public MyBean myBean() {
    // instantiate, configure and return bean ...
} } ``` ### @PropertySource > Spring의 ν™˜κ²½μ— PropertySourceλ₯Ό μΆ”κ°€ν•˜κΈ°μœ„ν•œ νŽΈλ¦¬ν•˜κ³  선언적인 λ©”μ»€λ‹ˆμ¦˜μ„ μ œκ³΅ν•˜λŠ” μ• λ„ˆν…Œμ΄μ…˜μœΌλ‘œ @Configuration ν΄λž˜μŠ€μ™€ ν•¨κ»˜ μ‚¬μš©λ©λ‹ˆλ‹€. ```java @Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Repeatable(value=PropertySources.class) public @interface PropertySource ``` > example ```java @Configuration @PropertySource("classpath:/com/myco/app.properties") public class AppConfig {

@Autowired
Environment env;

@Bean
public TestBean testBean() {
    TestBean testBean = new TestBean();
    testBean.setName(env.getProperty("testbean.name"));
    return testBean;
} } ```

πŸ“ lombok

@Getter

λͺ¨λ“  ν•„λ“œμ— @Getter 및 / λ˜λŠ” @Setter에 주석을 달아 lombok이 κΈ°λ³Έ getter / setterλ₯Ό μžλ™μœΌλ‘œ μƒμ„±ν•˜λ„λ‘ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
public @interface Getter 

example ```java import lombok.AccessLevel; import lombok.Getter; import lombok.Setter;

public class GetterSetterExample { /** * Age of the person. Water is wet. * * @param age New value for this person’s age. Sky is blue. * @return The current value of this person’s age. Circles are round. */ @Getter @Setter private int age = 10;

/**
* Name of the person.
* -- SETTER --
* Changes the name of this person.
* 
* @param name The new value.
*/
@Setter(AccessLevel.PROTECTED) private String name;

@Override public String toString() {
    return String.format("%s (age: %d)", name, age);
} } ```