0
我收到豆类是如何工作的一些帮助,到目前为止,我有一个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.CurrentDateServiceImpl" />
<bean id= "CurrentDateServiceFormat" class ="xx.CurrentDateServiceFormatImpl">
<property name="service" ref="currentDateService"/>
</bean>
</beans>
一个简单的方法来获得当前日期:
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 String formatCurrentDate(){
return service.getCurrentDate().format(formatter);
}
public void setService(CurrentDateService service){
this.service = service;
}
}
我的测试是:
@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();
System.out.println(service);
System.out.println(service2);
LocalDate date2 = service.getCurrentDate();
assertEquals(date, date2);
}
}
但我打印出来的service2是空的,因此即时无法运行service2.formatCurrentDate,我做错了什么?
另请注意,如果您只是在学习Spring,您应该学习JavaConfig的处事方式 - 基于XML的配置仅用于遗留目的,不会用于这个世界...... – 2014-10-06 07:31:42
是否有任何优秀的教程可以提供建议学习JavaConfig的方式? – Monty 2014-10-06 07:35:32
最简单的出发点之一是[new Spring Boot](http://spring.io/guides/gs/spring-boot/)自动引导程序。否则,[参考文档]中有大量信息(http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch03.html)。 – 2014-10-06 07:37:33