본문 바로가기

🎧️ 강의듣기

Spring Framework (2)

Spring

            java                *

            kotlin              (android(google))

                    https://spring.io/guides/tutorials/spring-boot-kotlin/

            groovy

 

spring 생성 방법

            개발용 IDE

                  * library 추가

                   eclipse

                                + STS plug-in

                   intelliJ IDEA

                   Visual Studio Code

            library 추가

            1. 수동

            2. 프로젝트 관리 도구

                        maven / gradle

 

spring 관리

                pom.xml

                Spring bean Configuration → 객체의 의존성 설정 → DI

                    생성

                                <bean name="helloBean1" class="com.exam.ex02.HelloBean" />

                                <bean id="helloBean1" class="com.exam.ex02.HelloBean" />

                    초기화

                                <constructor-arg>

                                <property name="seq" value="1" />

 

com.exam.ex01/Hello.java (인터페이스)

package com.exam.ex01;

public interface Hello {
	void sayHello( String name );
}

 

com.exam.ex01/HelloBean1.java

// HelloBean1.java
package com.exam.ex01;

public class HelloBean1 implements Hello {

	public HelloBean1() {
		System.out.println( "HelloBean1() 호출" );
	}
	@Override
	public void sayHello(String name) {
		System.out.println( name + "님 안녕하세요" );
	}
}

// HelloBean2.java

package com.exam.ex01;

public class HelloBean2 implements Hello {

	public HelloBean2() {
		System.out.println( "HelloBean2() 호출" );
	}
	@Override
	public void sayHello(String name) {
		System.out.println( name + "님 안녕하세요" );
	}
}

 

com.exam.ex01/BeanConfig.java

package com.exam.ex01;

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

@Configuration
public class BeanConfig {
	
	@Bean
	public HelloBean1 helloBean1() {
		return new HelloBean1();
	}
	
	@Bean
	@Scope("prototype")
	public HelloBean2 helloBean2() {
		return new HelloBean2();
	}
	
	/* 
	 * helloBean이름으로 사용할것임
	 * @Bean("helloBean") 으로 사용가능
	 * */
	@Bean("helloBean")
	@Scope("prototype")
	public HelloBean2 helloBean3() {
		return new HelloBean2();
	}
}

 

 

com.exam.ex01/App.java

package com.exam.ex01;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		AnnotationConfigApplicationContext ctx
		= new AnnotationConfigApplicationContext( BeanConfig.class );
		
		HelloBean1 helloBean1 = (HelloBean1)ctx.getBean( "helloBean1" );
		
		helloBean1.sayHello( "홍길동" );
		
		Hello hello = (Hello)ctx.getBean( "helloBean1" );
		hello.sayHello( "박문수" );
		
		HelloBean2 helloBean2 = (HelloBean2)ctx.getBean( "helloBean2" );
		helloBean2.sayHello( "임꺽정 1 : " + helloBean2 );
		
		HelloBean2 helloBean3 = (HelloBean2)ctx.getBean( "helloBean2" );
		helloBean3.sayHello( "임꺽정 2 : " + helloBean2 );
		
		HelloBean2 helloBean4 = (HelloBean2)ctx.getBean( "helloBean" );
		helloBean4.sayHello( "임꺽정 3 : " + helloBean4 );
		
		
		ctx.close();
	}

}

 

패키지 만들고

com.exam.ex02/HelloBean.java

package com.exam.ex02;

public class HelloBean {
	private String name;
	
	
	public HelloBean() {
		// TODO Auto-generated constructor stub
		this.name = "홍길동";
		System.out.println( "HelloBean() 호출" );
	}
	
	/* 디폴트 생성자 */
	public HelloBean( String name ) {
		this.name = name;
		System.out.println( "HelloBean(String name) 호출" );
	}
	public void sayHello() {
		System.out.println( name + "님 안녕하세요" );
	}
}

 

com.exam.ex02/BeanConfig.java

package com.exam.ex02;

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

@Configuration
public class BeanConfig {

	@Bean
	public HelloBean helloBean1() {
		return new HelloBean();
	}
	
	@Bean
	public HelloBean helloBean2() {
		return new HelloBean( "박문수" );
	}
}

 

com.exam.ex02/App.java

