SPRING
2017.05.30 / 18:55

'Spring JavaConfig'¿¡ ÇØ´çµÇ´Â ±Û 7°Ç

Secutiry
Ãßõ ¼ö 236

Using Java Configuration



¿ø¹® : Chapter 7. Using Java Configuration

¾ÖÇø®ÄÉÀ̼ǿ¡¼­ ¾Ö³ëÅ×À̼ÇÀ» ¼³Á¤ ÆÄÀÏ·Î »ç¿ëÇÒ ¼ö ÀÖ´Â ¹æ¹ýÀ¸·Î ´ÙÀ½°ú °°Àº Ŭ·¡½º°¡ ÀÖ´Ù.

    * AnnotationApplicationContext      

ÀÌ Å¬·¡½º´Â Ant ½ºÅ¸ÀÏ ÆÐÅÏÀ» »ç¿ëÇÏ¿© ¾Ö³ëÅ×À̼ÇÀ» »ç¿ëÇÑ ¼³Á¤ Ŭ·¡½º À̸§À» ¹ÞÀ» ¼ö ÀÖ´Ù.

      ApplicationContext oneConfig = 
             new AnnotationApplicationContext(SimpleConfiguration.class.getName());
      ApplicationContext aBunchOfConfigs = 
             new AnnotationApplicationContext("**/configuration/*Configuration.class");

ÀÌ ¾ÖÇø®ÄÉÀÌ¼Ç ÄÁÅؽºÆ®´Â ÁÖ¾îÁø ÆÐÅÏ¿¡ ¸ÅĪµÇ´Â Ŭ·¡½º Æнº ³»ÀÇ Å¬·¡½ºµéÀ» ÀÚµ¿À¸·Î ÀоîµéÀÌ°í ºóÀ¸·Î µî·ÏÇÑ´Ù. ÀÌ ¹æ¹ýÀÇ ´ÜÁ¡Àº ¼³Á¤ °´Ã¼¸¦ parameterization ÇÒ ¼ö ¾ø´Ù´Â °ÍÀÌ´Ù.

    * Configuration post processor

      <beans>
       <!-- Spring configuration -->
       <bean class="org.springframework.samples.petclinic.JdbcConfiguration"/>

       <!-- Java Configuration post processor -->
       <bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/>
      </beans>

µÎ ¹ø° ¹æ¹ýÀº ¼³Á¤ ó¸® »Ó¸¸ ¾Æ¸®³ª (ConfigurationPostProcessor¸¦ ÅëÇؼ­) ¼³Á¤ °´Ã¼ ÀÚüµµ Á¦¾î Çϱ⠶§¹®¿¡ º¸´Ù ¸¹Àº ¼³Á¤ ¿É¼ÊÀ» Á¦°øÇÑ´Ù.

¼³Á¤À» ºóÀ¸·Î Á¤ÀÇÇÔÀ¸·Î½á, Spring ÄÁÅ×À̳ʴ ¼³Á¤À» (¼Ó¼º ¶Ç´Â ƯÁ¤ »ý¼ºÀÚ¸¦ »ç¿ëÇؼ­)¼³Á¤ÇÒ ¼ö ÀÖ´Ù.

      <beans>

       <!-- a possible configurable configuration -->
       <bean class="org.my.company.config.AppConfiguration">
          <property name="env" value="TESTING"/>
          <property name="monitoring" value="true"/>
          <property name="certificates" value="classpath:/META-INF/config/MyCompany.certs"/>
       </bean>

       <!-- Java Configuration post processor -->
       <bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/>

      </beans>
½Å°í

'Spring JavaConfig > Chapter 7' Ä«Å×°í¸®ÀÇ ´Ù¸¥ ±Û

Using Java Configuration  (0)2007.07.25
top

Mixing XML and annotations



¿ø¹® : Chapter 6. Mixing XML and annotations

