1.创建bean对象
package entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {
private Integer id;
private String name;
private String sex;
private String specialty;
private String grade;
public Student(String name, String sex, String specialty, String grade) {
this.name = name;
this.sex = sex;
this.specialty = specialty;
this.grade = grade;
}
}
2.创建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.xsd">
<bean id="student" class="entity.Student">
<property name="id" value="666"/>
<property name="name" value="Spring"/>
<property name="sex" value="男"/>
<property name="grade" value="大六"/>
<property name="specialty" value="信息安全"/>
</bean>
</beans>
3.简单使用
import entity.Student;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
private static Logger logger = LogManager.getLogger("SpringTest");
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) applicationContext.getBean("student");
logger.debug(student.toString());
}
}