Spring 속성 주입 set 방법 주입 기본 데이터 형식

1804 단어 J2EE
Spring 은 set 방법 으로 속성 주입 을 실현 할 수 있 습 니 다.
1. 속성 주입 이 필요 한 클래스
package com.maty.property; 
/** 
* @author maty  e-mail:[email protected]
* @version     :2018 5 16    3:04:58 
*                 
*/
public class User
{
	private String name; //       

	//      set  
	public void setName(String name)
	{
		this.name = name;
	}
	
	//               
	public void UserTest()
	{
		System.out.println("user name  ="+name);
	}
	
}

2. ApplicationContext. xml 의 작성

	
		
	
//       : User     Spring    ,           property    User       

3. 주입 이 성 공 했 는 지 테스트 클래스 를 작성 합 니 다.
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.maty.property.PropertyConstructor;
import com.maty.property.User;

/** 
* @author maty  e-mail:[email protected]
* @version     :2018 5 16    12:23:01 
*     
*/
public class MyTest
{
	public static void main(String[] args)
	{		
		//   :  xml  
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		User bean = (User) context.getBean("user");
		bean.UserTest();
	}
	
}

4. 실행 결과
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
user name  =maty

5. 총화
속성 주입 기능 은 주로 bean 태그 의 property 하위 태그 에 있 으 며, 이 하위 태그 작업 의 전 제 는 속성 주입 이 필요 한 클래스 가 이 속성의 set 방법 을 제공 하 는 것 입 니 다.주입 대상 이 라면 다음 편 을 참고 하 세 요.

좋은 웹페이지 즐겨찾기