잡동사니에도 사랑을

[21.11.09] (Chapter02 - sample02) 본문

SPRING

[21.11.09] (Chapter02 - sample02)

luvforjunk 2021. 11. 9. 13:56
728x90
반응형

[문제1]

Project : chapter02

Package : sample02

Interface : Calc.java

추상Method : public void calculate();

Class : HelloSpring - main()

CalcAdd.java

CalcMul.java

 

[실행결과]

25 + 36 = xx

→ xml에서 CalcAdd를 빈(add)으로 Constructor Injection

 

25 * 36 = xx

→ xml에서 CalcMul를 빈(mul)으로 Setter Injection

 

 

////////Calc.java

package sample02;

public interface Calc {
	public void calculate(); // 추상메소드
	// Overiding해주기 위해 add와 mul이 들어온다.
	
}

 

 

 

////////CalcAdd.java

package sample02;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

// @AllArgsConstructor
@Component("calcAdd")
public class CalcAdd implements Calc {
	private int num1, num2;

	public CalcAdd(@Value("25") int num1, @Value("36") int num2) {

		this.num1 = num1;
		this.num2 = num2;
	}

	@Override
	public void calculate() {
		System.out.println(num1 + "+" + num2 + "=" + (num1 + num2));
	}
}

 

 

 

////////CalcMul.java

package sample02;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import lombok.Setter;

@Component
public class CalcMul implements Calc {
	// @Setter
	private int num1, num2;
	
	@Autowired
	public void setNum1(@Value("25") int num1) {
		this.num1 = num1;
	}

	@Autowired
	public void setNum2(@Value("36") int num2) {
		this.num2 = num2;
	}

	@Override
	public void calculate() {
		System.out.println(num1 + "*" + num2 + "=" + (num1 * num2));
	}

}

 

 

 

////////HelloSpring.java

package sample02;

import java.applet.Applet;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloSpring {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

		// 형 변환 (1)
		Calc calc;
		calc = (Calc) context.getBean("calcAdd");
		calc.calculate();
		
		// 형 변환 (2)
		calc = context.getBean("calcMul", Calc.class);
		calc.calculate();

		/*
		// 생성자 이용
		CalcAdd calcAdd = new CalcAdd(25, 36);

		// setter 이용
		CalcMul calcMul = new CalcMul();
		calcMul.setNum1(25); // xml에서 property name="" 에는 메소드명이 들어가야 하는데, setNum1이 곧 메소드명이다
		calcMul.setNum2(36);
		*/
	}
}

 

 

 

////////SpringConfiguration.java

package spring.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import sample01.MessageBeanImpl;
import sample02.CalcAdd;
import sample02.CalcMul;

@Configuration
public class SpringConfiguration {
// 환경설정 파일이므로 뉴 하지 않는다	

	@Bean
	// Bean이라고 명시하지 않으면 그냥 일반 자바클래스이다. Spring이 요구하는 값이 아니다.
	public MessageBeanImpl messageBeanImpl() { // 메소드명은 반드시 현재클래스의 객체명을 써준다 또는 Bean명
		return new MessageBeanImpl("포도");
	}

	@Bean
	public CalcAdd calcAdd() { // public 리턴되는명 메소드명=id명()
		return new CalcAdd(25, 36);
	}

	@Bean(name = "calcMul") // 빈 아이디 명으로 써라.
	public CalcMul getcalcMul() { // 메소드명
		return new CalcMul();
	}
}

 

 

 

////////applicationContext.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<!-- <context:component-scan base-package="sample01"></context:component-scan> -->
	<context:component-scan base-package="spring.conf"></context:component-scan>
	<!-- 빈이 설정되어 있는 곳을 가리킴 -->
	
	<!-- <context:component-scan base-package="sample02"></context:component-scan> -->
</beans>
728x90
반응형

'SPRING' 카테고리의 다른 글

[21.11.11] (sample03 - chapter02_SpringMaven)  (0) 2021.11.11
[21.11.10]  (0) 2021.11.09
[21.11.09] (Chapter02 - sample02 / 03 / 04)  (0) 2021.11.09
[21.11.08] (Chapter02)  (0) 2021.11.08
[21.11.08] (chapter01)  (0) 2021.11.08