[英]Spring beans value injection
我開始使用Spring框架,並嘗試將bean的概念包起來。 我有一個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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id= "currentDateService" class ="xx.CurrentDateSerivceimpl" />
</beans>
和一個獲取當前日期的類:
public class CurrentDateServiceImpl implements CurrentDateService {
public LocalDate getCurrentDate() {
return LocalDate.now() ;
}
我要完成的是一個簡單的@test,以斷言bean值是否與我提供的當前日期相同。
我堅持的是:
@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);
}
我不知道如何將Bean的價值提供給測試,我想知道如何實現它,以及是否有除了spring docs本身以外的任何好的教程/文檔。
編輯:
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);
}
}
為什么這不打印格式化的日期?
使用彈簧支架進行測試。 代碼將更加簡潔易懂。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"application-context.xml"})
public class TestClass{
@Autowired
CurrentDateService service;
@Test
public void test() {
LocalDate date = LocalDate.now();
LocalDate date2 = service.getCurrentDate();
assertEquals(date, date2);
}
}
我認為您想執行以下操作:
public class CurrentDateServiceImpl implements CurrentDateService {
public LocalDate getCurrentDate() {
return LocalDate.now() ;
}
}
public class CurrentDateServiceFormatImpl implements CurrentDateServiceFormat{
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy");
CurrentDateService service;
public void myMethod(){
return service.getCurrentDate().format(FORMATTER);
}
public void setService(CurrentDateService service){
this.service = service;
}
}
<?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" />
<bean id= "CurrentDateServiceFormat" class ="xx.CurrentDateServiceFormatImpl">
<property name="service" id-ref="currentDateService"/>
</bean>
</beans>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.