바쁘신 몸 Rod Johnson이 오랜만에 포스팅을 했다.
public class MyConfig {
@Bean
public Person rod() {
return new Person("Rod Johnson");
}
@Bean(scope = Scope.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;
}
}
뒤따르는 설명이 잘 흡수가 안되어서 그려본 그림
이보다 더 직관적일 수 있는 설명은 아래의 XML 설정과 같은 효과를 낸다는 점이다.
<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>
자바를 설정을 위한 internal DSL로 쓴 것이다. EJB 3.0 스타일의 애노테이션과 구분되어 보이는 것은 Spring의 the non-invasive promise를 지키려 했다는 점이다. 위의 자바 코드는 XML 설정과 마찬가지로 오직 설정을 위한 코드일 뿐이며, 도메인의 내용과는 물리적으로 분리되어 있다.
XML로 설정하는 것에 비해 얻을 수 있는 장점은 IDE의 리팩토링 지원, 자바의 상속과 가시성을 활용할 수 있다는 점이다. 특히. 리팩토링 활용은 확실히 끌리는 메리트다.
원문의 장점 소개
- References (such as the reference to the "rod" bean in the example) survive refactoring; any good IDE provides great tool support.
- Because configurations are Java classes, they can participate in inheritance relationships. For example, you could define a superclass that demands some abstract @Beans to be implemented in subclasses.
- It creates a new visibility option. An @Bean method can be protected, it which case it benefits from the usual characteristics of the Spring component, but is not visible externally–that is it not injectable and cannot be obtained by calling getBean() on the IoC context.
더 흥미로운 점은 XML과 자바로 작성한 설정을 마음대로 섞어서 사용할 수 있다는 점이다. 만일, 선호가 다른 둘 이상의 개발자가 설정을 관리한다면.. ^^
공존을 가정하면, XML에서 자바 DSL로 만든 설정을 부르기 위해서는 아래와 같은 코드가 필요하다.
<bean class="..MyConfig"/>
<bean class="org.springframework.beans.factory.java.ConfigurationPostProcessor"/>
<bean class="SomeRandomBean">
<property…
</bean>
</beans>
자바 DSL을 직접 부르는 방안도 제공한다.
ApplicationContext aBunchOfConfigs = new AnnotationApplicationContext("**/configuration/*Configuration.class");
댓글 보기 Hi,
지훈 2006/11/29 15:35
그야말로 컨휘규레이션의 황제 로드 아저씨 답네요.
그 '드드드득' TV를 로타리라고 했나 보네요.
비유가 참 기발하십니다 ^^
Urubatan 2006/12/04 22:57
It is almost the reason I started Spring-Annotation project (https://spring-annotation.dev.java.net/nonav/);
Today night I`ll release the version 1.0.2 that will use the XML Schema option of spring 2.0 to enable the annotations reading (instead of another ApplicationContext implementation as the previous version).
if you do not like the Spring-Annotation approach, you can at lease use the same same kind of XML Schema configuration to enable your annotation approach.
look at one of the test cases:
package net.java.dev.springannotation.annotation.beans;
import net.java.dev.springannotation.annotation.Property;
public class ParentBean {
@Property(bean="scopeTest")
private ScopeTestBean testProp;
public ScopeTestBean getTestProp() {
return testProp;
}
public void setTestProp(ScopeTestBean testProp) {
this.testProp = testProp;
}
}
import net.java.dev.springannotation.annotation.Bean;
@Bean(name="parentTest")
public class ParentPropertyBean extends ParentBean{
}
package net.java.dev.springannotation.annotation;
import net.java.dev.springannotation.annotation.beans.ParentPropertyBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
public class ParentPropertyTest extends TestCase {
private ApplicationContext factory;
protected void setUp() throws Exception {
factory = new ClassPathXmlApplicationContext("classpath*:testContext.xml");
}
public void testParentProperty() {
ParentPropertyBean bean = (ParentPropertyBean) factory.getBean("parentTest");
assertNotNull(bean.getTestProp());
}
}
– the XML configuration –
<?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:sa="https://spring-annotation.dev.java.net/context"
xsi:schemaLocation="https://spring-annotation.dev.java.net/context https://spring-annotation.dev.java.net/nonav/context.xsd"
default-autowire="byName">
<sa:annotation-autoload/>
</beans>
I almost never use the @Property annotation, I prefer to just use the auto-wire approach …
We have a lot of good code already written, the support for JSF is great and you have two more scopes already implemented for JSF applications (working in port it to Spring MVC).
flash (from the end of one request to the start of the next one)
and conversation (until a method with @ConvEnd is executed)
please take a look and comment.
- 80년대 '드드드득' 소리를 내며 회전시켜서 채널을 선택하던 방식 [본문으로]
