Spring/Study 2020. 4. 13. 16:05

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

@Configuration 어노테이션은 JAVA 파일을 스프링 컨테이너로 사용하도록 명시해주는 어노테이션이다. 따라서 @Configuration 어노테이션을 통해 JAVA 파일이 스프링 컨테이너(xml) 역할을 하도록 할 수 있다. JAVA 파일에서는 @Bean 어노테이션을 사용하여 메소드로  빈(Bean) 객체를 생성한다.

 

bean

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <bean id="studentDao" class="ems.member.dao.StudentDao" ></bean>
 
</beans>
cs

 

@Configuration
public class MemberConfig {
    @Bean
    public StudentDao studentDao() {
        return new StudentDao();
    }
}
cs

 

constructor

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <bean id="registerService" class="ems.member.service.StudentRegisterService">
        <constructor-arg ref="studentDao" ></constructor-arg>
    </bean>
 
</beans>
cs

 

@Configuration
public class MemberConfig {
    @Bean
    public StudentRegisterService registerService() {
        return new StudentRegisterService(studentDao());
    }
}
cs

 

property

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <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>
 
</beans>
cs

 

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

 

list

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <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>
    </bean>
 
</beans>
cs

 

@Configuration
public class MemberConfig {
    @Bean
    public EMSInformationService informationService() {
        EMSInformationService info = new EMSInformationService();
        
        ArrayList<String> developers = new ArrayList<String>();
        developers.add("Cheney.");
        developers.add("Eloy.");
        developers.add("Jasper.");
        developers.add("Dillon.");
        developers.add("Kian.");
        info.setDevelopers(developers);
        
        return info;
    }
}
cs

 

map

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="informationService" class="ems.member.service.EMSInformationService">
        <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>
    </bean>
 
</beans>
cs

 

@Configuration
public class MemberConfig {
    @Bean    
    public EMSInformationService informationService() {
        EMSInformationService info = new EMSInformationService();
        
        Map<StringString> administrators = new HashMap<StringString>();
        administrators.put("Cheney""cheney@springPjt.org");
        administrators.put("Jasper""jasper@springPjt.org");
        info.setAdministrators(administrators);
    
        return info;
    }
}

Colored by Color Scripter

cs

posted by DevMoomin
: