본문 바로가기

SpringFramework

Spring01정리

<<<<<<xml로 객체생성하기>>>- <bean>태그사용

ArticleDAO.java----------------------------------------------//인터페이스

package Chapter1;

public interface ArticleDAO {

       String sayhello(String name);

}

WriteArticleDAO.java-----------------------------------------------------------------//상속받음

package Chapter1;

public class WriteArticleDAO implements ArticleDAO{

       @Override

       public String sayhello(String name) {

             return "Hello"+name;

       }

}

App.xml----------------------------------------------------------------------------//객체생성

<bean id="hello" class="Chapter1.WriteArticleDAO"/>

<bean id="dao" class="Chapter2.WriteArticleDAO "/>

<!-- Article dao=new WriteArticleDAO(); 동일 -->

<<<<<<<DI>>>>>>>>>>>>>>

ArticleDAO.java---------------------------------------------------------------------//인터페이스

package Chapter2;

public interface ArticleDAO {

       public void hello();

}

ArticleSupport.java-----------------------------------//인터페이스 타입으로 매개변수를 주입한 DI구현

package Chapter2;

public class ArticleSupport {

       private ArticleDAO dao;

       private String name;

      

       public ArticleSupport(ArticleDAO dao){             //생성자로 DI구현

             this.dao=dao;

       }

       public ArticleSupport(String name){

             this.name=name;

       }

       public void show(){

             dao.hello();

             System.out.println(name);

       }

}

WriteArticleDAO.java--------------------------------------------------------------//ArticleDAO 상속받음

package Chapter2;

public class WriteArticleDAO implements ArticleDAO{

            public void hello(){

                System.out.println("hello");

            }

}

App.xml-------------------------------------------------//객체생성

<bean id="dao" class="Chapter2.WriteArticleDAO "/>

<bean id="asupport1" class="Chapter2.ArticleSupport">

<constructor-arg ref="dao"/>

</bean>

<!—생성자 DI 매개변수로 외부객체를 만들어서 참조하는 경우 -->

<bean id="asupport2" class="Chapter2.ArticleSupport">

<constructor-arg><bean class="Chapter2.WriteArticleDAO"/>

</constructor-arg>

</bean>             <!-- 생성자 DI 매개변수로 바로 객체생성해서 주입하는 경우 -->

 

<bean id="asupport3" class="Chapter2.ArticleSupport">

<constructor-arg value="hong"></constructor-arg>

</bean>              <!-- 생성자 매개타입이 String으로 value -->

 

<bean id="asupport4" class="Chapter2.ArticleSupport">

<constructor-arg> <value>hong</value></constructor-arg>

</bean>                         <!-- value엘리먼트로 value 설정 -->

 

<<<<<생성자에 타입설정해주기>>>>> -Setter메서드의 매개변수값 전달방법, Property설정

<bean id="na_add_age" class="Sample.T">

<constructor-arg><value>hong</value></constructor-arg>

<constructor-arg><value type="int">12</value></constructor-arg>

</bean>             <!-- 만일 타입을 지정해주지 않으면 String으로 인식 -->

T1.java----------------------------------------------------------------------------------//인터페이스

package Sample2;

public interface T1 {

}

Info.java---------------------------------------------------//인터페이스

package Sample2;

public interface Info {

       void prt();

       void show();

}

Infoimple.java---------------------------------------------//상속받음

package Sample2;

public class InfoImple implements Info {

       @Override

       public void prt() {

             System.out.println("infoImple prt");

       }

       @Override

       public void show() {

             System.out.println("infoInple show");

       }

}

 

T1imple.java----------------------------------------------------------------------------//상속받음

package Sample2;

public class T1Imple implements T1 {

       private String name;

       private int age;

       private Info ins;

       public void setName(String name) {

             this.name = name;

       }

       public void setAge(int age) {

             this.age = age;

       }

       public void setIns(Info ins) {

             this.ins = ins;

       }

       public void prt(){

             if(ins!=null){

 

                    System.out.println(name+"  , "+age);

                    System.out.println("========about ins==============");

                    ins.prt();

                    ins.show();

             }else{

                    System.out.println("ins:null");

             }

       }

}

ApplicationContext.xml------------------------------------------------------------//DI

 

<!-- 방법1:setter메서드의 매개변수를 넣어줄 객체를 생성한다. -->

<bean id="t1" class="Sample2.T1Imple">

       <property name="name" value=""/>

       <property name="age" value="10"/>

       <property name="ins"><bean class="Sample2.InfoImple"/></property>

</bean>

 

<!-- 방법2:setter메서드의 매새변수를 넣어줄 미리 만들어놓은 bean객체를 ref 불러온다. -->

<bean id="tinfo" class="Sample2.InfoImple"/>

<bean id="t2" class="Sample2.T1Imple">

<property name="name" value="kim"/>

<property name="age" value="10"/>

<property name="ins"><ref bean="tinfo"/> </property>

</bean>

 

<!-- 방법3:프로퍼티 엘리먼트를 쓰지않고 p:필드명=”으로 지정함 -->

<bean id="t3" class="Sample2.T1Imple" p:name="park" p:age="5" p:ins-ref="tinfo"/>

 


 

<<<<List 객체 저장하기>>>>-Property 이용한 객체 저장

-------------------------------------------------------------- ListData.java

package Sample3;

import java.util.List;

public class ListData {

       List<String> list;

       List<Integer> list2;

       public List<String> getList() {

             return list;

       }

       public void setList(List<String> list) {

             this.list = list;

       }

       public List<Integer> getList2() {

             return list2;

       }

       public void setList2(List<Integer> list2) {

             this.list2 = list2;

       }

