spring framework

Spring Project Core

=> DI / AOP
=> Spring MVC(Model2)
=> Spring Boot

DI(Dipendency Injection : 의존성 주입)

: ioc를 중심 객체(생성, 소멸)의 관리를 spring이 대신 객체의 관리 설정

  • XML - Spring Bean Configuration File
  • Annotation

의존성 주입

멤버 필드에 대한 초기화

  • 생성자 주입
  • setter메서드 주입

Spring Project 구성

  • Java Project + 수동 라이브러리 적용
  • Maven Projcet
    jdk버전 문제발생
    java project -> maven으로 변경
  • Gradle Project
  • STS(Spring Tool Suite)
    -> eclipse + 라이브러리(https://spring.io/tools)

Spring을 통한 출력 예제

1

<?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-4.3.xsd">

	<bean name="helloBean" class="com.exam.spring01.HelloBean" scope="prototype"/>
	
</beans>
public class HelloBean {
	public void sayHello(String name) {
		System.out.println(name + "hi");
	}
}
//
public class App {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		GenericXmlApplicationContext ctx
		= new GenericXmlApplicationContext("classpath:com/exam/spring01/context.xml");
		
		HelloBean helloBean = (HelloBean)ctx.getBean("helloBean");
		helloBean.sayHello("ㅇㅇㅇ");
		
		ctx.close();
	}

}

2

BoardTO

public class BoardTO {
	private int seq;
	private String subject;
	
	public int getSeq() {
		return seq;
	}
	public void setSeq(int seq) {
		System.out.println("setSeq(int seq) 호출");
		this.seq = seq;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		System.out.println("setSubject(String subject) 호출");
		this.subject = subject;
	}
}

App

public class App {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 일반적인 방법
		BoardTO to = new BoardTO();
		to.setSeq(1);
		to.setSubject("제목 1");

		System.out.println(to.getSeq());
		System.out.println(to.getSubject());
	}
}

context.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-4.3.xsd">

	<bean name="to" class="com.exam.spring02.BoardTO" scope="prototype">
		<property name="seq" value="2"/>
		<property name="subject" value="제목 2"/>
	</bean>

	<bean name="writeAction1" class="com.exam.spring02.WriteAction" scope="prototype">
		<constructor-arg ref="to"/> <!-- to를 참조 -->
	</bean>
	
	<bean name="writeAction2" class="com.exam.spring02.WriteAction" scope="prototype">
		<constructor-arg>
			<bean class="com.exam.spring02.BoardTO" scope="prototype">
				<property name="seq" value="5"/>
				<property name="subject" value="제목 5"/>
			</bean>
		</constructor-arg>
	</bean>
	
	<!-- to값을 가져온다 -->
	<bean name="listAction1" class="com.exam.spring02.ListAction" scope="prototype">
		<property name="to" ref="to"/>
	</bean>
	
	<!-- 내부적으로 새로 설정한다 -->
	<bean name="listAction2" class="com.exam.spring02.ListAction" scope="prototype">
		<property name="to">
			<bean class="com.exam.spring02.BoardTO" scope="prototype">
				<property name="seq" value="6"/>
				<property name="subject" value="제목 6"/>
			</bean>
		</property>
	</bean>
</beans>

App2

public class App2 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		GenericXmlApplicationContext ctx
		= new GenericXmlApplicationContext("classpath:com/exam/spring02/context.xml");
		
		// context.xml의 to값을 가져옴
		BoardTO to = (BoardTO)ctx.getBean("to");
		System.out.println(to.getSeq());
		System.out.println(to.getSubject());
		
		// 값을 재입력 -> 출력한다.
		to.setSeq(3);
		to.setSubject("제목 3");
		System.out.println(to.getSeq());
		System.out.println(to.getSubject());
		
		ctx.close();
	
	}
}

WriteAction

public class WriteAction {
	private BoardTO to;
	
	public WriteAction(BoardTO to) {
		// TODO Auto-generated constructor stub
		System.out.println("WriteAction(BoardTO to) 호출");
		
		this.to = to;
	}
	
	public void execute() {
		System.out.println(to.getSeq());
		System.out.println(to.getSubject());
	}
}

App3

package com.exam.spring02;

public class App3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BoardTO to = new BoardTO();
		to.setSeq(4);
		to.setSubject("제목 4");
		
		WriteAction action = new WriteAction(to);
		action.execute();
	}

}
=> WriteAction통해 xml의 BoardTO를 불러옴.


App4

public class App4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		GenericXmlApplicationContext ctx
		= new GenericXmlApplicationContext("classpath:com/exam/spring02/context.xml");
		
		WriteAction writeAction1 = (WriteAction)ctx.getBean("writeAction1");
		writeAction1.execute();
		//
		WriteAction writeAction2 = (WriteAction)ctx.getBean("writeAction2");
		writeAction2.execute();
		
		ctx.close();
	}

}


ListAction

public class ListAction {
	private BoardTO to;
	
	public BoardTO getTo() {
		return to;
	}

	public void setTo(BoardTO to) {
		this.to = to;
	}

	public void execute() {
		System.out.println(to.getSeq());
		System.out.println(to.getSubject());
	}
}

App5

public class App5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		GenericXmlApplicationContext ctx
		= new GenericXmlApplicationContext("classpath:com/exam/spring02/context.xml");
		
		ListAction listAction1 = (ListAction)ctx.getBean("listAction1");
		listAction1.execute();
		
		ListAction listAction2 = (ListAction)ctx.getBean("listAction2");
		listAction2.execute();
	
		ctx.close();
	}

}


context.xml 선언 종류

context.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:p="http://www.springframework.org/schema/p"
    /////
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
	<!-- 방법1 -->
	<bean id="to1" class="com.exam.spring03.BoardTO" scope="prototype">
		<property name="seq" value="1"/>
		<property name="subject" value="제목 1"/>
	</bean>
	<!-- 방법2 -->
	<bean id="to2" class="com.exam.spring03.BoardTO"  p:seq="2" p:subject="제목 2" scope="prototype"/>
</beans>

BoradTO

public class BoardTO {
	private int seq;
	private String subject;
	
	public int getSeq() {
		return seq;
	}
	public void setSeq(int seq) {
		System.out.println("setSeq(int seq) 호출");
		this.seq = seq;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		System.out.println("setSubject(String subject) 호출");
		this.subject = subject;
	}
}

App

public class App {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		GenericXmlApplicationContext ctx
		= new GenericXmlApplicationContext("classpath:com/exam/spring03/context.xml");
		
		BoardTO to1 = (BoardTO)ctx.getBean("to1");
		System.out.println(to1.getSeq());
		System.out.println(to1.getSubject());
		
		BoardTO to2 = (BoardTO)ctx.getBean("to2");
		System.out.println(to2.getSeq());
		System.out.println(to2.getSubject());
		
		ctx.close();
	}
}

0개의 댓글

Powered by GraphCDN, the GraphQL CDN