본문 바로가기

프로그래머/JAVA

Spring DI(예제)

package sample1;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

 

public class HelloApp {

 

        public static void main(String[] args) {

               //Resource resource = new ClassPathResource("applicationContext.xml");

               //BeanFactory factory = new XmlBeanFactory(resource);

                       ApplicationContext factory = new FileSystemXmlApplicationContext("applicationContext.xml");

                       MessageBean bean = (MessageBean) factory.getBean("messageBean");

                       bean.sayHello();      

        }

}

 

package sample1;

 

public interface MessageBean {

        void sayHello();

}

 

package sample1;

 

public class MessageBeanImpl implements MessageBean {

 

        private String name;

        private String greeting;

       

        @Override

        public void sayHello() {

               System.out.println(String.format("{0}{1}!",greeting, name));

        }

 

        public MessageBeanImpl(String name) {

               super();

               this.name = name;

        }

 

        public void setGreeting(String greeting) {

               this.greeting = greeting;

        }

       

}

 

<?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 = "messageBean" class = "sample1.MessageBeanImpl">

               <constructor-arg>

                       <value>Spring</value>

               </constructor-arg>

               <property name="greeting">

                       <value>Hello, </value>

               </property>

        </bean>

 

</beans>

'프로그래머 > JAVA' 카테고리의 다른 글

Spring AOP(AspectJ)  (0) 2015.03.03
Spring AOP  (0) 2015.03.03
JDBC(Java DataBase Connectivity)  (0) 2013.03.29
[JSP] JavaBean  (0) 2013.03.29
[JSP] 세션 리스너  (0) 2013.03.28