       //List<String> aaa=new ArrayList<String>();

       //aaa.add("ddd");

       //aaa.add("ccc");

      

       //ListData d=new ListData();

       //d.setList(aaa);

}

----------------------------------------------------------------------------------------ApplicationContext.xml

<bean id="lst1" class="Sample3.ListData">

<!-- list.setList(new ArrayList()); 동일 -->

<property name="list">                          <!-- List<String> list; -->

<list>

<value>홍길동</value>

<value>김하나</value>

<value>김지은</value>

</list>

</property>

</bean>

 

<bean id="lst2" class="Sample3.ListData">

<property name="list2">

<list value-type="java.lang.Integer">

<value>100</value>                 <!-- 아니면 value type 지정해주어도됨 -->

<value>200</value>

<value>300</value>

</list>

</property>

</bean>

<<<<<Map>>>>>객체생성하기

----------------------------------------------------------MapData.java

package Sample3;

import java.util.Map;

public class MapData {

       private Map<String,MemberDTO> dto;

       public Map<String, MemberDTO> getDto() {

             return dto;

       }

       public void setDto(Map<String, MemberDTO> dto) {

             this.dto = dto;

       }

}

-----------------------------------------------------------------------------------MapDataTest.java

package Sample3;

public class MapDataTest {

       MapData data;

       @Before

       public void bbb(){

             ApplicationContext ctx

=new ClassPathXmlApplicationContext("ApplicationContext3.xml");

             data=ctx.getBean("data",MapData.class);

 //MapData.class타입의 객체를 읽어옴

       }

       @Test                                      //단위테스트

       public void aaa(){

             Assert.assertNotNull(data);

             Assert.assertEquals("kim",data.getDto().get("a1").getName());

       }

}

---------------------------------------------------------MemberDTO.java

package Sample3;

public class MemberDTO {

       private String name;

       private int age;

       public String getName() {

             return name;

       }

       public void setName(String name) {

             this.name = name;

       }

       public int getAge() {

             return age;

       }

       public void setAge(int age) {

             this.age = age;

       }

}

------------------------------------------------------------------------------------ApplicationContext.xml

<bean id="data" class="Sample3.MapData">  

       <property name="dto">                 

<!-- dto이름을 가진 MapData객체 생성-->

       <map>                  

             <entry>                     <!-- Map타입의 Key value설정 -->

             <key><value>a1</value></key>       

             <bean class="Sample3.MemberDTO">

             <property name="name">   <!-- MemberDTO 필드명name값에 

kimset해줌 --> 

             <value>kim</value> <!-- 중요! 반드시 MemberDTO객체를 생성해야함! -->

             </property>

             </bean>

             </entry>

            

            

             <entry>

             <key><value>a2</value></key>    <!-- 외부객체 d 참조해서 매개변수를 주입 -->

             <ref bean="d"/>

             </entry>

       </map>

       </property>

       </bean>

      

       <bean id="d" class="Sample3.MemberDTO">

       <property name="name" value="hong"/>

       </bean>

 

<<<<Properties받아오기>>>>

-------------------------------------------------------------BookList.java

package Sample4;

import java.util.Properties;

public class BookList {

       private Properties props;

       public void setProps(Properties props){

             this.props=props;

       }

       public Properties getProps(){

             return props;

       }

}

-----------------------------------------------------------------------------------------BookTest.java

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:ApplicationContext4.xml")

public class BookTest {

       @Autowired                  //ApplicationContext타입으로 자동연결

       private ApplicationContext ctx;

      

       @Test

       public void pro()

       {

             BookList b=ctx.getBean("client",BookList.class);

             Assert.assertEquals("1000",b.getProps().get("timeout"));

                                 }

------------------------------------------------------ApplicationContext.xml

<bean id="client" class="Sample4.BookList">

<property name="props">

       <props>                                          <!-- 속성Key value 설정 -->

             <prop key="server">192.168.0.10</prop>

             <prop key="timeout">1000</prop>

       </props>

</property>

</bean>

--------------------------------------------------------Info.java

package Sample5;

public class Info {

       private String name;

       public String getName() {

             return name;

       }

       public void setName(String name) {

             this.name = name;

       }

}

----------------------------------------------------------------------------------Auto.java

package Sample5;

public class Auto {

       private Info info1;

       private Info info2;

       public Info getInfo1() {

             return info1;

       }

       public void setInfo1(Info info1) {

             this.info1 = info1;

       }

       public Info getInfo2() {

             return info2;

       }

       public void setInfo2(Info info2) {

             this.info2 = info2;

       }

       public Auto(){                                  //기본생성자

             System.out.println("Auto Contstructor");

       }

       public Auto(Info info){                        //매개변수가 있는 생성자

             System.out.println("Auto Constructor - info");

       }

}

-----------------------------------------------------------------------AutoTest.java

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:ApplicationContext5.xml")

 

public class AutoTest {

       @Autowired

       private ApplicationContext ctx;

      

       @Test

       public void pro()

       {

             Auto b=ctx.getBean("t2",Auto.class);

       }

}

--------------------------------------------------------------------ApplicationContext.xml

<bean id="info2" class="Sample5.Info"/>   <!-- 이름으로 연결(byName) -->

<bean id="t2" class="Sample5.Auto" autowire="byName"/>

<!-- Setter연결(객체가 생성되어야 setter 불러올 있으므로 생성자 자동실행) -->

 

'SpringFramework' 카테고리의 다른 글

Spring websocket  (0) 2023.12.10
spring batch  (0) 2023.02.06
SpringFramework  (0) 2018.02.03
MVC,spring  (0) 2012.09.04
Spring Day4  (0) 2012.06.01