π 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);
} } ```