spring - property

2024. 2. 6. 17:55Daily Codig Reminder

property

<bean class="com.spring.EchoBean" id="echoBean">
<property name="mesg" value="홍길동"></property><!-- set 함수 호출 -->
</bean>

<bean class="com.spring.EchoBean" id="echoBean2">
<property name="mesg">
<value>이순신</value>
</property>

 

echoBean

public class EchoBean {
String aaa;


public String sayEcho() {
	return "hello";
}
public EchoBean() {
	System.out.println("반드시 필요ㅘㄴ !!! 기본생ㅇ성자 호출");
}
public String getAaa() {
	return aaa;
}
public void setMesg(String mesg) {
	System.out.println("setMesg(String mesg)호출");
	this.aaa = mesg;
}

}

 

main

GenericXmlApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/spring/echo.xml");
		EchoBean echo = ctx.getBean("echoBean", EchoBean.class);
		System.out.println(echo.getAaa());
		
		echo = ctx.getBean("echoBean2", EchoBean.class);
		System.out.println(echo.getAaa());

 

출력=>

반드시 필요ㅘㄴ !!! 기본생ㅇ성자 호출

setMesg(String mesg)호출

반드시 필요ㅘㄴ !!! 기본생ㅇ성자 호출

setMesg(String mesg)호출

홍길동

이순신

 

<bean class="com.spring.AnotherBean" id="anotherBean">
<constructor-arg name="name" value="홍길동"></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg>
</bean>

<bean class="com.spring.AnotherBean" id="anotherBean2">
<property name="name" value="이순신"></property>
<property name="age" value="10"></property>
</bean>

<bean class="com.spring.EchoBean" id="echoBean">
<property name="xxx" ref="anotherBean"></property>
</bean>
<bean class="com.spring.EchoBean" id="echoBean2">
<property name="xxx" >
<ref bean="anotherBean2"/>
</property>
</bean>

ref 를 따로 뺄 시에는 bean 사용

 

GenericXmlApplicationContext ctx =
				new GenericXmlApplicationContext("classpath:com/spring/echo.xml");
		EchoBean echo = ctx.getBean("echoBean", EchoBean.class);
		System.out.println(echo.getXxx());
		
		echo = ctx.getBean("echoBean2", EchoBean.class);
		System.out.println(echo.getXxx());

기본생성자

기본생성자

AnotherBean [name=홍길동, age=20]

AnotherBean [name=이순신, age=10]

 

<bean class="com.spring.Cat" id ="cat1">
<property name="catName" value ="야옹이"></property>
<property name="catAge" value ="10"></property>
</bean>

<bean class="com.spring.Person" id ="xxx">

<property name="username" value ="홍길동"></property>
<property name="age" value ="20"></property>
<property name="cat" ref="cat1"></property>
<!-- ref id 매개변수 명과 같은 빈을 자동 선택 -->
</bean>


<bean class="com.spring.Cat" id="cat1" p:catName="야옹이" p:catAge="10"></bean>
<bean class="com.spring.Person" id="person" p:username="홍길동" p:age="20" p:cat-ref="cat1"> </bean>

namespace 에 p 추가시

p: 하고 ctrl +space 누르면 자동생성됨.

id 도 자동생성가능 하지만 소문자로 시작하는 class 이름을 가져옴

위아래 같은 용도의 코드

 

<bean class="com.dao.DeptDAO" id ="deptDAO"></bean>
<bean class="com.service.DeptService" id ="deptService">
<property name="dao" ref ="deptDAO"></property>
</bean>

 

dao

package com.dao;

import java.util.Arrays;
import java.util.List;

public class DeptDAO {

	public List<String> getList(){//db코드 구현 예정 
        return Arrays.asList("A", "B","C");
    }

	public DeptDAO() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
}

 

service

package com.service;

import java.util.List;

import com.dao.DeptDAO;

public class DeptService {

	public DeptDAO dao;

	public DeptDAO getDao() {
		return dao;
	}

	public void setDao(DeptDAO dao) {
		this.dao = dao;
	}

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

	public DeptService() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
}

 

 

main

 

public static void main(String[] args) {
		ApplicationContext ctx = new
				GenericXmlApplicationContext("classpath:com/spring/config.xml");
		DeptDAO dao = new DeptDAO();
        DeptService service = new DeptService();
        service.setDao(dao);

        List<String> list=service.getList();

        System.out.println(list);
	}

출력=>

[A, B, C]

 

System.out.println("2.xml에서 빈 생성 , 생성 빈 사용 방법=========");
		ApplicationContext ctx = new
				GenericXmlApplicationContext("classpath:com/spring/person.xml");
		Person xxx = ctx.getBean("person", Person.class);
		System.out.println(xxx);
		System.out.println("=============");

		Cat c = ctx.getBean("cat1", Cat.class);
		System.out.println(c);
		System.out.println("===============");
		Dog d = ctx.getBean("dog1", Dog.class);
		System.out.println(d);

 

