스프링 프레임 워크에서 작업을 시작하고 있습니다. 콩 개념을 머리에 쓰려고합니다. 빈 값이있는 경우스프링 빈 값 주입
public class CurrentDateServiceImpl implements CurrentDateService {
public LocalDate getCurrentDate() {
return LocalDate.now() ;
}
내가 달성하기 위해 노력하고있어 주장하는 간단한 @Test가 : 현재 날짜를 얻을
<?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">
<bean id= "currentDateService" class ="xx.CurrentDateSerivceimpl" />
</beans>
그리고 클래스 : 나는 xml 파일이 현재 공급되는 날짜와 동일합니다. 나는 시험에 빈의 값을 제공 할 수있는 방법을 모르고있어
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
CurrentDateServiceImpl currentDateServiceObj = (CurrentDateServiceImpl) context.getBean("currentDateService");
LocalDate date = LocalDate.now();
LocalDate date2 = "the value of the bean";
assertEquals(date, date2);
}
, 나는 그것을 수행하는 방법을 궁금하고이 있다면 :
내가에 붙어 것은 스프링 문서 자체를 제외하고 어떤 좋은 자습서/문서화
편집 :
package lt.insoft.app.bl.service.impl;
import static org.junit.Assert.assertEquals;
import java.time.LocalDate;
import lt.insoft.app.bl.service.CurrentDateService;
import lt.insoft.app.bl.service.CurrentDateServiceFormat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/resources/META-INF/application-context.xml" })
public class CurrentDateServiceImplTest {
@Autowired
CurrentDateService service;
CurrentDateServiceFormat service2;
@Test
public void test() {
LocalDate date = LocalDate.now();
LocalDate date2 = service.getCurrentDate();
String date3 = service2.formatCurrentDate();
System.out.println(date3);
assertEquals(date, date2);
}
}
왜이 포맷 된 날짜를 인쇄되지 않는 이유는 무엇입니까?
단순히 전화'currentDateServiceObj.getCurrentDate을()' – Jens
손으로 모든 작업을 수행 할 필요가 없습니다. 새로운 코드의 경우 일반적으로 XML을 통한 JavaConfig를 권장하고 빈 조회에 대한 주입을 권장합니다. – chrylis