建maven工程
import java.util.Date; public class Student { private String stuNum; private String stuName; private int stuAge; //private Date enterenceTime; @Override public String toString() { return "Student{" + "stuNum='" + stuNum + '\'' + ", stuName='" + stuName + '\'' + ", stuAge=" + stuAge + '}'; } public String getStuNum() { return stuNum; } public void setStuNum(String stuNum) { this.stuNum = stuNum; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } /* public Date getEnterenceTime() { return enterenceTime; }
package com.qfedu.ioc.test; import com.qfedu.ioc.bean.Student; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = (Student) context.getBean("student"); System.out.println(student); System.out.println(student.getStuAge()); } }
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="com.qfedu.ioc.bean.Student"> <property name="stuNum" value="1"/> <property name="stuName" value="ygl"/> <property name="stuAge" value="21"/> </bean> </beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spring1</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <!--suppress MavenModelInspection --> <version>5.1.5.RELEASE</version> </dependency> </dependencies> </project>