Java ¼³Á¤°ú XML ¼³Á¤Àº »óÈ£ º£Å¸ÀûÀÌÁö ¾Ê´Ù. ÇϳªÀÇ Spring ¾ÖÇø®ÄÉÀ̼ǿ¡¼­ ¸ðµÎ »ç¿ëµÉ ¼ö ÀÖ´Ù. XML ÆÄÀÏ¿¡ ÀÖ´Â beanÀ» °¡Á®¿À±â À§Çؼ­´Â, Spring ÄÁÅ×À̳ʸ¦ »ç¿ëÇØ¾ß ÇÑ´Ù. ÀÌÀü¿¡ ¾ð±ÞÇß´Ù½ÃÇÇ, @ExternalBean ¾Ö³ëÅ×À̼Ç(ÃßõÇÏ´Â ¹æ½Ä)À» »ç¿ëÇÏ¿© ±×·¸°Ô ÇÒ ¼ö ÀÖ´Ù. ÀÌ·¸°Ô ÇÏ°í ½ÍÁö ¾Ê°Å³ª ÀÌ·± ¹æ¹ýÀÌ ºÎÀûÀýÇÑ °æ¿ì³×´Â, @Configuration Ŭ·¡½º¿¡ÀÇÇØ »ç¿ëµÇ´Â beanFacotry¿¡ Á¢±ÙÇÒ ¼ö ÀÖ´Ù. ÀÌ·¸°Ô ÇÏ·Á¸é, ¼³Á¤ Ŭ·¡½º°¡ ConfigurationSupport ¸¦ »ó¼ÓÇϰųª BeanFacrtoryAware ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇϵµ·Ï ÇÏ¸é µÈ´Ù.

´ÙÀ½ÀÇ XML ¼³Á¤À» »ìÆ캸ÀÚ.

<bean id="myBean" class="MyBean"/>

myBeanÀ» Java¿¡¼­ ÂüÁ¶ÇÏ·Á¸é ´ÙÀ½°ú °°ÀÌ ÀÛ¼ºÇÒ ¼ö ÀÖ´Ù.

@Configuration
public class MyConfig extends ConfigurationSupport {

  @Bean
  public ExampleBean anotherBean() {
     ExampleBean bean = new ExampleBean("anotherBean");
     bean.setDep(getBean("myBean")); // use utility method to get a hold of 'myBean'
     return bean;
  }
}

@Configuration
public class MyOtherConfig implements BeanFactoryAware {
  private BeanFactory beanFactory;

  public void setBeanFactory(BeanFactory beanFactory) {
     // get access to the owning bean factory
     this.beanFactory = beanFactory;
  }

  @Bean
  public ExampleBean yetAnotherBean() {
     ExampleBean bean = new ExampleBean("yetAnotherBean");
     bean.setDep(beanFactory.getBean("myBean")); // use dependency lookup
     return bean;
  }
}

´Ù½Ã ÇÑ ¹ø °­Á¶ÇÏÁö¸¸, ConfigurationSupport³ª BeanFacrtoryAware¸¦ »ç¿ëÇϱâ Àü¿¡ ¶È°°Àº ±â´ÉÀ» Á¦°øÇÏ´Â (offers the same capability in a refactoring friendly manner) @ExternalBeanÀ» »ç¿ëÇÏ°í ½ÍÁö´Â ¾ÊÀºÁö Àç°íÇϱ⠹ٶõ´Ù.

JavaConfig ¹èÆ÷ÆÇ¿¡ º¯°æµÈ Petclinic ¿¹Á¦¿¡ º¸¸é ¸î¸î XML ¼³Á¤ ºÎºÐÀ» Java¿Í Groovy·Î ±³Ã¼ÇÑ °ÍÀÌ ÀÖ´Ù. »ùÇà Æú´õ¸¦ »ìÆ캸±æ ¹Ù¶õ´Ù.
½Å°í

'Spring JavaConfig > Chapter 6' Ä«Å×°í¸®ÀÇ ´Ù¸¥ ±Û

Mixing XML and annotations  (0)2007.07.24
top

Naming strategy



¿ø¹® : Chapter 5. Naming strategy

Áö±Ý±îÁöÀÇ ¸ðµç ¿¹Á¦µéÀº, ¸Þ¼Òµå À̸§À¸·Î ¸Þ¼Òµå¸¦ È£ÃâÇÏ¿© beanÀ» ¹Þ¾Æ¿Ô´Ù.

@Configuration
public class ColoursConfiguration {
  // create a bean with name 'blue'
  @Bean
  public Color blue() {
    ...
  }
  ...
}

// dependency lookup for the blue colour
applicationContext.getBean("blue");

