Spring Framework

조성현·2023년 5월 24일
0

UI

  1. jQuery
  • jQuery UI - w3ui ..
  • Bootstrap - template ..
  1. x
  • Vue
  • React

Spring Framework

: 다양한 기능 - 객체를 다루는 기능
(https://spring.io/)
=> 전자정부프레임워크의 핵심
=> 유니콘 기업에서 많이 사용

Spring 개발 종류

application 개발 <- back-end + 서버 부분
web 개발

Spring의 핵심

Spring Bean Configuration File(Assembler)

객체생성 시점

  • 클래스(스태틱) 멤버필드 : 프로그램 실행 시
  • 인스턴스 멤버필드 : 인스턴화 될 때 마다

=> singleton(클래스와 비슷) / prototype(인스턴스와 비슷)

Spring으로 출력하기

객체생성을 통한 출력

// 생성자
public class HelloBean1 {
	public void sayHello(String name) {
		System.out.println(name + "님 안녕하세요");
	}
}
public class HelloBean2 {
	public void sayHello(String name) {
		System.out.println("Hello " + name);
	}
}
// 실행 클래스
public class App {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 객체의 생성을 프로그래머가 담당한다.
		HelloBean1 bean1 = new HelloBean1();
		bean1.sayHello("가나다");
		
		HelloBean2 bean2 = new HelloBean2();
		bean2.sayHello("라마바");
	}

}

인터페이스를 통한 출력

// 인터페이스
public interface Hello {
	void sayHello(String name);
}
// 생성자
public class HelloBean1 implements Hello {

	@Override
	public void sayHello(String name) {
		// TODO Auto-generated method stub
		System.out.println(name + "님 안녕하세요");
	}

}
public class HelloBean2 implements Hello {

	@Override
	public void sayHello(String name) {
		// TODO Auto-generated method stub
		System.out.println("Hello " + name);
	}

}
// 실행 클래스
public class App {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		// 다형성
		Hello hello = new HelloBean1(); // 객체생성
		hello.sayHello("사아자");
		System.out.println(hello);
		
		hello = new HelloBean2(); // 재생성
		hello.sayHello("차카타");
		System.out.println(hello);
	}

}

spring 출력 예시

// 생성자
public class HelloBean1 {
	
	public HelloBean1() {
		// TODO Auto-generated constructor stub
		System.out.println("HelloBean1() 호출");
	}
	
	public void sayHello(String name) {
		System.out.println(name + "님 안녕하세요");
	}
}
public class HelloBean2 {
	
	public HelloBean2() {
		// TODO Auto-generated constructor stub
		System.out.println("HelloBean2() 호출");
	}
	
	public void sayHello(String name) {
		System.out.println("Hello " + name);
	}
}
// 실행 클래스
public class App {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		GenericXmlApplicationContext ctx
		= new GenericXmlApplicationContext("classpath:com/exam/spring03/context.xml");
		
		//HelloBean1 helloBean1 = (HelloBean1)ctx.getBean("helloBean1");
		HelloBean1 helloBean1 = (HelloBean1)ctx.getBean("helloBean3");
		helloBean1.sayHello("qwer");
		HelloBean2 helloBean2 = (HelloBean2)ctx.getBean("helloBean2");
		helloBean2.sayHello("asdf");
		
		ctx.close();
	}

}
<?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">

	<!-- HelloBean1 helloBean1 = new HelloBean1()와 같은 역할-->
	<bean name="helloBean1" class="com.exam.spring03.HelloBean1"/>
	<bean name="helloBean2" class="com.exam.spring03.HelloBean2"/>
	
	<bean id="helloBean3" class="com.exam.spring03.HelloBean1"/>
</beans>

인터페이스를 포함한 spring 출력

// 인터페이스
public interface Hello {
	void sayHello(String name);
}
// 생성자
public class HelloBean1 implements Hello {

	@Override
	public void sayHello(String name) {
		// TODO Auto-generated method stub
		System.out.println(name + "님 안녕하세요");
	}

}
public class HelloBean2 implements Hello {

	@Override
	public void sayHello(String name) {
		// TODO Auto-generated method stub
		System.out.println("Hello " + name);
	}

}
// 실행 클래스
public class App {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		GenericXmlApplicationContext ctx
		= new GenericXmlApplicationContext("classpath:com/exam/spring04/context.xml");
		
		//HelloBean1 hello = (HelloBean1)ctx.getBean("helloBean1");
		// 이름값과 인터페이스만 알면 된다.
		Hello hello = (Hello)ctx.getBean("helloBean1");
		hello.sayHello("ㅇㅇㅇ");
		hello = (Hello)ctx.getBean("helloBean2");
		hello.sayHello("ㅇㅇㅇ");
		
		ctx.close();
	}

}
<?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">

	<!-- HelloBean1 helloBean1 = new HelloBean1() -->
	<bean name="helloBean1" class="com.exam.spring04.HelloBean1"/>
	<bean name="helloBean2" class="com.exam.spring04.HelloBean2"/>
	
</beans>

singleton과 prototype

