You will be fine

<Spring> 6. Spring basic, IoC, DI

by BFine
반응형

1. basic

  • 자바는 플랫폼에 독립적 언어(write once run anywhere : wora ) 소프트웨어 실행 환경

  • H/W + OS +JDK(JVM) ---> 자바 실행환경 ( 플랫폼 : 프로그램 실행 환경 )

  • framework 프로그래머가 프로그램을 만들기 위한 기본적인 골격 코드

  • 자바 디자인 패턴, IOC ,DI , AOP ,MVC ,DAO

1
2
3
4
5
6
7
    <!-- Generic properties -->// 버전 수정
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     <!-- Spring --> // spring 버전수정
        <spring-framework.version>5.0.5.RELEASE</spring-framework.version>
 
cs


※ C:\Users\컴퓨터명\.m2\repository\org\springframework // 라이브러리 삭제  , ※ POJO: PLAIN OLD JAVA OBJECT


2. IoC(Inversion of Control)

  • 결합도를 낮추는 방법,  XML 매핑을 통해 JAVA내부에서는 외부에서 전달받기만 하는 구조

  • DI : Dependency Injection, DL : Dependency Lookup

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
=========Dog(interface)=============
interface Dog {
    public void bark();
    public int avg_price();
}
==================================
public class Mala implements Dog{
    @Override
    public void bark() {
        // TODO Auto-generated method stub
        System.out.println("말라뮤트 : 에프! 에프!");
    }
    @Override
    public int avg_price() {
        // TODO Auto-generated method stub
        return 500000;
    }    
}
===================================
public class Beagle implements Dog{
    @Override
    public void bark() {
        // TODO Auto-generated method stub
        System.out.println("비글 : 알~알~");
    }
    @Override
    public int avg_price() {
        // TODO Auto-generated method stub
        return 200000;
    }
}
===================================
=============Main==================
ApplicationContext context=
             new ClassPathXmlApplicationContext("dog_xml.xml");
 // new ClassPathXmlApplicationContext // 여러개 쓸때 
                    //                         (new String[]{"dog_xml.xml",".xml"});
 
Dog dog1=context.getBean("dog1",Dog.class);
Dog dog2=context.getBean("dog2",Dog.class);
 dog1.bark();
  System.out.println("평균 가격 : "+dog1.avg_price());
  dog2.bark();
   System.out.println("평균 가격 : "+dog2.avg_price());
===================================        
 
cs

  • . XML 

1
2
3
4
5
6
7
<?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">
<!--<import resources="경로"> import 가능 -->
<bean id="dog1" class="test.Beagle"></bean>
<bean id="dog2" class="test.Mala"></bean>
</beans>
cs


실행





3. DI, Xml

  • Tomcat은 web server + servlet container+jsp container( 메서드를 자동으로 호출 ) 구성

  • DI : Dependency Injection,  java 문법 대신 XML에서 처리[ Spring container : XML->java로 변환 ] 

1
2
3
4
5
6
7
8
9
10
11
12
13
<bean id="객체명" class="패키지명.class 명" >
    <property name="변수명" value="값"/>
    <property name="참조변수명" ref="참조변수의 bean id" />
</bean>
===============생성자 이용======================
<constructor-arg index="" value=""/>
<constructor-arg index="" ref=""/
===============================================
xmlns:p="http://www.springframework.org/schema/p" 
<!-- p태그이용(Namespaces)-->
<bean id="객체명" class="패키지명.class 명"  p:변수명="변수값"
p:참조변수명-ref="참조변수의 bean id" />
    
cs

  • POJO= Spring bean 객체 

1
2
3
4
5
6
7
<bean id="" class=""
scope=""
init-method="" 처음 실행
destroy-method="" 마지막 실행
lazy-init="default" 객체생성을 getBean할때 생성(늦춤)
></bean>
 
cs

  • singleton[default] : 객체를 하나만 생성하여 공유(하나만) (==application 객체), prototype : 객체를 새로 만들는 것

4. List, Set, Map

  • List, Set, Map 인터페이스를 XML에서 DI가 가능하다. ※ setter 메소드 필수

  • XML 설정 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<bean id="dog" class="test.Beagle">
  <property name="list">
  <list>
  <value type="java.lang.Integer" >1</value>
  <value type="java.lang.Integer" >2</value>
  </list>
 </property>
  
 <property name="set">
  <set value-type="java.lang.Integer">
  <value>1</value>
  <value>2</value>
  </set>
 </property>
 
 <property name="map">
  <map key-type="java.lang.String" 
  value-type="java.lang.Integer" >
  <entry key="1번" value="1"></entry>
  <entry key="2번" value="2"></entry>  
  </map>
 </property>
</bean>
cs
  • JAVA에서 Bean 불러오기

1
2
3
4
5
6
7
8
9
10
11
12
Beagle beagle=context.getBean("dog",Beagle.class);
 
List<Integer> list=beagle.getList();
Set<Integer> set=beagle.getSet();
Map<String,Integer> map=beagle.getMap();
     
for(int i=0;i<list.size();i++)
 System.out.print(list.get(i)+" ");
     
System.out.println("\n"+set.toString());
System.out.println(map.get("1번")+" "+map.get("2번"));
     
cs



실행


반응형

블로그의 정보

57개월 BackEnd

BFine

활동하기