¸î¸î °æ¿ì¿¡´Â, ÀÌ·¯ÇÑ ³×ÀÌ¹Ö ½ºÅ°¸¶°¡ ÀûÇÕÇÏÁö ¾ÊÀ» ¼öµµ ÀÖ´Ù. Áï ¼­·Î ´Ù¸¥ ¼³Á¤ Ŭ·¡½º¿¡¼­ °°Àº À̸§ÀÇ Á¤ÀǸ¦ »ç¿ëÇÏ°í ÀÖ´Â °æ¿ì°¡ ±×·¸´Ù. ÀÌ·¯ÇÑ ³×ÀÌ¹Ö ÁöħÀ» »ç¿ëÇÏ´Â ÀÔÀå¿¡¼­ ¸¾´ë·Î º¯°æÇÒ ¼ö ÀÖµµ·Ï BeanNamingStrategy ÀÎÅÍÆäÀ̽º¸¦ Á¦°øÇÏ°í ÀÖ´Ù.

¹°·Ð, Äڵ带 ÀÛ¼ºÇϱâ Àü¿¡, SpringÀÌ ±âº»À¸·Î Á¦°øÇÏ°í ÀÖ´Â MethodNameStrategy ±¸ÇöüµéÀ» »ìÆ캸´Â °ÍÀÌ ÁÁ°Ú´Ù.

 <!-- Java Configuration post processor -->
 <bean class="org.springframework.config.java.process.ConfigurationPostProcessor">
    <property name="namingStrategy">
       <bean class="org.springframework.config.java.naming.MethodNameStrategy">
          <property name="prefix" value="CLASS"/>
       </bean>
    </property>
 </bean>

ÀÌ·± ¼³Á¤À» Çصθé, beanÀ» »ý¼ºÇÒ ¶§ ¸Þ¼Òµå À̸§¿¡ Ŭ·¡½º À̸§±îÁö ºÙ¿©ÁÖ°Ô µÈ´Ù.

// »õ·Î¿î ÀÛ¸í ÁöħÀ» »ç¿ëÇÏ¿© blue ºó °¡Á®¿À±â
applicationContext.getBean("ColoursConfiguration.blue");
½Å°í

'Spring JavaConfig > Chapter 5' Ä«Å×°í¸®ÀÇ ´Ù¸¥ ±Û

Naming strategy  (0)2007.07.24
top

Wire dependencies



¿ø¹® : Chapter 4. Wire dependencies

beanÀ» ¹­±â À§Çؼ­, °£´ÜÇÏ°Ô JavaÀÇ ¹®¹ýÀ» »ç¿ëÇÒ ¼ö ÀÖ´Ù.

@Bean(scope = DefaultScopes.SINGLETON)
public Person rod() {
  return new Person("Rod Johnson");
}

@Bean(scope = DefaultScopes.PROTOTYPE)
public Book book() {
  Book book = new Book("Expert One-on-One J2EE Design and Development");
  book.setAuthor(rod());  // rod() method is actually a bean reference !
  return book;
}

À§ÀÇ ¿¹Á¦¿¡¼­, Ã¥ÀÇ ÀúÀÚ´Â rod ¸Þ¼ÒµåÀÇ ¹Ýȯ°ªÀ» »ç¿ëÇÏ°í ÀÖ´Ù. ÇÏÁö¸¸, book°ú rod ¸ðµÎ @BeanÀ¸·Î ¸¶Å©µÈ ¸Þ¼Òµå±â ¶§¹®¿¡, SpringÀÇ °ü¸®ÇÏ´Â beanµéÀ» ¹ÝȯÇÏ°Ô µÇ¸ç, µû¶ó¼­ ÄÁÅ×À̳ÊÀÇ °ü¸® ´ë»óÀÌ µÈ´Ù. rod ºóÀº ½Ì±ÛÅæÀÌ°í book ºóÀº ÇÁ·ÎÅäŸÀÔÀÌ´Ù. ¼³Á¤ ÆÄÀÏÀ» ¸¸µé ¶§, SpringÀº ¾Ö³ëÅ×ÀÌ¼Ç ¹®¸ÆÀ» ¾Ë¾ÆÂ÷¸®°í rod() ¸Þ¼Òµå È£ÃâÀ» 'rod'¶ó´Â ºó nameÀ¸·Î ÂüÁ¶Çϵµ·Ï ÇÑ´Ù.

