관리 메뉴

HAMA 블로그

자바 어노테이션의 모든것 - (4) 본문

Java

자바 어노테이션의 모든것 - (4)

[하마] 이승현 (wowlsh93@gmail.com) 2015. 7. 5. 16:58


4. 스프링 설정  :   어노테이션  vs   XML


MyBean.java 가  XML/어노테이션/자바 컨피그레이션을 통해   어떻게 객체로 만들어지는지 살펴보도록하자.


( 1 ) XML 을 통한 설정 

Beans:

스프링 컨테이너 안에 만들고싶은 자바 클래스들 ("benas") 이다.  두번째 빈은 첫번째 빈을 DI 받고 있다.

  • MyBean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.hmkcode.spring.beans;
 
public class MyBean {
 
    private String name;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    @Override
    public String toString() {
        return "MyBean [name=" + name + "]";
    }
 
}
  • AnotherBean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.hmkcode.spring.beans;
 
public class AnotherBean {
 
    private MyBean myBean;
 
    public MyBean getMyBean() {
        return myBean;
    }
 
    public void setMyBean(MyBean myBean) {
        this.myBean = myBean;
    }
 
    @Override
    public String toString() {
        return "AnotherBean [myBean=" + myBean + "]";
    }  
}

XML 설정 :

  • src/main/resources/config/XMLConfig.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
       xsi:schemaLocation="http://www.springframework.org/schema/beans
 
 
    <bean id="myBean" class="com.hmkcode.spring.beans.MyBean">
        <property name="name" value="xmlBean" />
    </bean>
 
    <bean id="anotherBean" class="com.hmkcode.spring.beans.AnotherBean">
                <property name="myBean" ref="myBean" />
        </bean>
</beans>

위의 설정은 아래와 동등하다.

MyBean myBean = new MyBean(); 
myBean.setName("xmlBean");

AnotherBean anotherBean = new AnotherBean();
anotherBean.setMyBean(mybean);

Run:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.hmkcode;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.hmkcode.spring.beans.AnotherBean;
import com.hmkcode.spring.beans.MyBean;
 
public class App
{
    public static void main( String[] args )
    {
        ApplicationContext ctxXML = new ClassPathXmlApplicationContext("config/XMLConfig.xml");
        AnotherBean anotherBean = (AnotherBean) ctxXML.getBean("anotherBean");
 
        System.out.println( anotherBean);
    }
}

Output:

AnotherBean [myBean=MyBean [name=xmlBean]]


( 2 ) 어노테이션 기반 설정 

Beans + Annotation:

  • MyBean.java
  • @Component(value=“myBean2″) : this will enable Spring to detect this bean when scanning and instantiate a bean named “myBean2
  • @Value(value=“AnnotationBean”) : this equivalent to mybean2.setName(“AnnotationBean”);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.hmkcode.spring.beans;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component(value="myBean2")
public class MyBean {
 
    private String name;
 
    public String getName() {
        return name;
    }
 
    @Value(value="AnnotationBean")
    public void setName(String name) {
        this.name = name;
    }
 
