[Spring] xml사용하기1

     

    Calculator.java

    package com.javalec.ex;
    
    public class Calculator {
    	
    	public void addition(int firstNum, int secondNum) {
    		System.out.println("addition()");
    		int result = firstNum + secondNum;
    		System.out.println(firstNum + " + " + secondNum + " = " + result);
    	}
    
    	public void subtraction(int firstNum, int secondNum) {
    		System.out.println("subtraction()");
    		int result = firstNum - secondNum;
    		System.out.println(firstNum + " - " + secondNum + " = " + result);
    	}
    
    	public void multiplication(int firstNum, int secondNum) {
    		System.out.println("multiplication()");
    		int result = firstNum * secondNum;
    		System.out.println(firstNum + " * " + secondNum + " = " + result);
    	}
    
    	public void division(int firstNum, int secondNum) {
    		System.out.println("division()");
    		int result = firstNum / secondNum;
    		System.out.println(firstNum + " / " + secondNum + " = " + result);
    	}
    }
    

     

     

    MyCalculator.java

    package com.javalec.ex;
    
    public class MyCalculator {
    
    	Calculator calculator;
    	private int firstNum;
    	private int secondNum;
    	
    	public void add() {
    		calculator.addition(firstNum, secondNum);
    	}
    	public void sub() {
    		calculator.subtraction(firstNum, secondNum);
    	}
    	public void mul() {
    		calculator.multiplication(firstNum, secondNum);
    	}
    	public void div() {
    		calculator.division(firstNum, secondNum);
    	}
    	
    	public void setCalculator(Calculator calculator) {
    		this.calculator = calculator;
    	}
    	public void setFirstNum(int firstNum) {
    		this.firstNum = firstNum;
    	}
    	public void setSecondNum(int secondNum) {
    		this.secondNum = secondNum;
    	}
    	
    }
    

     

     

    MainClass.java

    package com.javalec.ex;
    
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.GenericXmlApplicationContext;
    
    import com.Ex01.MyInfo;
    
    public class MainClass {
    
    	public static void main(String[] args) {
    
    		
    		String configlocation = "classpath:applicationCTX.xml";
    		AbstractApplicationContext ctx = new GenericXmlApplicationContext(configlocation);
    		
    		MyInfo myinfo = ctx.getBean("bmiCalculator", MyInfo.class);
    		
    		myinfo.getInfo();
    		
    	}
    
    }
    

     

     

    applicationCTX.xml

    <?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-2.0.xsd">
    
    	<bean id="calculator2" class="com.javalec.ex.Calculator" />
    
    	<bean id="myCalculator" class="com.javalec.ex.MyCalculator">
    		<property name="calculator">
    			<ref bean="calculator2" />
    		</property>
    		<property name="firstNum" value="10" />
    		<property name="secondNum" value="2" />
    	</bean>
    
    </beans>
    

     

     

     

    xml은 코드의 setter없이 자동으로 넣어준다.

    JSP의 Beans와 java의 객체 주입 방식을 이해 했다면 쉽게 배울 수 있다.

    반응형

    'Web > Spring' 카테고리의 다른 글

    [Spring] Xml Java vs XML  (0) 2020.10.21
    [Spring] xml 활용하기  (0) 2020.10.21
    [Spring] xml 사용하기3  (0) 2020.10.20
    [Spring] xml 사용하기2  (0) 2020.10.20
    [Spring] 설치하기  (0) 2020.10.20

    댓글

    Designed by JB FACTORY