<?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="helloBean1" class="com.exam.spring05.HelloBean1" scope="prototype"/>
	<bean name="helloBean2" class="com.exam.spring05.HelloBean2" scope="prototype"/>
	-->
	<!-- singleton은 디폴트 값 -->
	<bean name="helloBean1" class="com.exam.spring05.HelloBean1" scope="singleton"/>
	<bean name="helloBean2" class="com.exam.spring05.HelloBean2"/>
</beans>
// 생성자
public class HelloBean1 {
	public HelloBean1() {
		// TODO Auto-generated constructor stub
		System.out.println("HelloBean1() 호출");
	}
	public void sayHello(String name) {
		System.out.println(name + "님 안녕하세요");
	}
}
public class HelloBean2 {
	public HelloBean2() {
		// TODO Auto-generated constructor stub
		System.out.println("HelloBean2() 호출");
	}
	public void sayHello(String name) {
		System.out.println("Hello " + name);
	}
}
// 실행 클래스
public class App {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		GenericXmlApplicationContext ctx
		= new GenericXmlApplicationContext("classpath:com/exam/spring05/context.xml");
		
		HelloBean1 helloBean1 = (HelloBean1)ctx.getBean("helloBean1"); // 생성자 호출
		helloBean1.sayHello("ㅂㅂㅂ : " + helloBean1);
		
		HelloBean1 helloBean3 = (HelloBean1)ctx.getBean("helloBean1"); // 생성자 호출
		helloBean3.sayHello("ㅈㅈㅈ : " + helloBean3);
		// => prototype : 객체 형태는 같아도 객체 형성 메모리 구조는 다르기 때문에 주소값이 다르다(그때 그때 마다 새로생성해서 출력됨)
		// => singleton : 객체형태와 메모리 구조도 같기 때문에 주소값이 똑같다
		
		/*
		HelloBean2 helloBean2 = (HelloBean2)ctx.getBean("helloBean2"); // 생성자 호출
		helloBean2.sayHello("ㅁㅁㅁ");
		*/
		
		ctx.close();
	}

}

singleton

prototype

나눠서 출력하기

<?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="helloBean1" class="com.exam.spring06.HelloBean" scope="prototype"/>
	<bean name="helloBean2" class="com.exam.spring06.HelloBean" scope="prototype">
	
		<constructor-arg>
			<value>ㅇㅇㅇ</value>
		</constructor-arg>
	
		<constructor-arg value="ㅁㅁㅁ"/>
	</bean>
	
	<bean name="helloBean3" class="com.exam.spring06.HelloBean" scope="prototype">
      <!-- 동시에 사용하면 에러발생 -->
		<!-- 방법1 -->
		<constructor-arg>
			<value></value>
		</constructor-arg>
		<constructor-arg>
			<value>나다</value>
		</constructor-arg>
		
		<!-- 방법2 -->
		<constructor-arg value=""/>
		<constructor-arg value="ㅁㅁ"/>
	</bean>
	
</beans>
// 생성자
public class HelloBean {
	private String name;
	
	public HelloBean() {
		// TODO Auto-generated constructor stub
		System.out.println("HelloBean() 호출");
		this.name = "ㅇㅇㅇ";
	}
	
	public HelloBean(String name) {
		System.out.println("HelloBean(String name) 호출");
		this.name = name;
	}
	public HelloBean(String firstName, String lastName) {
		System.out.println("HelloBean(String firstName, String lastName) 호출");
		this.name = lastName + " " + firstName;
	}
	
	public void sayHello() {
		System.out.println(this.name + " 님 안녕");
	}
}
// 실행 클래스
public class App {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		GenericXmlApplicationContext ctx
		= new GenericXmlApplicationContext("classpath:com/exam/spring06/context.xml");
		
		HelloBean helloBean1 = (HelloBean)ctx.getBean("helloBean1"); // 생성자 호출
		helloBean1.sayHello();
		
		HelloBean helloBean2 = (HelloBean)ctx.getBean("helloBean2"); // 생성자 호출
		helloBean2.sayHello();
		
		HelloBean helloBean3 = (HelloBean)ctx.getBean("helloBean3"); // 생성자 호출
		helloBean3.sayHello();
		
		ctx.close();
	}

}

TO를 활용해서 출력

<?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.spring08.BoardTO" scope="prototype">
      	<!-- 값을 입력한다 -->
		<constructor-arg value="1"/>
		<constructor-arg value="제목 1"/>
	</bean>
	<bean name="listAction" class="com.exam.spring08.ListAction" scope="prototype">
		<constructor-arg>
			<ref bean="to"/>
		</constructor-arg>
	</bean>
</beans>
// TO
public class BoardTO {
	private String seq;
	private String subject;
	
	public BoardTO(String seq, String subject) {
		// TODO Auto-generated constructor stub
		System.out.println("BoardTO(String seq, String subject) 호출");
		this.seq = seq;
		this.subject = subject;
	}

	public String getSeq() {
		return seq;
	}

	public String getSubject() {
		return subject;
	}
}
// 생성자
public class ListAction {
	private BoardTO to;
	
	public ListAction(BoardTO to) {
		// TODO Auto-generated constructor stub
		System.out.println("ListAction(BoardTO to) 호출");
		this.to = to;
	}
	
	public void execute() {
		System.out.println(to.getSeq());
		System.out.println(to.getSubject());
	}
}
// 실행 클래스
public class App {

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

}

0개의 댓글