    @Override
    public String toString() {
        return "MyBean [name=" + name + "]";
    }
 
}
  • AnotherBean.java
  • @Component(value=“anotherBean”) : this will enable Spring to detect this bean when scanning and instantiate a bean named “anotherBean”
  • @Autowired @Qalifier(“myBean2″): this equivalent to anotherBean.setMyBean(mybean2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.hmkcode.spring.beans;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
 
@Component(value="anotherBean")
public class AnotherBean {
 
    private MyBean myBean;
 
    public MyBean getMyBean() {
        return myBean;
    }
 
    @Autowired
    @Qualifier("myBean2")
    public void setMyBean(MyBean myBean) {
        this.myBean = myBean;
    }
 
    @Override
    public String toString() {
        return "AnotherBean [myBean=" + myBean + "]";
    }
 
}

XML 설정: (annotation 이 사용가능하도록) 

 단지 어노테이션을 활성화 하기위해 XML 이 필요하다. XML 안에 따로 <bean> 같은 태그를 사용하지 않았다.

  • XMLConfig-Annotation.xml
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
 
<beans     xmlns="http://www.springframework.org/schema/beans"      
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
        xmlns:context="http://www.springframework.org/schema/context"    
        xsi:schemaLocation="http://www.springframework.org/schema/beans
      <context:component-scan base-package="com.hmkcode.spring.beans"/>
      <!-- The use of <context:component-scan> implicitly enables the functionality of <context:annotation-config> -->
</beans>

Run:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.hmkcode;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hmkcode.spring.beans.AnotherBean;
 
public class App
{
    public static void main( String[] args )
    {
 
        ApplicationContext ctxAnnotation = new ClassPathXmlApplicationContext("config/XMLConfig-Annotation.xml");
        AnotherBean anotherBean  = (AnotherBean) ctxAnnotation.getBean("anotherBean");
 
        System.out.println( anotherBean);
 
    }
}

Output:

AnotherBean [myBean=MyBean [name=AnnotationBean]]


( 3 ) 자바를 이용한 설정 (XML 제거됨) 

 Beans:

  • MyBean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.hmkcode.spring.beans;
 
import org.springframework.beans.factory.annotation.Value;
 
public class MyBean {
 
    private String name;
 
    public String getName() {
        return name;
    }
 
    @Value(value="JavaConfigBean")
    public void setName(String name) {
        this.name = name;
    }
 
    @Override
    public String toString() {
        return "MyBean [name=" + name + "]";
    }
 
}
  • AnotherBean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.hmkcode.spring.beans;
 
import org.springframework.beans.factory.annotation.Autowired;
 
public class AnotherBean {
 
    private MyBean myBean;
 
    public MyBean getMyBean() {
        return myBean;
    }
 
    @Autowired
    public void setMyBean(MyBean myBean) {
        this.myBean = myBean;
    }
 
    @Override
    public String toString() {
        return "AnotherBean [myBean=" + myBean + "]";
    }
 
}

Cofiguration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.hmkcode.spring.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.hmkcode.spring.beans.AnotherBean;
import com.hmkcode.spring.beans.MyBean;
 
@Configuration
public class JavaConfig {
 
    @Bean
    public MyBean myBean(){
        return  new MyBean();
    }
 
    @Bean(name = "anotherBean2")
    public AnotherBean anotherBean(){
        return new AnotherBean();
    }
}

Run:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.hmkcode;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.hmkcode.spring.beans.AnotherBean;
import com.hmkcode.spring.config.JavaConfig;
 
public class App
{
    public static void main( String[] args )
    {
 
        ApplicationContext ctxJavaConfig = new AnnotationConfigApplicationContext(JavaConfig.class);
        AnotherBean anotherBean = (AnotherBean) ctxJavaConfig.getBean("anotherBean2");
 
        System.out.println( anotherBean);
 
    }
}

Output:

AnotherBean [myBean=MyBean [name=JavaConfigBean]]




AnnotationConfigApplicationContext 분석 

@Configuration
public class AnnotatedHelloConfig{
    @bean
    public AnnotatedHello annotatedHello(){
        return new AnnotatedHello();

}

}  

....

  ApplicationContext ctx=new AnnotationConfigApplicationContext(AnnotatedHelloConfig.class);

  AnnotatedHello hello =ctx.getBean("annotatedHello", AnnotatedHello .class);

  

위의 코드는 @Configuration 으로 <beans> 를 대신하고 
@bean 으로 자바코드에 의한 빈등록을 하게 해주는 예이다.

 AnnotationConfigApplicationContext  을 이용하여 설정 클래스를 입력받은후에 
 getBean 메소드를 통하여 내부의 특정 객체를 가져오는 소스이다.


AnnotationConfigApplicationContext  인사이드를 보도록 하자.

(분석중..) 


Reference 

http://tutorials.jenkov.com/java-reflection/annotations.html

http://tutorials.jenkov.com/java/annotations.html

http://www.javacodegeeks.com/2014/11/java-annotations-tutorial.html

http://hmkcode.com/spring-configuration-xml-annotation-java/



Comments