1.搭建Spring运行时环境
首先创建一个java项目,这里我用的是idea
导入jar包
①spring自身JAR包:spring-framework-4.0.0.RELEASE\libs目录下
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELE2ASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
② commons-logging-1.1.1.jar

jar包导入后,如图创建一个简单的类进行spring测试,

包含两个属性和set,get,tostring方法。
然后创建一个创建Spring的配置文件


创建完成后,应该如图所示

使用Spring创建对象,为属性赋值
然后在spring配置文件的命名空间添加对象和属性
<?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 = "person" class="com.atguigu.spring.mod.Person">
<property name="id" value="1001"></property>
<property name="name" value="小明"></property>
</bean>
</beans>

创建main方法进行测试

public static void main(String[] args) {
//初始化容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person)ac.getBean("person");
System.out.println(person);
}
- 最后控制台输入结果


915

被折叠的 条评论
为什么被折叠?



