javascript
1.搭建Spring环境
開發步驟:
? ? ?第1步: 添加jar包
- ? ? ? ? commons-logging-1.2.jar
- ? ? ? ? spring-aop-5.1.2.RELEASE.jar
- ? ? ? ? spring-beans-5.1.2.RELEASE.jar
- ? ? ? ? spring-context-5.1.2.RELEASE.jar
- ? ? ? ? spring-core-5.1.2.RELEASE.jar
- ? ? ? ? spring-expression-5.1.2.RELEASE.jar
commons-logging-1.2.jar另行下載,其余jar包在下載壓縮包解壓后libs目錄下
? ? ?第2步: 創建Java類
? ? ?? ? ? ?創建1個實體類
? ? ?? ? ? ?備注: 測試Spring框架控制實體類[創建實體類,提供實體類對象]
? ? ?第3步: 創建Spring配置文件
? ? ?? ? ? ?配置文件名稱 [ beans.xml,application.xml,spring.xml,spring-bean.xml...] 任意,位置不限[src下].
? ? ?? ? ? ?配置文件模板位置 [ 參考官網或下載的ZIP中的?pdf 或 html ]
? ? ? ? ? ?這里舉例下載的壓縮包中的位置:
? ? ? ? ? ? ? ? 1.在docs > spring-framework-reference 打開index.html文件
? ? ? ? ? ? ? ?2.點擊 Core 超鏈接
? ? ? ? ? ? ? ?3. 進入頁面后往下翻,或者直接點擊左側導航欄就可以直接找到要求的配置文件格式
? ?
? ? ? ? ? ? ? ? 示例:
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --></bean><!-- more bean definitions go here --> </beans>備注:可以把常用的配置文件做成模板來用,方便創建,自定義模板請看
? ? ?第4步: 編寫Spring配置文件
? ? ?? ? ? ?<bean>用于將指定位置的Java類,交給Spring容器管理 [ 控制反轉 ( 幫你創建對象 ) ,依賴注入 ( 向你提供對象 ) 等等 ]?
<bean id="類昵稱,自定義"? class="類所在的真實路徑" />? ? ?? ? ? ?示例:
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="myuser" class="entity.User" /></beans>? ? ?? ? ? ?bean標簽<bean id="" class="" />和<bean id="" class=""></bean>沒有區別,一個在自身閉合,一個是再寫一個閉合標簽閉合
? ? ?第5步: 測試程序
public static void main(String[] args) {//讀取配置文件ApplicationContext app=new ClassPathXmlApplicationContext("application.xml");//從Spring容器中獲得1個對象User user= (User) app.getBean("myuser");//Object-->Useruser.setName("小白");System.out.println(user); }?
? ? ?實體類User,配合測試代碼和配置文件閱讀
package entity;import org.springframework.stereotype.Component;/*** @auther LiuWeirui* @date 2021/6/7 9:39*/ public class User {private int id;private String name;private char sex;private int age;private String address;@Overridepublic String toString() {return "\nUserMapper{" +"id=" + id +", name='" + name + '\'' +", sex=" + sex +", age=" + age +", address='" + address + '\'' +'}' + "\n";}public User() {}public User(int id, String name, char sex, int age, String address) {this.id = id;this.name = name;this.sex = sex;this.age = age;this.address = address;}//省略get和set方法get() and set() ... }總結
以上是生活随笔為你收集整理的1.搭建Spring环境的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 指数上涨股票却在跌什么原因
- 下一篇: SpringMVC_1.认识MVC