ÀÌ ÄÁÅ×À̳ʴ ¸Å¹ø book ºóÀ» ¿äûÇÒ ¶§ ¸¶´Ù ¶È°°Àº(½Ì±ÛÅæ) rod ºóÀ» ÂüÁ¶ÇÏ°í ÀÖ´Â ´Ù¸¥(ÇÁ·ÎÅäŸÀÔ) book ºóÀ» ¹ÝȯÇÒ °ÍÀÌ´Ù.

À§ÀÇ ÄÚµå´Â ¾Æ·¡¿Í µ¿ÀÏÇÏ´Ù:

<bean id="rod" class="Person" scope="singleton">
   <constructor-arg>Rod Johnson</constructor-arg>
</bean>

<bean id="book" class="Book" scope="prototype">
   <constructor-arg>Expert One-on-One J2EE Design and Development</constructor-arg>
   <property name="author" ref="rod"/>
</bean>

À§ÀÇ ¿¹Á¦´Â °¡Àå ÈçÈ÷ »ç¿ëÇÏ´Â scopeÀ» »ç¿ëÇÏ°í ÀÖÁö¸¸, ¾î¶² ŸÀÔÀÇ scopeÀÌ¶óµµ »ç¿ëÇÒ ¼ö ÀÖÀ½À» ¾Ë¾ÆµÎÀÚ.

@Bean (scope = "customer")
public Bag shopingBag() {
  return new Basket();
}

@Bean (scope = "shift")
public Manager shopManager() {
  ...
}
½Å°í

'Spring JavaConfig > Chapter 4' Ä«Å×°í¸®ÀÇ ´Ù¸¥ ±Û

Wire dependencies  (0)2007.07.24
top

Bean Visibility



¿ø¹® : Chapter 3. Bean Visibility

JavaConfigÀÇ ¸ÚÁø ±â´É Áß Çϳª´Â beanÀÇ °¡½Ã¼ºÀ» ¼³Á¤ÇÒ ¼ö ÀÖ´Ù´Â °ÍÀÌ´Ù. JavaConfig´Â ¸Þ¼Òµå Á¢±Ù Áö½ÃÀÚ¸¦ »ç¿ëÇÏ¿© ¸Þ¼Òµå¿¡¼­ ¹ÝȯÇÒ bean¿¡ Á¢±Ù ÇÒ ¼ö ÀÖ´ÂÁö ¾ø´ÂÁö ¿©ºÎ¸¦ °áÁ¤ÇÒ ¼ö ÀÖ´Ù. 

´ÙÀ½ÀÇ ¼³Á¤À» »ìÆ캸ÀÚ.

@Configuration
public abstract class VisibilityConfiguration {

  @Bean
  public Bean publicBean() {
     Bean bean = new Bean();
     bean.setDependency(hiddenBean());
     return bean;
  }
  
  @Bean
  protected HiddenBean hiddenBean() {
     return new Bean("protected bean");
  }

  @Bean
  private HiddenBean secretBean() {
     Bean bean = new Bean("private bean");
     // hidden beans can access beans defined in the 'owning' context
     bean.setDependency(outsideBean());
  }

  @ExternalBean
  public abstract Bean outsideBean()
}

À§ ¼³Á¤ ³»¿ëÀ» ¹ÙÅÁÀ¸·Î ´ÙÀ½°ú °°Àº XML ¼³Á¤ ÆÄÀÏÀ» »ç¿ëÇÒ ¼ö ÀÖ´Ù.(¿©·¯ ¼³Á¤ ÆÄÀÏ Á¾·ù¸¦ È¥¿ëÇÏ´Â Àü·«¿¡ ´ëÇؼ­´Â º» éÅÍ¿¡¼­ ´Ù·ê °ÍÀÌ´Ù.)

<beans>
 <!-- the configuration above -->
 <bean class="my.java.config.VisibilityConfiguration"/>

 <!-- Java Configuration post processor -->
 <bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/>

 <bean id="mainBean" class="my.company.Bean">
    <!-- this will work -->
    <property name="dependency" ref="publicBean"/>
    <!-- this will *not* work -->
    <property name="anotherDependency" ref="hiddenBean"/>
 </bean>
