물론 제가 아직 Spring에 대한 내공이 부족하기도 하고, .NET의 Extension Methods 를 자주 사용하다보니 static class 를 습관적으로 많이 써서 생기는 문제일 수 있습니다. (이거 좋은 습관은 아닌데...)
어쨌든 기존 소스 중에 Singleton으로 써야 하고, static field 를 가진 클래스에 대해 injection을 수행해야 하는 것이 있었습니다. 다음과 같은 Class 가 있었습니다.
UnitOfWorks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example.spring.staticinjection; | |
public class UnitOfWorks { | |
private static UnitOfWorkFactory unitOfWorkFactory; | |
public static void setUnitOfWorkFactory(UnitOfWorkFactory factory) { | |
unitOfWorkFactory = factory; | |
} | |
public static UnitOfWorkFactory getUnitOfWorkFactory() { | |
return unitOfWorkFactory; | |
} | |
} |
UnitOfWorks 는 static field로 UnitOfWorkFactory를 가지고 있습니다. 이 필드를 Spring 이 injection을 할 수 있도록 해주고 싶었습니다.
xml 에서 작업하려면 MethodInvokingFactoryBean 을 이용하여 static method 를 호출하면 됩니다.
application-context.xml
xml 에서도 되는데 java configuration에서도 되겠지요?
그래서 구글링을 했더니 다행히, 몇번만에 해답을 찾았습니다.
원문: Spring annotations static injection tutorial
이 문서를 보고, 핵심은 원하는 클래스를 @Component로 지정하여 Spring Container에서 관리하도록하고, injection을 수행하도록 @Autowired를 지정하면 됩니다.
새로운 UnitOfWorks.java
UnitOfWorks 클래스는 @Component로 지정하고, @Autowired 해야 할 메소드는 static 이 아닌 instance method로 변경한 것으로 끝났다^^
마지막으로 Spring java configuration 에서 위의 UnitOfWorks를 ComponentScan 으로 등록해주면 됩니다.
StaticInjectionConfig.java
ComponentScan에서 UnitOfWorks class 를 지정해서 Component로 등록하게 하면, Bean으로 등록된 UnitOfWorkFactory가 injection이 됩니다^^
이로서 static field에 대해서도 java configuration에서 설정할 수 있게 되었습니다.
흠... Spring 에 대해 좀 더 공부해야겠네요...
xml 에서 작업하려면 MethodInvokingFactoryBean 을 이용하여 static method 를 호출하면 됩니다.
application-context.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- UnitOfWorkFactory 인스턴스를 UnitOfWorks Singleton의 static field에 설정합니다. --> | |
<bean name="unitOfWorkInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> | |
<property name="staticMethod" value="kr.nsoft.data.hibernate.unitofwork.UnitOfWorks.setUnitOfWorkFactory"/> | |
<property name="arguments"> | |
<list> | |
<ref bean="unitOfWorkFactory"/> | |
</list> | |
</property> | |
</bean> |
xml 에서도 되는데 java configuration에서도 되겠지요?
그래서 구글링을 했더니 다행히, 몇번만에 해답을 찾았습니다.
원문: Spring annotations static injection tutorial
이 문서를 보고, 핵심은 원하는 클래스를 @Component로 지정하여 Spring Container에서 관리하도록하고, injection을 수행하도록 @Autowired를 지정하면 됩니다.
새로운 UnitOfWorks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example.spring.staticinjection; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Component; | |
@Component | |
public class UnitOfWorks { | |
private static UnitOfWorkFactory unitOfWorkFactory; | |
/** | |
* Spring가 UnitOfFactory 를 인젝션을 수행합니다. | |
* | |
* @param factory | |
*/ | |
@Autowired | |
public void setUnitOfWorkFactory(UnitOfWorkFactory factory) { | |
unitOfWorkFactory = factory; | |
} | |
public static UnitOfWorkFactory getUnitOfWorkFactory() { | |
return unitOfWorkFactory; | |
} | |
} |
UnitOfWorks 클래스는 @Component로 지정하고, @Autowired 해야 할 메소드는 static 이 아닌 instance method로 변경한 것으로 끝났다^^
마지막으로 Spring java configuration 에서 위의 UnitOfWorks를 ComponentScan 으로 등록해주면 됩니다.
StaticInjectionConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example.spring.staticinjection; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
@ComponentScan( basePackageClasses = { UnitOfWorks.class } ) | |
public class StaticInjectionConfig { | |
@Bean | |
public UnitOfWorkFactory unitOfWorkFactory() { | |
return new UnitOfWorkFactory(); | |
} | |
} |
ComponentScan에서 UnitOfWorks class 를 지정해서 Component로 등록하게 하면, Bean으로 등록된 UnitOfWorkFactory가 injection이 됩니다^^
이로서 static field에 대해서도 java configuration에서 설정할 수 있게 되었습니다.
흠... Spring 에 대해 좀 더 공부해야겠네요...
댓글 없음:
댓글 쓰기