Spring學習筆記——第二部分 HelloSpring
阿新 • • 發佈:2021-02-08
技術標籤:Spring學習筆記javaspringmaven
Spring學習筆記——第二部分 HelloSpring
1. 配置檔案
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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.zhang.pojo.Hello">
<property name="str" value="Spring"/>
</ bean>
</beans>
2. 實體類
package com.zhang.pojo;
public class Hello {
private String str;
public void setStr(String str) {
this.str = str;
}
public String getStr() {
return str;
}
@Override
public String toString(){
return "Hello{" +
"str='" + str +"\'" +
"}";
}
}
3. 測試
public static void main(String[] args) {
//獲取Spring的上下文物件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//我們的物件都在Spring中管理,需要時取出即可
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello);
}