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();
   }
}
      