</beans>

À§ÀÇ ¼³Á¤¿¡¼­ ÇϳªÀÇ JavaConfig°¡ ¼³Á¤¿¡ Æ÷ÇԵǸç, 3°³ÀÇ ºóÀ» »ý¼ºÇÒ Ã¥ÀÓÀ» Áö°í ÀÖ´Ù. publicBean, hiddenBean ±×¸®°í secretBeanÀÌ´Ù. ÀÌ ¼¼°³ÀÇ beanµéÀº °¢ÀÚ ¼­·Î¸¦ ÂüÁ¶ÇÒ ¼ö´Â ÀÖÁö¸¸, ApplicationContext ÀÔÀå¿¡¼­´Â ¿ÀÁ÷ publicBean¸¸ ÂüÁ¶ÇÒ ¼ö ÀÖ´Ù. hiddenBean°ú secretBeanÀº Á¢±ÙÀÌ ¿ÀÁ÷ VisiblilityConfiguration ³»ºÎ·Î Á¦ÇѵǾî ÀÖ´Ù.

@Bean ¾Ö³ëÅ×À̼ÇÀÌ ´Þ¸® ¸Þ¼Òµå Áß¿¡ publicÀÌ ¾Æ´Ñ °Í(protected, private, defalut)µéÀº ¸ðµÎ '°¨ÃçÁø' beanÀ¸·Î »ý¼ºµÉ °ÍÀÌ´Ù. 

À§ÀÇ ¿¹Á¦¿¡¼­, mainBeanÀº publicBean°ú heddenBeanÀ» »ç¿ëÇÏ¿© ¼³Á¤ÇÏ°í ÀÖ´Ù. ÇÏÁö¸¸, hiddenBeanÀº ¼û°ÜÁ® Àֱ⠶§¹®¿¡ ·±Å¸Àӽÿ¡ ´ÙÀ½°ú °°Àº ¿¡·¯¸¦ ¹ß»ý½ÃŲ´Ù.

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'hiddenBean' is defined
 ...

°¡½Ã¼ºÀ» Á¦°øÇϱâ À§ÇØ, JavaConfig´Â Spring ÄÁÅ×À̳ʰ¡ Á¦°øÇÏ´Â application context »ó¼Ó±¸Á¶ÀÇ ÀåÁ¡À» ¹Þ¾Æµé¿´´Ù. ÀÚ½Ä ¾ÖÇø®ÄÉÀÌ¼Ç ÄÁÅ×À̳ʿ¡¼­ ¼û°ÜÁø ºóµéÀ» ¼ÒÀ¯ÇÏ°íÀִ  ºÎ¸ð ÄÁÅؽºÆ®¿¡ ÀÖ´Â ºóµé¿¡ Á¢±ÙÇÒ ¼ö ÀÖ´Ù. 
½Å°í

'Spring JavaConfig > Chapter 3' Ä«Å×°í¸®ÀÇ ´Ù¸¥ ±Û

Bean Visibility  (0)2007.07.24
top

Components



¿ø¹® : Chapter 2. Components

¾Ö³ëÅ×À̼ÇÀ» »ç¿ëÇÏ¿© ÀÚ¹Ù ±¸Á¶¸¦ °èÃþÈ­ ÇÏ´Â ÀÚ¹Ù ¼³Á¤Àº, °³¹ßÀÚµéÀÌ ÀÚ¹Ù¸¸À» »ç¿ëÇÏ¿© ºóÀ» »ý¼ºÇÏ°í ¼³Á¤ÇÒ ¼ö ÀÖµµ·Ï ÇÑ´Ù. °£´ÜÇÏ°Ô, °³¹ßÀÚ´Â ÀÚ¹Ù Äڵ带 »ç¿ëÇÏ¿© ºóÀ» ¸¸µé°í ¼³Á¤Çϸç, ÄÁÅ×À̳ʰ¡ ±×°ÍµéÀ» »ç¿ëÇϵµ·Ï Áö½ÃÇÑ´Ù. °è¼Ó ÁøÇàÇϱâ Àü¿¡, SpringÀÇ ÀÌ»óÀº ¼³Á¤ÆÄÀÏÀÌ ¾î¶² Çüŵç(ÀÚ¹Ùµç XMLÀ̵ç) »ó°ü¾øÀÌ ¿ìÁö µÈ´Ù´Â °ÍÀ» À¯³äÇ϶ó.

