У меня есть простой проект под названием HelloWorld
spring, и я пытаюсь загрузить два свойства из файла applicationContext.xml
, но свойства не устанавливаются в соответствующих установщиках.
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
</dependencies>
основной метод:
public class MyApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
TrackCoach trackCoach = context.getBean("myCoach", TrackCoach.class);
trackCoach.getDailyWorkout();
System.out.println(trackCoach.getName());
System.out.println(trackCoach.getFamily());
}
}
TrackCoach.java
public class TrackCoach {
private String name;
private String family;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("name is set");
this.name = name;
}
public String getFamily() {
return family;
}
public void setFamily(String family) {
System.out.println("family is set");
this.family = family;
}
public void getDailyWorkout() {
System.out.println("track coach!");
}
}
applicationContext.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-3.0.xsd">
<bean id="myCoach" class="TrackCoach">
<property name="name" value="Jhon"/>
<property name="family" value="Goo"/>
</bean>
</beans>
Вывод:
track coach!
null
null