전체 글 (246)
2022-03-18 16:44:51
반응형

실습 4일차_@Value_resourceBundle

 

 

 

 Spring @Value_resourceBundle

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Cat;

public class MainClass {
	public static void main(String[] args) {
		
		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/test.xml");

		Cat c = ctx.getBean("cat1", Cat.class);
		System.out.printf("고양이 이름:%s 나이:%s 성별:%s", c.getName(), c.getAge(), c.getSex());	
	}
}

Cat.java

package com.dto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

public class Cat {
	
	@Value("${cat.name}")
	String name;
	@Value("${cat.age}")
	int age;
	@Value("${cat.sex}")
	String sex;
	
	public Cat() {

	}

	// 기본 생성자 안 만들고 인자있는 생성자 만듬
	public Cat(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Cat [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
}

Person.java

package com.dto;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Person {
	
	String username;
	int userage;
	String address;
	
	//@Autowired(required = false)
	//@Qualifier("cat1")
	@Resource(name = "cat2")
	Cat cat; // null
	
	public Person() {
	
	}

	// 기본 생성자 안 만들고 인자있는 생성자 만듬
	public Person(String username, int userage, String address, Cat cat) {
		super();
		this.username = username;
		this.userage = userage;
		this.address = address;
		this.cat = cat;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getUserage() {
		return userage;
	}

	public void setUserage(int userage) {
		this.userage = userage;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Cat getCat() {
		return cat;
	}

	public void setCat(Cat cat) {
		this.cat = cat;
	}

	@Override
	public String toString() {
		return "Person [username=" + username + ", userage=" + userage + ", address=" + address + ", cat=" + cat + "]";
	}
	
}

test.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:annotation-config />

	<context:property-placeholder location="classpath:com/config/cat.properties" file-encoding="utf-8"/> 

	<bean id="cat1" class="com.dto.Cat" />
	<!-- 
	<bean id="cat1" class="com.dto.Cat"> < 생성자로 주입   property라는 setter method 이용-->
		<!-- 	<property name="name" value="나비" />
		<property name="age" value="3" />
		<property name="sex" value="암컷" />
	</bean>
	 -->

</beans>

 

<!-- @ 어노테이션 활성화 -->
<context:annotation-config />

* context 활성화 방법

[Namespaces 탭] → [context] Check → Configure Namespaces 알림창 OK 버튼 마우스로 클릭 

 

 

cat.properties

cat.name = 망치 
cat.age = 2
cat.sex = 수컷

 

* 출력 화면 

 

 출처

 

@Value: 

+ 강의 교재

 

반응형
2022-03-18 16:38:15
반응형

실습 4일차_@Resource

 

 

 

 Spring @Resource

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Cat;
import com.dto.Person;

public class MainClass {
	public static void main(String[] args) {
		
		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/test.xml");

		Person p1 = ctx.getBean("p1", Person.class);
		Cat c = p1.getCat();
		
		System.out.printf("이름:%s 나이:%s 주소:%s \n", p1.getUsername(), p1.getUserage(), p1.getAddress()); // printformat
		System.out.printf("고양이 이름:%s 나이:%s 성별:%s", c.getName(), c.getAge(), c.getSex());	
	}
}

Cat.java

package com.dto;
import org.springframework.beans.factory.annotation.Autowired;

public class Cat {
	String name;
	int age;
	String sex;
	
	public Cat() {

	}

	// 기본 생성자 안 만들고 인자있는 생성자 만듬
	public Cat(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Cat [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
}

Person.java

package com.dto;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Person {
	
	String username;
	int userage;
	String address;
	
	//@Autowired(required = false)
	//@Qualifier("cat1")
	@Resource(name = "cat2")
	Cat cat; // null
	
	public Person() {
	
	}

	// 기본 생성자 안 만들고 인자있는 생성자 만듬
	public Person(String username, int userage, String address, Cat cat) {
		super();
		this.username = username;
		this.userage = userage;
		this.address = address;
		this.cat = cat;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getUserage() {
		return userage;
	}

	public void setUserage(int userage) {
		this.userage = userage;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Cat getCat() {
		return cat;
	}

	public void setCat(Cat cat) {
		this.cat = cat;
	}

	@Override
	public String toString() {
		return "Person [username=" + username + ", userage=" + userage + ", address=" + address + ", cat=" + cat + "]";
	}
	
}

user.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:annotation-config />

	<bean id="cat1" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비" />
		<property name="age" value="3" />
		<property name="sex" value="암컷" />
	</bean>
	
	<bean id="cat2" class="com.dto.Cat"> <!-- 생성자로 주입  -->
		<property name="name" value="나비2" />
		<property name="age" value="3" />
		<property name="sex" value="암컷" />
	</bean>
	
	<bean id="p1" class="com.dto.Person">
		<property name="username" value="고길동" />
		<property name="userage" value="30" />
		<property name="address" value="서울" />
		<!--  <property name="cat" ref="cat" /> 자동으로 주입  Cat.java @Autowired 코드 확인-->
	</bean>
</beans>

 

<!-- @ 어노테이션 활성화 -->
<context:annotation-config />

* context 활성화 방법

[Namespaces 탭] → [context] Check → Configure Namespaces 알림창 OK 버튼 마우스로 클릭 

 

 

 

* 출력 화면 

 

 출처

 

@Resource : 

+ 강의 교재

 

반응형
2022-03-18 08:54:32
반응형

실습 3일차_@Autowired

 

 

 

 Spring @Autowired

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;

public class MainClass {
	public static void main(String[] args) {

		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		UserService service = ctx.getBean("service", UserService.class);
		List<String> list = service.list();
		System.out.println(list);
		
		UserService service2 = ctx.getBean("service2", UserService.class);
		List<String> list2 = service2.list();
		System.out.println(list2);
		
		UserService service3 = ctx.getBean("service3", UserService.class);
		List<String> list3 = service3.list();
		System.out.println(list3);
	}
}

UserDAO.java

package com.dao;
import java.util.Arrays;
import java.util.List;

public class UserDAO {

	// DB 연동 가정
	public List<String> list(){
		List<String> list = Arrays.asList("홍길동", "고길동", "신길동");
		return list;
	}
}

UserService.java

package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.dao.UserDAO;

public class UserService {

	@Autowired
	UserDAO dao;
	
	// user.xml 2. property는 기본 생성자가 있어야 에러가 안남
	public UserService() {
		
	}
	
	// 생성자 주입
	public UserService(UserDAO dao) {
		this.dao = dao;
	}
	
	// setter 메서드 주입
	public void setDao(UserDAO dao) {
		this.dao = dao;
	}

	public List<String> list(){
		return dao.list();
	}
}

user.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">

	<bean id="dao" class="com.dao.UserDAO" />
	
	<!-- @ 어노테이션 활성화  -->
	<context:annotation-config />
	
	<!-- 1. xml의 constructor를 이용한 의존성 주입_Injection -->
	<bean id="service" class="com.service.UserService">
		<constructor-arg name="dao" ref="userDAO" />
	</bean>
	
	<!-- 2. xml의 setter 메서드를 이용한 의존성 주입_Injection -->
	<bean id="service2" class="com.service.UserService">
		<property name="dao" ref="userDAO" />
	</bean>
	
	<!-- 3. @Autowired를 이용한 자동 의존성 주입_Injection -->
	<bean id="service3" class="com.service.UserService" />

</beans>

 

<!-- @ 어노테이션 활성화 -->
<context:annotation-config />

* context 활성화 방법

[Namespaces 탭] → [context] Check → Configure Namespaces 알림창 OK 버튼 마우스로 클릭 

 

 

 

* 출력 화면 

 

 Spring @Autowired_required_false

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;

public class MainClass {
	public static void main(String[] args) {

		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		UserService service3 = ctx.getBean("service3", UserService.class);
		List<String> list3 = service3.list();
		System.out.println(list3);
	}
}

UserDAO.java

package com.dao;
import java.util.Arrays;
import java.util.List;

public class UserDAO {

	// DB 연동 가정
	public List<String> list(){
		List<String> list = Arrays.asList("홍길동", "고길동", "신길동");
		return list;
	}
}

UserService.java

package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.dao.UserDAO;

public class UserService {

	@Autowired(required = false) // 가장 많이 사용
	UserDAO dao;

	public List<String> list(){
		return dao.list();
	}
}

user.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">

	<bean id="dao" class="com.dao.UserDAO" />
	
	<!-- @ 어노테이션 활성화  -->
	<context:annotation-config />
	
	<!-- 3. @Autowired를 이용한 자동 의존성 주입_Injection -->
	<bean id="service3" class="com.service.UserService" />

</beans>

* 출력 화면 

 

 Spring @Autowired_@Qualifier

MainClass.java

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;

public class MainClass {
	public static void main(String[] args) {

		// IoC Container
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		UserService service3 = ctx.getBean("service3", UserService.class);
		List<String> list3 = service3.list();
		System.out.println(list3);
	}
}

UserDAO.java

package com.dao;
import java.util.Arrays;
import java.util.List;

public class UserDAO {

	// DB 연동 가정
	public List<String> list(){
		List<String> list = Arrays.asList("홍길동", "고길동", "신길동");
		return list;
	}
}

UserService.java

package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.dao.UserDAO;

public class UserService {

	@Autowired // 가장 많이 사용
	@Qualifier("userDAO") // 명시적으로 누구를 쓸 것인지 찍어서 설정하면 에러가 안남 ㅋ
	UserDAO dao;

	public List<String> list(){
		return dao.list();
	}
}

user.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:annotation-config />
	
	<!-- 3. @Autowired를 이용한 자동 의존성 주입_Injection -->
	<bean id="service3" class="com.service.UserService" />
	<bean id="userDAO" class="com.dao.UserDAO" />
	<bean id="userDAO2" class="com.dao.UserDAO" />

</beans>

 

* 출력 화면 

 

 출처

 

@Autowired : 

+ 강의 교재

 

반응형
2022-03-18 07:34:21
반응형

실습 3일차_@Required

 

@Required Annotation은 Spring 2부터 제공되며, 필수 Property를 명시할 때 사용된다. 

필수 Property를 지정하려면 먼저 Property 설정 Method에 @Required Annotation을 붙여야 한다.

 

 Spring @Required

MainClass.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;

public class MainClass {
	public static void main(String[] args) {
		
		ApplicationContext ctx = 
				new GenericXmlApplicationContext("classpath:com/config/user.xml");
		
		UserService service = ctx.getBean("service", UserService.class);
		System.out.printf("service: %s", service);

		UserService service2 = ctx.getBean("service2", UserService.class);
		System.out.printf("service2: %s", service2);
		
	}
}

UserService.java

package com.service;

import org.springframework.beans.factory.annotation.Required;

public class UserService {
	
	String mesg; // null

	// setter 메서드 주입
	@Required // 넣어줘야 에러 안 뜸
	public void setMesg(String mesg) {
		this.mesg = mesg;
	}

	@Override
	public String toString() {
		return "UserService [mesg=" + mesg + "]";
	}
}

user.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:annotation-config />

	<bean id="service" class="com.service.UserService" />
	
	<bean id="service2" class="com.service.UserService">
		<property name="mesg" value="helloworld" />
	</bean>
	
</beans>

 

<!-- @ 어노테이션 활성화 -->
<context:annotation-config />

* context 활성화 방법

[Namespaces 탭] → [context] Check → Configure Namespaces 알림창 OK 버튼 마우스로 클릭 

 

 

 

* 출력 화면 

 

 출처

 

@Required : https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=rex4314&logNo=158814986 

+ 강의 교재

 

반응형
2022-03-18 06:35:25
반응형

실습 3일차_I18N_MessageSource

 

 I18N이란?

 

I18N은 Internationalization의 축약형이다.

Internationalization은 알파벳이 20개로, 가장 첫 글자인 I와 가장 마지막 글자인 N 사이에 알파벳이 18개 있다고 하여

I18N으로 칭한다. 즉, 국제화, Internationalization, I18N 모두 같은 말이다.

 

SW 국제화(I18N)이란?

SW(Software) 국제화는 SW가 특정 지역이나 언어에 종속되지 않고 다양한 지역, 언어에서 정상 동작하도록

국제적으로 통용되는 SW를 설계하고 개발하는 과정을 말한다.

 

 Spring Injection_Callback

MainClass.java

import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.foo.Example;

public class MainClass {
	public static void main(String[] args) {

		ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/user.xml");
		String mesg = ctx.getMessage("greet", null, "Hello~~~", Locale.KOREA);
		System.out.println(mesg);
		
		String mesg2 = ctx.getMessage("greet2", new Object[] {"Kim"}, "Hello~~~", Locale.UK);
		System.out.println(mesg2);
		
		String mesg3 = ctx.getMessage("greet3", new Object[] {"Kim", 20}, "Hello~~~", Locale.UK);
		System.out.println(mesg3);
		
		//Example 빈 참조
		Example exam = ctx.getBean("exam", Example.class);
		exam.printMessage();
		
	}
}

 

Example.java

package com.foo;

import java.util.Locale;
import org.springframework.context.MessageSource;

public class Example {

	MessageSource messageSource;
	
	//setter 메서드 주입
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}
	
	public void printMessage() {
		String mesg = messageSource.getMessage("greet", null, "Hello~~~", Locale.KOREA);
		System.out.println(mesg);
	}
}

 

hello_en.properties

# hello_en.properties
greet=Good Morning
greet2=name:{0}
greet3=name:{0} age:{1}

hello_ko.properties

# hello_ko.properties
greet=안녕하세요.
greet2=이름:{0}
greet3=이름:{0} 나이:{1}

user.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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	
	<!-- hello_xx.properties 에서 hello만 저장한다. -->

	<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
	<!-- id="messageSource" id 값이 정해져 있음 다르게 쓰면 안됨.. -->
		<property name="basenames">
			<list>
			<value>classpath:com/config/hello</value> <!-- 문자열일 경우 value 사용 -->
			</list>
		</property>
		<property name="defaultEncoding" value="utf-8" /> <!-- default encoding: ISO-8859-1. > utf-8로 변경  --> 
	</bean>

	<bean id="exam" class="com.foo.Example">
		<property name="messageSource" ref="messageSource" />
	</bean>

</beans>

* 출력 화면 

 

 출처

 

* I18N : https://miaow-miaow.tistory.com/32

+ 강의 교재

반응형
2022-03-14 08:31:24
반응형

실습 3일차_Injection_Callback

 

https://docs.spring.io/spring-framework/docs/4.3.0.RELEASE/spring-framework-reference/html/beans.html#beans-collection-elements

 

 Spring Injection_Callback

ExampleMain.java

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

public class ExampleMain {

	public static void main(String[] args) {
	
		// IoC Container
		
		//ApplicationContext ctx =
		GenericXmlApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/config/conf.xml");
		
		ctx.close();
	}

}

ExampleBean.java

package examples;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExampleBean {
	
	private static final Logger logger = LoggerFactory.getLogger(ExampleBean.class);

	String mesg; // null 
	
	public ExampleBean() {
		logger.info("ExampleBean 생성자 호출");
	}

	// setter 메서드 주입 ==> xml에서는 <property name = "mesg"
	public void setMesg(String mesg) {
		logger.info("setMesg 메서드 호출");
		this.mesg = mesg;
	}
	
	public String getMesg() {
		return mesg;
	}
	
	// init-method에서 호출
	public void init() {
		logger.info("init 메서드 호출");
	}
	
	// destroy-method에서 호출
	public void cleanup() {
		logger.info("cleanup 메서드 호출");
	}
	
}

conf.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.xsd">
	
	<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init" destroy-method="cleanup">
		<property name="mesg" value="helloworld" />
	</bean>
	
</beans>

* 출력 화면 

 

 Spring Injection_Callback

ExampleMain.java

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

public class ExampleMain {
	public static void main(String[] args) {
	
		// IoC Container
		
		//ApplicationContext ctx =
		GenericXmlApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/config/conf.xml");
		
		ctx.close();
	}
}

ExampleBean.java

package examples;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class ExampleBean implements InitializingBean, DisposableBean{
	
	private static final Logger logger = LoggerFactory.getLogger(ExampleBean.class);

	String mesg; // null 
	
	public ExampleBean() {
		logger.info("ExampleBean 생성자 호출");
	}

	// setter 메서드 주입 ==> xml에서는 <property name = "mesg"
	public void setMesg(String mesg) {
		logger.info("setMesg 메서드 호출");
		this.mesg = mesg;
	}
	
	public String getMesg() {
		return mesg;
	}
	
	// init-method에서 호출
	public void init() {
		logger.info("init 메서드 호출");
	}
	
	// destroy-method에서 호출
	public void cleanup() {
		logger.info("cleanup 메서드 호출");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		logger.info("afterPropertiesSet 메서드 호출");
		
	}

	@Override
	public void destroy() throws Exception {
		logger.info("destroy 메서드 호출");
	}
	
}

conf.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.xsd">
	
	<bean id="exampleInitBean" class="examples.ExampleBean">
		<property name="mesg" value="helloworld" />
	</bean>
	
</beans>

* 출력 화면 

 

 Spring Injection_Callback

ExampleMain.java

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

public class ExampleMain {
	public static void main(String[] args) {
	
		// IoC Container
		
		//ApplicationContext ctx =
		GenericXmlApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/config/conf.xml");
		
		ctx.close();
	}
}

ExampleBean.java

package examples;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class ExampleBean {
	
	private static final Logger logger = LoggerFactory.getLogger(ExampleBean.class);

	String mesg; // null 
	
	public ExampleBean() {
		logger.info("ExampleBean 생성자 호출");
	}

	// setter 메서드 주입 ==> xml에서는 <property name = "mesg"
	public void setMesg(String mesg) {
		logger.info("setMesg 메서드 호출");
		this.mesg = mesg;
	}
	
	public String getMesg() {
		return mesg;
	}
	
	@PostConstruct
    public void populateMovieCache() {
        logger.info("@PostConstruct 메서드 호출");
    }

    @PreDestroy
    public void clearMovieCache() {
    	logger.info("@PreDestroy 메서드 호출");
    }
}

conf.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:annotation-config />
	
	<bean id="exampleInitBean" class="examples.ExampleBean">
		<property name="mesg" value="helloworld" />
	</bean>
	
</beans>

* 출력 화면 

 

 출처

 

+ 강의 교재

반응형