JavaConfig¿¡¼­ »ç¿ëÇÒ ¼ö ÀÖ´Â °¡Àå Áß¿äÇÑ ¾Ö³ëÅ×À̼ǵéÀ» »ìÆ캸ÀÚ:

2.1. @Configuration

@Configuration ¾Ö³ëÅ×À̼ÇÀº ¼³Á¤ Ŭ·¡½º¸¦ ÁöĪÇÑ´Ù:

@Configuration
public class WebConfiguration {
   // bean definitions follow
}

@ConfigurationÀº Ŭ·¹½º¿¡ Àû¿ëÇÏ´Â ¾Ö³ëÅ×À̼ÇÀÌ°í ¼³Á¤ÇÒ beanµé¿¡ ±âº»À¸·Î Àû¿ëÇÒ °ÍµéÀ» ³ªÅ¸³½´Ù.

@Configuration(defaultAutowire = Autowire.BY_TYPE, defaultLazy = Lazy.FALSE)
public class DataSourceConfiguration 
     extends ConfigurationSupport {
}

<beans/> ÅÂ±×¿Í µ¿ÀÏÇÏ°Ô »ý°¢ÇÏ¸é µÈ´Ù. @Configuration ¾Ö³ëÅ×À̼ÇÀ» ´Þ°í Àִ Ŭ·¹½º´Â À¯Æ¿¸®Æ¼ ¸Þ¼Òµå¸¦ Á¦°øÇÏ´Â ConfigurationSupport ¸¦ È®ÀåÇÔÀ» ³ªÅ¸³½´Ù.

2.2. @Bean


À̸§ÀÌ ¾Ï½ÃÇϵíÀÌ, @Bean ¾Ö³ëÅ×À̼ÇÀº bean Á¤ÀÇ(<bean/> ű×)¸¦ ³ªÅ¸³½´Ù. °£´ÜÇÑ ¿¹Á¦·Î »ìÆ캸ÀÚ.

@Bean (scope = DefaultScopes.SESSION)
public ExampleBean exampleBean() {
  return new ExampleBean();
}

À§ÀÇ ÄÚµå´Â Spring ÄÁÅ×À̳ʿ¡°Ô methodÀÇ À̸§(bean name)À» »ç¿ëÇÏ¿© beanÀ» »ý¼ºÇϵµ·Ï ÇÏ°í, °ª(½ÇÁ¦ bean °´Ã¼)À» ¹ÝȯÇÑ´Ù. beanÀº session ½ºÄßÀÌ´Ù. Áï, exampleBean() ¸Þ¼Òµå´Â HTTP session ¸¶´Ù »õ·Î¿î °´Ã¼¸¦ »ý¼ºÇØ ÁÙ °ÍÀÌ´Ù.

¼ø¼ö ÀÚ¹Ù Äڵ带 »ç¿ëÇϱ⠶§¹®¿¡:

- static method¸¦ »ç¿ëÇϸé factory-mathod¸¦ »ç¿ëÇÒ ÇÊ¿ä°¡ ¾ø´Ù.

@Bean
public ExampleBean exampleBean() {
  return ExampleFactory.createBean();
}

¶ÇÇÑ

- º¹ÀâÇÑ °´Ã¼ »ý¼º½Ã ÇÊ¿äÇÑ FactoryBean/MethodInvokingFactoryBeanµµ ÇÊ¿ä¾ø´Ù.

@Bean(aliases = { "anniversaries" })
public List<Date> birthdays() {
  List<Date> dates = new ArrayList<Date>();
  Calendar calendar = Calendar.getInstance();

  calendar.set(1977, 05, 28);
  dates.add(calendar.getTime());
  dates.add(computeMotherInLawBirthday());

  return dates;
}

