2024. 2. 5. 18:24ㆍ카테고리 없음
bean 경로
<!-- root 로거 기본 설정 -->
<root level="debug"><!-- 로그 기록 레벨 : trace-debug-info- warn-error -->
<appender-ref ref="STDOUT" />
<appender-ref ref="DAILYOUT" />
</root>
error로 설정시 깔끔하게 나옴
ApplicationContext ctx = new GenericXmlApplicationContext("file:\\c:\\upload\\person.xml");
ApplicationContext ctx = new
GenericXmlApplicationContext("classpath:stu.xml");
Student s1 = ctx.getBean("xxx", Student.class);
System.out.println(s1);
Student s2 = (Student)ctx.getBean("xxx");
s2.setAge(20);
s2.setName("이순신");
System.out.println(s2.toString());
Student [name=홍길동, age=10]
Student [name=이순신, age=20]
ApplicationContext ctx = new
GenericXmlApplicationContext("com/kkk/*.xml");
Person p1 = ctx.getBean("xxx", Person.class);
System.out.println(p1);
System.out.println(p1.getInfo());
System.out.println("==============");
ApplicationContext ctx1 = new
GenericXmlApplicationContext("com/kkk/animal.xml");
Animal a =
(Animal)ctx1.getBean("yyy");
System.out.println(a);
System.out.println(a.getName());
→ console
2.xml에서 빈 생성 , 생성 빈 사용 방법=========
Animal 생성자 호출됨 기본생서아 호출
com.spring.Person@57db2b13 이순신
Animal 생성자 호출됨 기본생서아 호출
com.spring.Animal@2254127a 고양이
com/kkk/*.xml→ animal.xml에서 xxx yyy 둘다 받음
<bean class="com.spring.Animal" id="yyy"></bean>
<bean class="com.spring.Person" id="xxx"></bean>
import /bean 경로
<!-- 다른 configuration.xml 파일을 import 해서 사용 -->
<import resource ="animal.xml"/>
결과 동일 하게 나옴
System.out.println("2.xml에서 빈 생성 , 생성 빈 사용 방법=========");
ApplicationContext ctx = new
GenericXmlApplicationContext("classpath:com/kkk/person.xml");
Person p1 = ctx.getBean("xxx", Person.class);
System.out.println(p1);
System.out.println(p1.getInfo());
System.out.println("==============");
Animal a =
(Animal)ctx.getBean("yyy");
System.out.println(a);
System.out.println(a.getName());
xml 을 ,로 동시에 불러오다 ↓
ApplicationContext ctx = new
GenericXmlApplicationContext("classpath:com/kkk/animal.xml","classpath:com/kkk/person.xml");
multi
Servlet dao 패턴 이용 db
Servlet ⇒ service ⇒ dao → db class Service {
dao dao;
public Service(){
Public Service (Dao dao){ //생성자
this.dao =dao;
}
public void select(){
dao.select();
}
}
///////////////////////
Dao dao = new Dao; //빈생성
Service service = new Service(dao);// 주입
//constructor injection
Servlet 은 Service.select();
bean =⇒
<bean class="com.spring.Person" id ="aaa"></bean>
<!-- 기본생성자 사용 필수 -->
<bean class="com.spring.Person" id ="kkk">
<constructor-arg name="x" value ="롱길동"></constructor-arg>
System.out.println("2.xml에서 빈 생성 , 생성 빈 사용 방법=========");
ApplicationContext ctx = new
GenericXmlApplicationContext("classpath:com/spring/person.xml");
Person p1 = ctx.getBean("kkk", Person.class);
System.out.println(p1);
System.out.println(p1.getInfo());
System.out.println("==============");
Person p2 = ctx.getBean("aaa", Person.class);
System.out.println(p2);
System.out.println(p2.getInfo());
person.java
ublic class Person {
String name;
public Person() {
super();
System.out.println("person 기본생성자");
}
public Person(String x) {
super();
System.out.println("person애개변수");
this.name=x;
}
public String getInfo() {
return this.name;
}
}
★int 타입도 "" 붙여줘야함 자동으로 형변환해줌★
package com.spring;
public class Person {
String name;
// public Person() {
// super();
// System.out.println("person 기본생성자");
// }
public Person(String x) {
super();
System.out.println("person애개변수"+ x);
}
public Person(String x, String y) {
super();
System.out.println("person애개변수 x,y "+x+"\t"+y);
}
public Person(String xx, int yy) {
super();
System.out.println("person애개변수 xx,yy "+xx+"\t"+yy);
}
public String getInfo() {
return "홍길동";
}
}
bean
<bean class="com.spring.Person" id ="xxx">
<constructor-arg name="x" value ="홍길동"></constructor-arg>
</bean>
<bean class="com.spring.Person" id ="xxx2">
<constructor-arg name="x" value ="유관순"></constructor-arg>
<constructor-arg name="y" value ="제주"></constructor-arg>
</bean>
<bean class="com.spring.Person" id ="xxx3">
<constructor-arg name="xx" value ="유관순"></constructor-arg>
<constructor-arg name="yy" value ="10"></constructor-arg>
<!-- int 타입도 "" 붙여줘야함 자동으로 형변환해줌 -->
</bean>
</beans>
main
System.out.println("2.xml에서 빈 생성 , 생성 빈 사용 방법=========");
ApplicationContext ctx = new
GenericXmlApplicationContext("classpath:com/spring/person.xml");
Person p1 = ctx.getBean("xxx", Person.class);
System.out.println(p1);
System.out.println(p1.getInfo());
System.out.println("==============");
Person p2 = ctx.getBean("xxx2", Person.class);
System.out.println(p2);
System.out.println(p2.getInfo());
System.out.println("==============");
Person p3 = ctx.getBean("xxx3", Person.class);
System.out.println(p3);
System.out.println(p3.getInfo());
person애개변수홍길동
person애개변수 x,y 유관순 제주
person애개변수 xx,yy 유관순 10
com.spring.Person@4e0ae11f 홍길동
com.spring.Person@238d68ff 홍길동
com.spring.Person@4b86805d 홍길동
bean 을 만들어 ref 방식으로 bean안에 넣음
bean
<bean class="com.spring.Cat" id ="pet01">
<constructor-arg name="catName" value ="야옹이"></constructor-arg>
<constructor-arg name="catAge" value ="20"></constructor-arg>
</bean>
<bean class="com.spring.Person" id ="xxx">
<constructor-arg name="username" value ="홍길동"></constructor-arg>
<constructor-arg name="age" value ="10"></constructor-arg>
<constructor-arg name="cat" ref ="pet01"></constructor-arg>
</bean>
//방법1
Person person = new Person("홍길동",10,new Cat("야옹이",20));
//방법 2
Cat c = new Cat("야옹이", 20);
Person p = new Person("홍길동", 10, c);
System.out.println(p.getUsername());
System.out.println(p.getAge());
System.out.println(p.getCat());
방법2 방식으로
ApplicationContext ctx = new
GenericXmlApplicationContext("classpath:com/spring/person.xml");
Person xxx = ctx.getBean("xxx", Person.class);
System.out.println(xxx);
System.out.println("=============");
Cat yyy = xxx.getCat();
System.out.println(yyy);
System.out.println("==============");
Cat c = ctx.getBean("pet01", Cat.class);
System.out.println(c);
<bean class="com.spring.Cat" id ="pet01">
<constructor-arg name="catName" value ="야옹이"></constructor-arg>
<constructor-arg name="catAge" value ="20"></constructor-arg>
</bean>
<bean class="com.spring.Cat" id ="pet02">
<constructor-arg name="catName" value ="누렁이"></constructor-arg>
<constructor-arg name="catAge" value ="10"></constructor-arg>
</bean>
<bean class="com.spring.Person" id ="xxx">
<constructor-arg name="username" value ="홍길동"></constructor-arg>
<constructor-arg name="age" value ="10"></constructor-arg>
<constructor-arg name="cat" ref ="pet02"></constructor-arg>
</bean>
ApplicationContext ctx = new
GenericXmlApplicationContext("classpath:com/spring/person.xml");
Person xxx = ctx.getBean("xxx", Person.class);
System.out.println(xxx);
System.out.println("=============");
Cat yyy = xxx.getCat();
System.out.println(yyy);
System.out.println("==============");
Cat c = ctx.getBean("pet01", Cat.class);
System.out.println(c);
출력내용
2.xml에서 빈 생성 , 생성 빈 사용 방법=========
생성자 호출야옹이 20
생성자 호출누렁이 10
생성자호출홍길동 10
Cat [catName=누렁이, catAge=10]
Person [username=홍길동, age=10, cat=Cat [catName=누렁이, catAge=10]]
Cat [catName=누렁이, catAge=10]
Cat [catName=야옹이, catAge=20]
생성자 추가되면 그걸로 toString 재생성 필수 field 도
package com.spring;
public class Person {
String username;
int age;
Cat cat;
Dog dog;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "Person [username=" + username + ", age=" + age + ", cat=" + cat + ", dog=" + dog + "]";
}
public Person(String username, int age, Cat cat) {
super();
System.out.println("생성자호출"+username+"\t"+age+"\t"+cat);
this.username = username;
this.age = age;
this.cat = cat;
}
public Person(String username, int age) {
super();
System.out.println("생성자호출"+username+"\t"+age);
this.username = username;
this.age = age;
}
public Person(String username, int age, Cat cat, Dog dog) {
super();
this.username = username;
this.age = age;
this.cat = cat;
this.dog = dog;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
dao
기존방식
DeptService// Dao new 코드 없음
DeptDao 를 생성자를 통해 주입 받도록 구현 Main_0.java …
service.getList 호출 데이터 출력
dao
public class DeptDAO {
public List<String> getList(){
return Arrays.asList("A","B","C");
}
}
service
public class DeptService { // new 없음
public DeptDAO dao;
public DeptService(DeptDAO dao) { /////
super();
this.dao = dao;
}
public DeptService() {
super();
// TODO Auto-generated constructor stub
}
public List<String>getList(){
return dao.getList();
}
public DeptDAO getDao() {
return dao;
}
public void setDao(DeptDAO dao) {/////
this.dao = dao;
}
}
bean
<bean class="com.dao.DeptDAO" id ="deptDAO"></bean>
<bean class="com.service.DeptService" id ="deptService">
<constructor-arg name="dao" ref ="deptDAO"></constructor-arg>
</bean>
bean 방법
ApplicationContext ctx = new
GenericXmlApplicationContext("classpath:com/spring/config.xml");
DeptService service = ctx.getBean("deptService", DeptService.class);
List<String> list = service.getList();
System.out.println(list);
기존방법
DeptDAO dao = new DeptDAO();////////
//DeptService service = new DeptService(dao);////생성자를 통한 주입
DeptService service = new DeptService();
service.setDao(dao);
List<String> list = service.getList();
System.out.println(list);