Spring
toString
(์๋ฐ์ ๊ฐ์) ๊ฐ์ฒด์งํฅ ์ธ์ด๋ค์ ๊ฐ์ฒด์ toString์ด๋ผ๋ ๋ฉ์๋๋ฅผ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ค.
์ผ๋ฐ์ ์ผ๋ก toString์ ๊ทธ ๊ฐ์ฒด๋ฅผ ์ค๋ช ํด์ฃผ๋ ๋ฌธ์์ด์ ๋ฆฌํดํ๋ค.
๊ทธ๋ฆฌ๊ณ ๊ฐ์ฒด์ toString์ ๋ฎ์ด์ฐ๊ธฐ(overriding)ํ๋ฉด ๋ค๋ฅธ ํ์์ ๋ฌธ์์ด์ ๋ฆฌํดํ ์ ์๋ค.
๋ฌธ์์ด์ด ๊ธฐ๋๋๋ ๊ณณ์์ ๋ฌธ์์ด์ด ์๋ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด ์์คํ ์ ์์์ ์ผ๋ก toString์ ํธ์ถํ๋ค.
โญ์์ java ์ฝ๋ ์ ์์ฑ
public class Program {
public static void main(String[] args) {
List<Exam> exams = new ArrayList<>();
exams.add(new NewlecExam(1,1,1,1));
for(Exam e : exams)
System.out.println(e);
}
}
>>> NewlecExam [kor=1, eng=1, math=1, com=1]
โญSpring์ ์ด์ฉํ ์ฝ๋ ์ ์์ฑ๊ณผ DI
โจ์์ฑ์๋ฅผ ์ด์ฉํ ์ฝ๋ ์ ์์ฑ
(setting.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="exam" class="spring.di.entity.NewlecExam" p:kor="10" />
<bean id="exams" class="java.util.ArrayList">
<constructor-arg>
<list>
<bean class="spring.di.entity.NewlecExam" p:kor="20" />
<ref bean="exam"/>
</list>
</constructor-arg>
</bean>
</beans>
<bean>
๋ฅผ ์ด์ฉํด exams๋ผ๋ ์ฝ๋ ์
์ ์์ฑํ๋ค.
์ฝ๋ ์
์ ๊ฐ์ ์ถ๊ฐํ๋ add()
์ฐ์ฐ์ <property>
๋ก ํ ์ ์๋ค.
๋ฐ๋ผ์ ์์ฑ์์ ๊ฐ์ setting ํด์ฃผ๋ ๋ฐฉ์์ผ๋ก di๋ฅผ ํด์ผํ๋ค.<list>
ํ๊ทธ ์์ diํ๊ณ ์ ํ๋ ๊ฐ์ฒด๋ฅผ ๋ฃ์ ์ ์๋ค.
์ด๋ <bean>
์ ์ด์ฉํ์ฌ ์ง์ ์์ฑํ ์ ๋ ์๊ณ , <ref>
๋ฅผ ์ด์ฉํ์ฌ ์์์ ๋ง๋ค์๋ ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ฌ ์ ๋ ์๋ค.
(Program.java)
public class Program {
public static void main(String[] args) {
ApplicationContext context
= new ClassPathXmlApplicationContext("spring/di/setting.xml");
List<Exam> exams = (List<Exam>) context.getBean("exams");
for(Exam e : exams)
System.out.println(e);
}
}
์คํ๊ฒฐ๊ณผ
NewlecExam [kor=1, eng=1, math=0, com=0]
NewlecExam [kor=10, eng=0, math=0, com=0]
โจ util์ ์ด์ฉํ ์ฝ๋ ์ ์์ฑ
(setting.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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<bean id="exam" class="spring.di.entity.NewlecExam" p:kor="10" />
<util:list id="exams" list-class="java.util.ArrayList">
<bean class="spring.di.entity.NewlecExam" p:kor="1" p:eng="1" />
<ref bean="exam"/>
</util:list>
</beans>
namespace์์ util์ ์ ํํ์ฌ ์ถ๊ฐ<list>
ํ๊ทธ ์์ util์ ์ถ๊ฐํ์ฌ <util:list>
๋ก ๋ฐ๊พธ๋ฉด ์ฒ๋ฆฌ๊ธฐ๊ฐ util๋ก ๋ฐ๋๋ฉฐ ์ง์ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ์ญํ ์ ํ ์ ์๋ค.
(<list>
๋ ๋ชฉ๋ก์ ์
ํ
ํ๋๋ฐ ์ฌ์ฉํ๋ ํ๊ทธ์ด๋ฉฐ, ์ง์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ง๋ ๋ชปํจ)
๊ทธ๋ฆฌ๊ณ list-class๋ฅผ ์ด์ฉํ์ฌ ์ฌ์ฉํ list๋ฅผ ๊ตฌ์ฒดํ ์์ผ์ผ ํ๋ค.
'๐๋ฐฑ์๋ : BackEnd' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring]@Autowired๋์๋ฐฉ์๊ณผ @Qualifier (0) | 2023.06.19 |
---|---|
[Spring]์ด๋ ธํ ์ด์ ์ ์ด์ฉํ ๋ ์ฅ์ ๊ณผ @Autowired๋ฅผ ์ด์ฉํ DI (0) | 2023.06.19 |
[Spring]DI ์ง์์ (0) | 2023.06.19 |
[Spring] ์์ (0) | 2023.06.19 |
[Spring] DI์ IoC Container (0) | 2023.06.19 |