본문 바로가기
Back-end/Spring Framework

java spring 어노테이션을 이용한 스프링 설정, 파일 분리

by javapp 자바앱 2020. 8. 3.
728x90

@Configuration

public class MemberConfig {

      

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

       @Bean

       public StudentDao studentDao() {

             return new StudentDao();

       }

      

       /*

       <bean id="registerService" class="ems.member.service.StudentRegisterService">

             <constructor-arg ref="studentDao" ></constructor-arg>

       </bean>

        */

       @Bean

       public StudentModifyService registerService() {

             return new StudentModifyService(studentDao());

       }

       @Bean

       public StudentRegisterService modifyService() {

             return new StudentRegisterService(studentDao());

       }

       @Bean

       public StudentDeleteService deleteService() {

             return new StudentDeleteService(studentDao());

       }

       @Bean

       public StudentSelectService selectService() {

             return new StudentSelectService(studentDao());

       }

       @Bean

       public StudentAllSelectService allSelectService() {

             return new StudentAllSelectService(studentDao());

       }

      

       /*

       <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 dbInfoDev = new DataBaseConnectionInfo();

             dbInfoDev.setJdbcUrl("jdbc:mysql://localhost/sogongdo?characterEncoding=utf8&amp;serverTimezone=UTC&amp;useSSL=false");

              dbInfoDev.setUserId("root");

             dbInfoDev.setUserPw("7452");

             return dbInfoDev;

       }

       @Bean

       public DataBaseConnectionInfo dataBaseConnectionInfoReal() {

             DataBaseConnectionInfo dbInfoReal = new DataBaseConnectionInfo();

             dbInfoReal.setJdbcUrl("jdbc:mysql://localhost/sogongdo?characterEncoding=utf8&amp;serverTimezone=UTC&amp;useSSL=false");

             dbInfoReal.setUserId("masterid");

             dbInfoReal.setUserPw("masterpw");

             return dbInfoReal;

       }

      

       /*

       <bean id="informationService" class="ems.member.service.EMSInformationService">

             <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 emsInfo = new EMSInformationService();

            

             ArrayList<String> developers = new ArrayList<String>();

             developers.add("Cheney");

             developers.add("Eloy");

             developers.add("Jasper");

             developers.add("Dillon");

             developers.add("Kian");

             emsInfo.setDevelopers(developers);

            

             Map<String,String> administrators = new HashMap<String,String>();

             administrators.put("Cheney", "cheney@springPjt.org");

             administrators.put("Jasper", "jasper@springPjt.org");

             emsInfo.setAdministrators(administrators);

            

             Map<String, DataBaseConnectionInfo> dbInfos = new HashMap<String, DataBaseConnectionInfo>();

             dbInfos.put("dev", dataBaseConnectionInfoDev());

             dbInfos.put("real", dataBaseConnectionInfoReal());

             emsInfo.setDbInfos(dbInfos);

            

             return emsInfo;

       }

}

 


파일 분리시

 

@Configuration
public class MemberConfig3 { 

      @Autowired  //자동주입

       DataBaseConnectionInfo dataBaseConnectionInfoDev;

      

       @Autowired

       DataBaseConnectionInfo dataBaseConnectionInfoReal;

 

@Bean
public EMSInformationService informationService() {

       Map<String, DataBaseConnectionInfo> dbInfos = new HashMap<String, DataBaseConnectionInfo>();

       dbInfos.put("dev", dataBaseConnectionInfoDev);

       dbInfos.put("real", dataBaseConnectionInfoReal);

       info.setDbInfos(dbInfos);

       return info;
}

}

 

       <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>


 

파일분리

 

@Configuration

@Import({MemberConfig2.class, MemberConfig3.class})

public class MemberConfigImport {..}

 

 


메인에서 적용

             AnnotationConfigApplicationContext ctx =

                           new AnnotationConfigApplicationContext(MemberConfig1.class, MemberConfig2.class, MemberConfig3.class);

      

             EMSInformationService informationService = ctx.getBean("informationService", EMSInformationService.class);

             informationService.outputEMSInformation();

댓글