ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA/SPRING] 어노테이션을 이용한 스프링 설정 – 1
    개발/Java 2019. 2. 28. 13:52

    어노테이션을 이용한 스프링 설정 – 1

    XML을 이용한 스프링 설정파일 제작을 Java파일로 제작할 수 잇는 방법

    새로운 Java 파일을 만든다

    • @Configuration 어노테이션을 통해, 설정 파일임을 명시한다.

    package ems.member.configration;

    import org.springframework.context.annotation.Configuration;

    @Configuration
    public class MemberConfig {

    }

    메소드를 통해 빈(Bean)을 만든다.

    • @Bean 어노테이션을 통해, 빈(Bean) 객체임을 명시한다.

    • 메소드의 이름은 빈(Bean)의 id와 동일하고, 반환형은 class의 이름과 동일하다.

    <bean id="studentDao" class="ems.member.dao.StudentDao" ></bean>

    ->

    @Bean
    public StudentDao studentDao() {
    return new StudentDao();
    }

    constructor-arg는 생성자 파라미터에 넣어준다.

    <bean id="registerService" class="ems.member.service.StudentRegisterService">
    <constructor-arg ref="studentDao" ></constructor-arg>
    </bean>

    ->

    @Bean
    public StudentRegisterService registerService() {
    return new StudentRegisterService(studentDao());
    }

    property는 해당 객체를 하나 생성한 후, setter을 통해 value를 입력해준다.

    <bean id="dataBaseConnectionInfoDev" class="ems.member.DataBaseConnectionInfo">
    <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />
    <property name="userId" value="scott" />
    <property name="userPw" value="tiger" />
    </bean>

    ->

    @Bean
    public DataBaseConnectionInfo dataBaseConnectionInfoDev() {
    DataBaseConnectionInfo infoDev = new DataBaseConnectionInfo();
    infoDev.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:xe"); infoDev.setUserId("scott");
    infoDev.setUserPw("tiger");

    return infoDev;
    }

    property의 값이 여러개인 경우(List,Map), ArrayList, HashMap 등 자료구조를 이용한다.

    <bean id="informationService" class="ems.member.service.EMSInformationService">
    <property name="info">
    <value>Education Management System program was developed in 2015.</value>
    </property>
    <property name="copyRight">
    <value>COPYRIGHT(C) 2015 EMS CO., LTD. ALL RIGHT RESERVED. CONTACT MASTER FOR MORE INFORMATION.</value>
    </property>
    <property name="ver">
    <value>The version is 1.0</value>
    </property>
    <property name="sYear">
    <value>2015</value>
    </property>
    <property name="sMonth">
    <value>1</value>
    </property>
    <property name="sDay">
    <value>1</value>
    </property>
    <property name="eYear" value="2015" />
    <property name="eMonth" value="2" />
    <property name="eDay" value="28" />
    <property name="developers">
    <list>
    <value>Cheney.</value>
    <value>Eloy.</value>
    <value>Jasper.</value>
    <value>Dillon.</value>
    <value>Kian.</value>
    </list>
    </property>
    <property name="administrators">
    <map>
    <entry>
    <key>
    <value>Cheney</value>
    </key>
    <value>cheney@springPjt.org</value>
    </entry>
    <entry>
    <key>
    <value>Jasper</value>
    </key>
    <value>jasper@springPjt.org</value>
    </entry>
    </map>
    </property>
    <property name="dbInfos">
    <map>
    <entry>
    <key>
    <value>dev</value>
    </key>
    <ref bean="dataBaseConnectionInfoDev"/>
    </entry>
    <entry>
    <key>
    <value>real</value>
    </key>
    <ref bean="dataBaseConnectionInfoReal"/>
    </entry>
    </map>
    </property>
    </bean>

    ->

    @Bean
    public EMSInformationService informationService() {
       EMSInformationService info = new EMSInformationService();
       info.setInfo("Education Management System program was developed in 2015.");
       info.setCopyRight("COPYRIGHT(C) 2015 EMS CO., LTD. ALL RIGHT RESERVED. CONTACT MASTER FOR MORE INFORMATION.");
       info.setVer("The version is 1.0");
       info.setsYear(2015);
       info.setsMonth(1);
       info.setsDay(1);
       info.seteYear(2015);
       info.seteMonth(2);
       info.seteDay(28);

       ArrayList<String> developers = new ArrayList<String>();
       developers.add("Cheney.");
       developers.add("Eloy.");
       developers.add("Jasper.");
       developers.add("Dillon.");
       developers.add("Kian.");
       info.setDevelopers(developers);

       Map<String, String> administrators = new HashMap<String, String>();
       administrators.put("Cheney", "cheney@springPjt.org");
       administrators.put("Jasper", "jasper@springPjt.org");
       info.setAdministrators(administrators);

       Map<String, DataBaseConnectionInfo> dbInfos = new HashMap<String, DataBaseConnectionInfo>();
       dbInfos.put("dev", dataBaseConnectionInfoDev());
       dbInfos.put("real", dataBaseConnectionInfoReal());
       info.setDbInfos(dbInfos);

       return info;
    }

    메인클래스에서, GenericXmlApplicationContext대신 AnnotationConfigApplicationContext를 이용한다.

    AnnotationConfigApplicationContext ctx = 
    new AnnotationConfigApplicationContext(MemberConfig.class);


    댓글

Designed by Tistory.