package com.exam.ex02;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class App {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		AnnotationConfigApplicationContext ctx
		= new AnnotationConfigApplicationContext( BeanConfig.class );
		
		HelloBean helloBean1 = (HelloBean)ctx.getBean( "helloBean1" );
		helloBean1.sayHello();
		
		HelloBean helloBean2 = (HelloBean)ctx.getBean( "helloBean2" );
		helloBean2.sayHello();
        
        // 또 다른 방법
        // ctx앞에 (HelloBean)를 해줄 필요가 없음
        HelloBean helloBean1 = ctx.getBean( "helloBean1", HelloBean.class );
		HelloBean helloBean2 = ctx.getBean( "helloBean2", HelloBean.class );
		
		helloBean1.sayHello();		
		helloBean2.sayHello();

		
		ctx.close();
	}

}

 

set만들기

 

com.exam.ex03/BoardTO.java

package com.exam.ex03;

public class BoardTO {
	private int seq;
	private String subject;
	
	public int getSeq() {
		return seq;
	}
	public void setSeq(int seq) {
		this.seq = seq;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
}

getter / setter 만들기

 

com.exam.ex03/BeanConfig.java

package com.exam.ex03;

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

@Configuration
public class BeanConfig {
	
	@Bean
	public BoardTO boardTO() {
		BoardTO to = new BoardTO();
		to.setSeq(1);
		to.setSubject("제목1");
		
		return to;
	}
}

 

com.exam.ex03/App.java

package com.exam.ex03;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.exam.ex03.BeanConfig;

public class App {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx
		= new AnnotationConfigApplicationContext( BeanConfig.class );
		// BeanConfig.java에 boardTO 호출하여 값 세팅 후 출력
		BoardTO to = (BoardTO)ctx.getBean("boardTO");
		
		System.out.println( to.getSeq() );
		System.out.println( to.getSubject() );
		
		ctx.close();
	}
}

 

com.exam.ex05/HelloBean.java

// HelloBean1.java
package com.exam.ex05;

public class HelloBean1 implements Hello {

	public HelloBean1() {
		System.out.println( "HelloBean1() 호출" );
	}
	@Override
	public void sayHello(String name) {
		System.out.println( name + "님 안녕하세요" );
	}
}

// HelloBean2.java
package com.exam.ex05;
public class HelloBean2 implements Hello {

	public HelloBean2() {
		System.out.println( "HelloBean2() 호출" );
	}
	@Override
	public void sayHello(String name) {
		System.out.println( "Hello " + name );
	}
}

// HelloBean3.java
package com.exam.ex05;
public class HelloBean3 implements Hello {

	public HelloBean3() {
		System.out.println( "HelloBean3() 호출" );
	}
	@Override
	public void sayHello(String name) {
		System.out.println( "hi " + name );
	}
}

 

com.exam.ex05/BeanConfig.java

// BeanConfig1.java

package com.exam.ex05;

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

@Configuration
@Import({BeanConfig2.class, BeanConfig3.class})
public class BeanConfig1 {

	@Bean
	public HelloBean1 helloBean1() {
		return new HelloBean1();
	}
}

// BeanConfig2.java

package com.exam.ex05;

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

@Configuration
public class BeanConfig2 {

	@Bean
	public HelloBean2 helloBean2() {
		return new HelloBean2();
	}
}

// BeanConfig3.java

package com.exam.ex05;

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

@Configuration
public class BeanConfig3 {

	@Bean
	public HelloBean3 helloBean3() {
		return new HelloBean3();
	}
}

 

com.exam.ex05/Hello.java

package com.exam.ex05;

public interface Hello {
	void sayHello( String name );
}

 

com.exam.ex05/App.java

package com.exam.ex05;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        // AnnotationConfigApplicationContext ctx
        // = new AnnotationConfigApplicationContext( BeanConfig1.class, BeanConfig2.class );
    	AnnotationConfigApplicationContext ctx
        = new AnnotationConfigApplicationContext( BeanConfig1.class );
        
        HelloBean1 helloBean1 = (HelloBean1)ctx.getBean("helloBean1");
        HelloBean2 helloBean2 = (HelloBean2)ctx.getBean("helloBean2");
        HelloBean3 helloBean3 = (HelloBean3)ctx.getBean("helloBean3");
        
        helloBean1.sayHello("홍길동");
        helloBean2.sayHello("박문수");
        helloBean3.sayHello("임꺽정");
        
        ctx.close();
    }
}

'🎧️ 강의듣기' 카테고리의 다른 글

Spring MVC  (0) 2021.08.10
스프링 프레임워크  (0) 2021.08.09
Spring Framework  (0) 2021.08.05
이클립스 Spring 설정하기  (0) 2021.08.05
Java를 위한 프레임 워크 mybatis  (0) 2021.08.02