spring 设置(XML配置)
示例
创建HelloSpring的步骤:
研究SpringBoot,看看它是否更适合您的需求。
有一个正确的依赖项设置的项目。建议您使用Maven或Gradle。
创建一个POJO类,例如Employee.java
创建一个XML文件,您可以在其中定义类和变量。e.gbeans.xml
创建您的主类,例如Customer.java
包括spring-beans(及其传递依赖项!)作为依赖项。
Employee.java:
package com.test;
public class Employee {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name= name;
}
public void displayName() {
System.out.println(name);
}
}beans.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-4.3.xsd">
<bean id="employee" class="com.test.Employee">
<property name="name" value="test spring"></property>
</bean>
</beans>Customer.java:
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Customer {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Employee obj = (Employee) context.getBean("employee");
obj.displayName();
}
}
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短