[英]Spring beans value injection
I'm starting work with the Spring framework and I'm trying to wrap my head around the beans concept. 我开始使用Spring框架,并尝试将bean的概念包起来。 I have an xml file: 我有一个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>
And a class to get the current date: 和一个获取当前日期的类:
public class CurrentDateServiceImpl implements CurrentDateService {
public LocalDate getCurrentDate() {
return LocalDate.now() ;
}
What I'm trying to accomplish is a simple @test to assert if the bean value is the same as the current date i supply. 我要完成的是一个简单的@test,以断言bean值是否与我提供的当前日期相同。
What I'm stuck at is: 我坚持的是:
@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);
}
I'm unaware of how i can supply the value of the bean in to the test, and i was wondering how to accomplish it and if there was any good tutorials/documentations except for the spring docs themselves 我不知道如何将Bean的价值提供给测试,我想知道如何实现它,以及是否有除了spring docs本身以外的任何好的教程/文档。
Edit: 编辑:
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);
}
}
Why is this not printing the formatted date? 为什么这不打印格式化的日期?
Use spring support for testing. 使用弹簧支架进行测试。 Code will be cleaner and easyer to understand. 代码将更加简洁易懂。
@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);
}
}
I think you wanted to do something like this: 我认为您想执行以下操作:
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.