@BeanÀº ¸Þ¼Òµå¿¡ ºÙÀÏ ¼ö ÀÖ´Â ¾Ö³ëÅ×À̼ÇÀÌ°í bean °´Ã¼¸¦ »ý¼ºÇÏ°í ¼³Á¤ÇÒ ¶§ »ç¿ëÇÒ ÀÚ¹Ù Äڵ带 ³ªÅ¸³½´Ù. ÀÌ ¾Ö³ëÅ×À̼ÇÀº XML ºó ¼³Á¤¿¡¼­ »ç¿ëÇÒ ¼ö Àִ autowiringlazy-initdependecy-checkdepends-on°ú scope°ú °°Àº ´ëºÎºÐÀÇ ¼³Á¤À» Áö¿øÇÑ´Ù. ¶ÇÇÑ, ¶óÀÌÇÁ»çÀÌŬ ¸Þ¼Òµå¿Í XXXAware ÀÎÅÍÆäÀ̽ºµµ ÀüºÎ Áö¿øÇÑ´Ù.

public class AwareBean implements BeanFactoryAware {

  private BeanFactory factory;

  // BeanFactoryAware setter
  public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
   this.factory = beanFactory;
  }

  public void close(){
    // do clean-up
  }
}

@Bean(destroyMethodName = "close", lazy = Lazy.TRUE)
public AwareBean createBeanFactoryAwareBean() {
   return new AwareBean();
}

destroyMethodNanme ¿Ü¿¡µµ, @Bean ¾Ö³ëÅ×À̼ÇÀº initMethodNameÀ» Á¦°øÇÑ´Ù. though its usage is discourage as one already has control over the object creation and thus can call the initializing method if needed.

2.3. @ExternalBean


@ExternalBeanÀº ´Ü¼øÇÑ ¸¶Å©¾÷ ¾Ö³ëÅ×À̼ÇÀ¸·Î 'ºÎ¸ð ¾ÖÇø®ÄÉÀÌ¼Ç ÄÁÅؽºÆ®'¿¡ Á¤ÀÇ µÇ¾î ÀÖ´Â '¿ÜºÎÀÇ' ºóÀ» ÀÎÁ§¼Ç ÇÒ ¶§ »ç¿ëÇÑ´Ù. ¿¹Á¦¸¦ »ìÆ캸ÀÚ:

@Configuration
public abstract class ExternalBeanConfiguration {
  @Bean
  public TestBean james() {
    TestBean james = new TestBean();
    // inject dependency from ann()
    james.setSpouse(ann());
    return james;
  }

  // Will be taken from the parent context
  @ExternalBean
  public abstract TestBean ann();
}

JavaConfig°¡ @ExternalBeanÀ» ¸¸³ª°Ô µÇ¸é, ¼ÒÀ¯ÇÏ°í ÀÖ´Â ¸Þ¼Òµå¸¦ ¿À¹ö¶óÀ̵åÇÏ¿© ¾ðÁ¦µç ¸Þ¼Òµå¸¦ È£ÃâÇÒ ¼ö ÀÖµµ·Ï ÇÑ´Ù. 'ºÎ¸ð ¾ÖÇø®ÄÉÀÌ¼Ç ÄÁÅؽºÆ®'´Â ¸Þ¼Òµå À̸§¿¡ ÇØ´çÇÏ´Â beanÀ» ã¾Æ ÁØ´Ù(bean naming°ú °ü·ÃµÈ éÅÍ¿¡¼­ º¸´Ù ÀÚ¼¼ÇÑ ³»¿ëÀ» ÂüÁ¶). ÀÌ·± ¹æ¹ýÀ», ¼³Á¤ ÆÄÀÏÀº ¼ø¼ö ÀÚ¹Ù·Î ³²À» ¼ö ÀÖ°í Àͼ÷ÇÏ°Ô ¸®ÆÑÅ͸µ ÇÒ ¼ö ÀÖ´Ù.

@ExternalBeanÀÌ ÀÏ¹Ý ¸Þ¼Òµå¿¡µµ ¿ª½Ã ÀÛµ¿ÇÔÀ» ±â¾ïÇ϶ó. À§ÀÇ ¿¹Á¦´Â Ãß»ó ¸Þ¼Òµå¸¦ »ç¿ëÇÏ¿© ½ÇÇàµÇÁöµµ ¾ÊÀ» ¾²·¹±â Äڵ带 ÀÛ¼ºÇÏ´Â °ÍÀ» ¹æÁöÇß´Ù.

@Configuration
public class ExternalBeanOnNormalMethod {

