반응형
package org.doit.ik;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home"; //web-inf/view/home.jsp
}
}
package org.doit.ik.di;
public interface Record {
//1. 총점을 구해서 반환하는 메서드 추상메서드 2개
int total();
//2. 평균을 구해서 반환하는 메서드
double avg();
}
package org.doit.ik.di;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class RecordImpl implements Record{
//성적처리하는 객체
private int kor;
private int eng;
private int mat;
@Override
public int total() {
return kor + eng + mat;
}
@Override
public double avg() {
return total() / 3.0;
}
}
package org.doit.ik.di;
public interface RecordView {
//성적 입력 메서드
void input();
//성적 출력 메서드
void output();
}
package org.doit.ik.di;
import java.util.Scanner;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class RecordViewImpl implements RecordView{
/* private RecordImpl record = new RecordImpl(); */
private RecordImpl record = null;
@Override
public void input() {
try(Scanner scanner = new Scanner(System.in)){
System.out.printf(">kor,eng,mat input?");
int kor = scanner.nextInt();
int eng = scanner.nextInt();
int mat = scanner.nextInt();
this.record.setKor(kor);
this.record.setEng(eng);
this.record.setMat(mat);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void output() {
// TODO Auto-generated method stub
System.out.printf("kor =%d, eng =%d, mat=%d,tot=%d, avg=%2f\n",
this.record.getKor(),
this.record.getEng(),
this.record.getMat(),
this.record.total(),
this.record.avg()
);
}
}
package org.doit.ik.di.test;
import org.doit.ik.di.Record;
import org.doit.ik.di.RecordImpl;
import org.doit.ik.di.RecordViewImpl;
public class Ex01 {
public static void main(String[] args) {
//org/doik.di 패키지
//Record.java 인터페이스
//RecordImpl.java
//RecordView.java
//RecordViewImpl.java
RecordImpl record = new RecordImpl();
//setter DI
RecordViewImpl rvi= new RecordViewImpl();
// 생성자 DI
//RecordViewImpl rvi = new RecordViewImpl(record);
rvi.setRecord(record);
rvi.input(); //성적입력
rvi.output();//성적출략
System.out.println("END");
}
}
package org.doit.ik.di.test;
import org.doit.ik.di.RecordViewImpl;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Ex02 {
public static void main(String[] args) {
/*
* 1.스프링 객체 생성+조립-> 스프링 컨테이너
* 2. 2가지 방법
* xml 파일
* src/main/resources/application-context.xml 추가
* java 파일
*
* */
String resouceLocations ="application-context.xml";
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(resouceLocations);
//공장에서 가져와야함
RecordViewImpl rvi =(RecordViewImpl) ctx.getBean("rvi");
rvi.input(); //성적입력
rvi.output();//성적출략
System.out.println("END");
}//main
}
package org.doit.ik.di2;
import org.doit.ik.di.RecordImpl;
import org.doit.ik.di.RecordViewImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//application-context.xml // 자바클래스 설정 파일
@Configuration
public class Config {
@Bean
public RecordImpl record() {
return new RecordImpl();
}
/*
* <bean id="rvi" class="org.doit.ik.di.RecordViewImpl"> <constructor-arg
* ref="record"></constructor-arg>
*
* <!-- <property name="record" ref="record"></property> --> <!-- setter
* <property name="record" > <ref bean ="record"/>
*
*
* </property> -->
*
* <!-- <property name="setrecord" ref="개체record"></property> -->
*
* </bean>
*/
//유지보수가 필요함
@Bean(name = "rvi")
public RecordViewImpl getRecordViewImpl() {
RecordViewImpl rvi = new RecordViewImpl();
rvi.setRecord(record());
return rvi;
}
}
package org.doit.ik.di2.test;
import org.doit.ik.di.RecordViewImpl;
import org.doit.ik.di2.Config;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Ex03 {
public static void main(String[] args) {
/*
* 1.스프링 객체 생성+조립-> 스프링 컨테이너
* 2. 2가지 방법
* xml 파일
* src/main/resources/application-context.xml 추가
* java 파일
*
* */
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
Config.class);
//공장에서 가져와야함
//RecordViewImpl rvi =(RecordViewImpl) ctx.getBean("getRecordViewImpl");
RecordViewImpl rvi =(RecordViewImpl) ctx.getBean("rvi");//Bean name=
rvi.input(); //성적입력
rvi.output();//성적출략
System.out.println("END");
}//main
}
package org.doit.ik.di2.test;
import org.doit.ik.di.RecordViewImpl;
import org.doit.ik.di2.Config;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Ex03 {
public static void main(String[] args) {
/*
* 1.스프링 객체 생성+조립-> 스프링 컨테이너
* 2. 2가지 방법
* xml 파일
* src/main/resources/application-context.xml 추가
* java 파일
*
* */
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
Config.class);
//공장에서 가져와야함
//RecordViewImpl rvi =(RecordViewImpl) ctx.getBean("getRecordViewImpl");
RecordViewImpl rvi =(RecordViewImpl) ctx.getBean("rvi");//Bean name=
rvi.input(); //성적입력
rvi.output();//성적출략
System.out.println("END");
}//main
}
package org.doit.ik.di3;
public class DD {
public static void main(String[] args) {
}
}
package org.doit.ik.di3;
public interface Record3 {
int total();
double avg();
}
package org.doit.ik.di3;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class RecordImpl3 implements Record3{
//성적처리하는 객체
private int kor;
private int eng;
private int mat;
@Override
public int total() {
return kor + eng + mat;
}
@Override
public double avg() {
return total() / 3.0;
}
}
package org.doit.ik.di3;
public interface RecordView3 {
void input();
void output();
}
package org.doit.ik.di3;
import java.util.Scanner;
import javax.inject.Inject;
import javax.inject.Named;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class RecordViewImpl3 implements RecordView3{
//@Setter()
/* @Autowired(required=false) */
//@Resource(name="record1") java 9부터는 사용 x
@Inject
@Named(value ="record2")
private RecordImpl3 record = null;
//여러개여도 오류 없어도 오류...
@Override
public void input() {
try(Scanner scanner = new Scanner(System.in)){
System.out.printf(">kor,eng,mat input?");
int kor = scanner.nextInt();
int eng = scanner.nextInt();
int mat = scanner.nextInt();
this.record.setKor(kor);
this.record.setEng(eng);
this.record.setMat(mat);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void output() {
// TODO Auto-generated method stub
System.out.printf("kor =%d, eng =%d, mat=%d,tot=%d, avg=%2f\n",
this.record.getKor(),
this.record.getEng(),
this.record.getMat(),
this.record.total(),
this.record.avg()
);
}
}
package org.doit.ik.di3.test;
import org.doit.ik.di.RecordViewImpl;
import org.doit.ik.di3.RecordViewImpl3;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Ex04 {
public static void main(String[] args) {
String resouceLocations ="application-context3.xml";
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(resouceLocations);
RecordViewImpl3 rvi =(RecordViewImpl3) ctx.getBean("rvi");
rvi.input();
rvi.output();
System.out.println("END");
}//main
}
package org.doit.ik.di4;
public interface Record4 {
int total();
double avg();
}
package org.doit.ik.di4;
import org.springframework.stereotype.Component;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Component("record2")
public class RecordImpl4 implements Record4{
//성적처리하는 객체
private int kor;
private int eng;
private int mat;
@Override
public int total() {
return kor + eng + mat;
}
@Override
public double avg() {
return total() / 3.0;
}
}
package org.doit.ik.di4;
public interface RecordView4 {
void input();
void output();
}
package org.doit.ik.di4;
import java.util.Scanner;
import javax.inject.Inject;
import javax.inject.Named;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Component
//컴포넌트에 이름을 부여하지 않으면 클래스의 첫글자로 명명된다
public class RecordViewImpl4 implements RecordView4{
//@Setter()
@Autowired(required=false)
//@Resource(name="record1") java 9부터는 사용 x
// @Inject
//@Named(value ="record4")
private RecordImpl4 record = null;
//여러개여도 오류 없어도 오류...
@Override
public void input() {
try(Scanner scanner = new Scanner(System.in)){
System.out.printf(">kor,eng,mat input?");
int kor = scanner.nextInt();
int eng = scanner.nextInt();
int mat = scanner.nextInt();
this.record.setKor(kor);
this.record.setEng(eng);
this.record.setMat(mat);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void output() {
System.out.printf("kor =%d, eng =%d, mat=%d,tot=%d, avg=%2f\n",
this.record.getKor(),
this.record.getEng(),
this.record.getMat(),
this.record.total(),
this.record.avg()
);
}
}
package org.doit.ik.di4.test;
import org.doit.ik.di4.RecordViewImpl4;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Ex05 {
public static void main(String[] args) {
String resouceLocations ="application-context4.xml";
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(resouceLocations);
RecordViewImpl4 rvi =(RecordViewImpl4) ctx.getBean("recordViewImpl4");
rvi.input();
rvi.output();
System.out.println("END");
}//main
}
package org.doit.ik.mapper;
public interface SampleMapper {
String getTime();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.doit.ik.mapper.SampleMapper">
<select id="getTime" resultType="string">
SELECT sysdate
FROM dual
</select>
</mapper>
<?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">
<!-- p104 @Autowired 또는 @Resource 어노테이션을 사용하려면 추가 -->
<context:annotation-config/> <!-- 오류는 왜?? namespace에서 체크/.. -->
<bean id="record" class="org.doit.ik.di3.RecordImpl3" />
<bean id="record2" class="org.doit.ik.di3.RecordViewImpl3"/>
<bean id="rvi" class="org.doit.ik.di3.RecordViewImpl3"/>
<!-- new일때는 찾기 편함 주입 ->class 분석 -->
</beans>
<?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">
<!-- RecordImpl record = new RecordImpl(); -->
<bean id="record" class="org.doit.ik.di.RecordImpl" />
<!-- bean's class, except if it serves only as a parent definition for child
패키지명까지 줘야함 bean definitions. rvi.setRecord -->
<bean id="rvi" class="org.doit.ik.di.RecordViewImpl">
<constructor-arg ref="record"></constructor-arg>
<!-- <property name="record" ref="record"></property> -->
<!-- setter
<property name="record" >
<ref bean ="record"/>
</property> -->
<!-- <property name="setrecord" ref="개체record"></property> -->
</bean>
</beans>
<?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">
<!-- p104 @Autowired 또는 @Resource 어노테이션을 사용하려면 추가 -->
<context:annotation-config/> <!-- 오류는 왜?? namespace에서 체크/.. -->
<bean id="record" class="org.doit.ik.di3.RecordImpl3" />
<bean id="record2" class="org.doit.ik.di3.RecordViewImpl3"/>
<bean id="rvi" class="org.doit.ik.di3.RecordViewImpl3"/>
<!-- new일때는 찾기 편함 주입 ->class 분석 -->
</beans>
<?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">
<!-- p104 @Autowired 또는 @Resource 어노테이션을 사용하려면 추가 -->
<context:annotation-config/> <!-- 오류는 왜?? namespace에서 체크/.. -->
<bean id="record" class="org.doit.ik.di3.RecordImpl3" />
<bean id="record2" class="org.doit.ik.di3.RecordViewImpl3"/>
<bean id="rvi" class="org.doit.ik.di3.RecordViewImpl3"/>
<!-- new일때는 찾기 편함 주입 ->class 분석 -->
</beans>
스프링 di 와 test 코드들
'취준 note 2023 > spring' 카테고리의 다른 글
스프링 트랜젝션+pagenation (0) | 2023.08.12 |
---|---|
스프링AOP (0) | 2023.08.12 |
스프링 시큐리티 (0) | 2023.08.11 |
spring master - chap2 스프링빈 의존성 주입(IOC) (0) | 2023.01.23 |
코드없이 보는 스프링 부트 -페이징 정렬 처리하기 (0) | 2023.01.21 |