Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

waitedForU

[27][spring] Annotation 설정, component-scan 본문

Mvc_Spring

[27][spring] Annotation 설정, component-scan

Mr.Bini 2016. 5. 27. 10:03

[01] Annotation 설정  

Annotation 설정은  코드에 설정정보를 입력함으로써 설정파일을 사용하지 않거나  
  설정파일 크기를 줄이는데 유용합니다. 


  • @Autowired               -  의존하는 객체 주입 (필드,메소드/setter,생성자 에서 사용)        
                                         DI객체가 중복될때 Qualifier("") 사용
  • @Resource                 -  의존하는 객체 주입(필드,setter 에서 사용)
                                         DI객체가 중복될때 Resource(name="") 사용
  • @PostConstruct          -  객체 생성후 호출
  • @PreDestroy              -  객체가 소멸되기 바로전 호출
  • @Qualifier                 -  자동설정 제한하며, 수식어를 값으로 가짐
  • @Required                -  필수 프로퍼티를 명시 (setter 메소드에서 사용)                    

 

1.
 Annotation 예제 확인

      -  스프링 프로젝트의 설정 

        Project Type: Spring  Project --> Simple Spring Utility Project    
                Name: AnnoTest
             Package: spring.sts.annotation 
             Library: 프로젝트 생성시 관련 Spring 라이브러리가 자동으로 다운로드  
                      됩니다. 작업 컴퓨터는 인터넷에 연결되어 있어야 합니다. 



  packate: spring.sts.annotation


import javax.annotation.PostConstruct;

import javax.annotation.PreDestroy;
/**
 * 
 * @author Apurav
 *
 */
public class Foo {
    private String name;
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getName() {
        return name;
    }
 
    @PostConstruct
    public void init() {
        System.out.println("bean successfully initialized");
    }
    @PreDestroy
    public void cleanUp() {
        System.out.println("clean up called");
    }
 }
 


 
    


import javax.annotation.Resource;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
/**
 * 
 * @author AHN
 *
 */
public class Bar {
    @Autowired
    @Qualifier(value="secondaryFoo")
    private Foo foo;
    
    @Resource(name="foo")
    private Foo foo2;
    
    //@Required
    public void setFoo(Foo foo) {
        this.foo = foo;
    }
    public void  printFooName(){
        System.out.println(foo.getName());
        System.out.println(foo2.getName());
    }
    
}





import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 
 * @author Apurav
 *
 */
public class TestFooBar {
 
    public static void main(String[] args) throws InterruptedException {
 
 
        AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "annotation.xml");
        Bar bar = applicationContext.getBean("bar", Bar.class);
        bar.printFooName();
        applicationContext.registerShutdownHook();
         
    }
 
}




>>>>>> annotation.xml


<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 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
    <context:annotation-config />
 
    <bean id="foo" class="spring.sts.annotation.Foo">
        <property name="name" value="Apurav"></property>
    </bean>
    <bean id="anotherFoo" class="spring.sts.annotation.Foo">
        <property name="name" value="AHN"></property>
        <qualifier value="secondaryFoo"></qualifier>
    </bean>
    <bean id="bar" class="spring.sts.annotation.Bar" />
</beans>




 
  • @Component           @Repository, @Service 를 포괄적으로 사용 
  • @Controller             - @Component가 대신 사용될수 없음

 

         




  • @Service             -  business logic 의 서비스 패턴 클래스 에서 사용
  • @Repository        -  database access logic의  DAO 패턴 클래스 에서 사용




[02] xml설정파일에 등록없이 빈으로 사용가능한 스캔 

1. <context:component-scan> 

- @Component, @Service, @Controller가 적용된 클래스를 자동으로 등록해줍니다. 

<?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  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 


<context:component-scan base-package="spring.sts.annotation.*" /> 

</beans> 

 

2. <context:include-filter> 
   <context:exclude-filter> 

- 자동스캔대상에 포함시킬 클래스와 포함시키지 않을 클래스를 구체적으로  
  명시할 수 있습니다. 

<context:component-scan base-package="sts.hello.semple"> 
<context:include-filter type="regex" expression=" 
.*Mgr"/> 
<context:exclude-filter type="aspectj" expression=" 
..*Mgr"/> 
</context:component-scan> 

- regex:정규표현식 
- aspectj:Aspectj표현식 

Comments