  @ExternalBean
  public TestBean ann(){
      System.out.println("this code will not execute as the method " +
          "will be overriden with a bean look up at runtime");
  }
}

2.4. @ScopedProxy

SpringÀº scoped ÇÁ·Ï½Ã(ÀÚ¼¼ÇÑ ³»¿ëÀº ¸µÅ© ÂüÁ¶)¸¦ »ç¿ëÇÏ¿© scoped Á¾¼Ó¼ºÀÇ »ç¿ëÀ» Æí¸®ÇÏ°Ô Çß´Ù. ±×·± ÇÁ·Ï½Ã¸¦ »ý¼ºÇÏ´Â °¡Àå ½¬¿î ¹æ¹ýÀº, XML ¼³Á¤À» »ç¿ëÇÒ ¶§´Â <aop:scope-proxy/> ű׸¦ »ç¿ëÇÏ´Â °ÍÀÌ¿´´Ù. JavaConfig´Â ±× ´ë¾ÈÀ¸·Î @ScopedProxy ¾Ö³ëÅ×À̼ÇÀ» Á¦°øÇÏ¿© µ¿ÀÏÇÑ semantic°ú ¼³Á¤ ¿É¼ÇÀ» »ç¿ëÇÒ ¼ö ÀÖ´Ù.

XMLÀ» »ç¿ëÇÑ ·¹ÆÛ·±½ºÀÇ ¹®¼­¿¡ ÀÖ´Â ¿¹Á¦¸¦ JavaConfig¸¦ »ç¿ëÇÏ¿© ´ÙÀ½°ú °°ÀÌ ÀÛ¼ºÇÒ ¼ö ÀÖ´Ù.

// a HTTP Session-scoped bean exposed as a proxy
@Bean(scope = DefaultScopes.SESSION)
@ScopedProxy
public UserPreferences userPreferences() {
   return new UserPreferences();
}

@Bean
public Service userService() {
   UserService service = new SimpleUserService();
   // a reference to the proxied 'userPreferences' bean
   service.seUserPreferences(userPreferences());
   return service;
}
½Å°í

'Spring JavaConfig > Chapter 2' Ä«Å×°í¸®ÀÇ ´Ù¸¥ ±Û

Components  (0)2007.07.20
top

Introduction



¿ø¹® : Chapter 1. Introduction

¹ø¿ª

IoC éÅÍ¿¡¼­ ¾ð±Þ ÇßµíÀÌ, Spring IoCÀÇ ÇÙ½ÉÀº °´Ã¼¸¦ Spring ÄÁÅ×À̳ʿ¡¼­ ¾î¶»°Ô »ý¼º(instantiate)ÇÒÁö, °áÇÕÇÒÁö(assemble), °ü¸®ÇÒÁö(manage)  Á¤ÀÇÇÏ´Â bean °³³äÀÌ´Ù. SpringÀº ÀÚ¹Ù ÄÚµå·Î º¯È¯ÇÒ ¼ö ÀÖ´Â ¾î¶°ÇÑ ¸ÞŸ µ¥ÀÌÅÍ¶óµµ »ç¿ëÇÒ ¼ö ÀÖ°í, XMLÀº ±×·¯ÇÑ bean ¼³Á¤À» ±â¼úÇϴµ¥ °¡Àå ³Î¸® ¾²ÀÌ´Â ¹æ¹ýÀÌ´Ù. JDK 5 ÀÌ»óÀÇ ¹öÀü¿¡¼­ »ç¿ëÇÒ ¼ö ÀÖ´Â ¾Ö³ëÅ×À̼ÇÀº ½ÇÁ¦ ¼Ò½º Äڵ忡 Ãß°¡ Á¤º¸¸¦ ÀÔ·ÂÇÏ¿© ·±Å¸ÀÓ ¶§ Àû¿ëÇÒ ¼ö ÀÖ´Â Äڵ带 ¸¸µé¾î ³¾ ¼ö Àֱ⠶§¹®¿¡, ¼³Á¤ ÆÄÀÏÀÇ À¯·ÂÇÑ È帰¡ µÈ´Ù.

½Å°í

'Spring JavaConfig > Chapter 1' Ä«Å×°í¸®ÀÇ ´Ù¸¥ ±Û

Introduction  (0)