list

bean

<bean id="echoBean" class="com.spring.EchoBean">
<constructor-arg name="valueList">
<!-- list 주입 -->
<list >
<value>10</value>
<value>20</value>
<value>30</value>
</list>
</constructor-arg>
</bean>


<bean id="echoBean2" class="com.spring.EchoBean">
<property name="valueList">
<list >
<value>10</value>
<value>20</value>
<value>30</value>
</list>
</property>
</bean>

 

 

package com.spring;

import java.util.List;

public class EchoBean {
List valueList; //List<Object>

public EchoBean() {
	super();
	System.out.println("=================");
}
public String sayEcho() {
	return "Hello";
}
public List getValueList() {
	return valueList;
}

public void setValueList(List valueList) {
	this.valueList = valueList;
}

public EchoBean(List valueList) {
	super();
	this.valueList = valueList;
}

 

 

main

import java.util.List;

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

import com.spring.EchoBean;	
public class Main {

	public static void main(String[] args) {
		System.out.println("2.xml에서 빈 생성 , 생성 빈 사용 방법=========");
		ApplicationContext ctx = new
				GenericXmlApplicationContext("classpath:com/spring/echo.xml");
		EchoBean e = ctx.getBean("echoBean2", EchoBean.class);
		System.out.println(e.getValueList());
		List list = e.getValueList();
		for (Object object : list) {
			System.out.println(object);
		}

	}

}

출력 내용=>

2.xml에서 빈 생성 , 생성 빈 사용 방법=========

[10, 20, 30]

10

20

30

 

main

import java.util.List;

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

import com.spring.AnotherBean;
import com.spring.EchoBean;


public class Main_0 {

	public static void main(String[] args) {
		ApplicationContext ctx = new
				GenericXmlApplicationContext("classpath:com/spring/echo2.xml");
		EchoBean echo = ctx.getBean("echoBean", EchoBean.class);

		List<AnotherBean> list = echo.getValueList();
		for (AnotherBean a : list) {
			System.out.println(a);
		}
	}
}

 

bean2

<bean class="com.spring.AnotherBean" id ="anotherBean1">
<constructor-arg name="name" value="test1"></constructor-arg>
</bean>
<bean class="com.spring.AnotherBean" id ="anotherBean2">
<constructor-arg name="name" value="test2"></constructor-arg>
</bean>
<bean class="com.spring.AnotherBean" id ="anotherBean3">
<constructor-arg name="name" value="test3"></constructor-arg>
</bean>
<bean class="com.spring.EchoBean" id="echoBean">
<constructor-arg name="anotherBean" ref="anotherBean1"></constructor-arg>
<property name="valueList">
<list>
<ref bean="anotherBean1"/>
<ref bean="anotherBean2"/>
<ref bean="anotherBean3"/>
</list>
</property>
</bean>

 

bean1

<!--  기본생성자 사용 필수  -->
<!--  nameSpace 에 util 추가 -->
<bean class="com.spring.AnotherBean" id ="anotherBean1">
<constructor-arg name="name" value="test1"></constructor-arg>
</bean>
<bean class="com.spring.AnotherBean" id ="anotherBean2">
<constructor-arg name="name" value="test2"></constructor-arg>
</bean>
<bean class="com.spring.AnotherBean" id ="anotherBean3">
<constructor-arg name="name" value="test3"></constructor-arg>
</bean>

<!--  따로 떼서 재사용 가능 -->
<util:list id="list">
<ref bean="anotherBean1"/>
<ref bean="anotherBean2"/>
<ref bean="anotherBean3"/>
 </util:list>
<!-- 

List list = new ArrayList();
list.add(anotherBean1);
list.add(anotherBean2);
list.add(anotherBean3);

 -->

<bean class="com.spring.EchoBean" id="echoBean1">
<property name="valueList" ref="list"></property>
<property name="anotherBean" ref="anotherBean1"></property>
</bean>

<bean class="com.spring.EchoBean" id="echoBean2">
<property name="valueList" ref="list"></property>
<property name="anotherBean" ref="anotherBean1"></property>
</bean>

 

echoBean

public class EchoBean {
List<AnotherBean> valueList;//set 함수 이용
AnotherBean anotherBean; //생성자 이용하여 주입

'Daily Codig Reminder' 카테고리의 다른 글

@Autowired, CoC ,@Qualifier ,lifecycle  (1) 2024.02.08
props, autowired  (1) 2024.02.07
스프링 준비  (0) 2024.02.05
cors  (0) 2024.01.15
json , ajax  (1) 2024.01.15