Any Frame 3.2.1 로 따라하기 시작 함.. (샘플은 Lab-Anyframe-Student-3.2.0 을 사용)

> Lab 1

-. Bean look up 방식

1. ClassPathXmlApplicationContext 생성

  ClassPathXmlApplicationContext context;

  String[] locations = getConfigLocations();
  context = new ClassPathXmlApplicationContext(locations, false);
  context = new ClassPathXmlApplicationContext("context-helloworld.xml");
  context = new ClassPathXmlApplicationContext("context-*.xml");
  context.refresh();

    IHelloWorldService iHelloWorldService = (IHelloWorldService) context.getBean(IHelloWorldService.ROLE);
    System.out.println("HelloWorldService : " + iHelloWorldService.greet());

2. ClassPathResource 생성

  ClassPathResource resource = new ClassPathResource("context-helloworld.xml");
  BeanFactory factory = new XmlBeanFactory(resource);
    IHelloWorldService iHelloWorldService = (IHelloWorldService) factory.getBean(IHelloWorldService.ROLE);
    System.out.println("HelloWorldService : " + iHelloWorldService.greet());

3. Annotation 으로

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/spring/context-user-setter.xml" })
public class SetterInjectionTestCase extends AbstractDependencyInjectionSpringContextTests
{
}

> Lab 2

http://dev.anyframejava.org/anyframe/doc/core/3.2.1/corefw/guide/ioc-dependencies.html

-. Dependency Injection(DI)
1. Setter Injection

 * XML
 <bean id="com.sds.emp.services.user.UserService"
  class="com.sds.emp.services.user.impl.UserServiceImpl">
  <!-- TODO : setter injection -->
        <!-- <property name="userDAO" ref="userDAO" /> -->
        <property name="userDAO"><ref local="userDAO"/></property>
 </bean>
 
 <bean id="userDAO" class="com.sds.emp.services.user.impl.UserDAO"/>

 * JAVA
    // TODO - for setter injection
    public void setUserDAO(UserDAO userDAO){ this.userDAO = userDAO; }

2. Constructor Injection

 * XML
 <bean id="com.sds.emp.services.user.UserService"
  class="com.sds.emp.services.user.impl.UserServiceImpl">
  <!-- TODO : constructor injection -->
     <constructor-arg ref="userDAO"/>  
 </bean>
 
 <bean id="userDAO" class="com.sds.emp.services.user.impl.UserDAO"/>

 * JAVA
    // TODO - for constructor injection
    public UserServiceImpl(UserDAO userDAO){ this.userDAO = userDAO; }

3. Method Injection
3.1 Lookup Method Injection :: Singleton Bean이 Prototype Bean을 참조해야 할 경우 <lookup-method>를 설정한다.

 * XML
 <bean id="com.sds.emp.services.user.UserService"
  class="com.sds.emp.services.user.impl.UserServiceImpl">
  <!-- TODO : method injection -->
  <lookup-method name="getUserDAO" bean="userDAO"/>
 </bean>
 
 <!-- TODO : change scope from singleton to prototype (non singleton) -->
 <bean id="userDAO" class="com.sds.emp.services.user.impl.UserDAO" scope="prototype" />

 * JAVA
    // TODO - for method injection
    public UserDAO getUserDAO(){  return null;  }

3.2 Method Replacement :: 이미 존재하는 기존의 메소드를 수정하지 않은 상태에서 메소드의 기능을 변경하고자 할 때 <replaced-method>를 이용한다. 사용 예제는 다음과 같다.

 * XML
<bean id="beanFirst" class="test.BeanFirst"/>
<bean id="beanSecond" class=" test.BeanSecond">
        <replaced-method name="sayHello" replacer="methodReplacer">
            <arg-type>String</arg-type>
        </replaced-method>

</bean>
<bean id="methodReplacer" class="test.SayHelloMethodReplacer"/>

4. auto wiring, depends on bean

 * XML
 <!-- TODO : set autowiring, depends on bean -->
 <bean id="com.sds.emp.services.user.UserService"
  class="com.sds.emp.services.user.impl.UserServiceImpl" autowire="byType" depends-on="userSample">
 </bean>
 
 <!-- TODO : set lazy initialization -->
 <bean id="userDAO" class="com.sds.emp.services.user.impl.UserDAO" lazy-init="true"/>
 
 <bean id="userSample" class="com.sds.emp.services.user.impl.UserSample"/>

+ Recent posts