javascript
1.搭建Spring环境
開(kāi)發(fā)步驟:
? ? ?第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步: 創(chuàng)建Java類
? ? ?? ? ? ?創(chuàng)建1個(gè)實(shí)體類
? ? ?? ? ? ?備注: 測(cè)試Spring框架控制實(shí)體類[創(chuàng)建實(shí)體類,提供實(shí)體類對(duì)象]
? ? ?第3步: 創(chuàng)建Spring配置文件
? ? ?? ? ? ?配置文件名稱 [ beans.xml,application.xml,spring.xml,spring-bean.xml...] 任意,位置不限[src下].
? ? ?? ? ? ?配置文件模板位置 [ 參考官網(wǎng)或下載的ZIP中的?pdf 或 html ]
? ? ? ? ? ?這里舉例下載的壓縮包中的位置:
? ? ? ? ? ? ? ? 1.在docs > spring-framework-reference 打開(kāi)index.html文件
? ? ? ? ? ? ? ?2.點(diǎn)擊 Core 超鏈接
? ? ? ? ? ? ? ?3. 進(jìn)入頁(yè)面后往下翻,或者直接點(diǎn)擊左側(cè)導(dǎo)航欄就可以直接找到要求的配置文件格式
? ?
? ? ? ? ? ? ? ? 示例:
<?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>備注:可以把常用的配置文件做成模板來(lái)用,方便創(chuàng)建,自定義模板請(qǐng)看
? ? ?第4步: 編寫(xiě)Spring配置文件
? ? ?? ? ? ?<bean>用于將指定位置的Java類,交給Spring容器管理 [ 控制反轉(zhuǎn) ( 幫你創(chuàng)建對(duì)象 ) ,依賴注入 ( 向你提供對(duì)象 ) 等等 ]?
<bean id="類昵稱,自定義"? class="類所在的真實(shí)路徑" />? ? ?? ? ? ?示例:
<?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標(biāo)簽<bean id="" class="" />和<bean id="" class=""></bean>沒(méi)有區(qū)別,一個(gè)在自身閉合,一個(gè)是再寫(xiě)一個(gè)閉合標(biāo)簽閉合
? ? ?第5步: 測(cè)試程序
public static void main(String[] args) {//讀取配置文件ApplicationContext app=new ClassPathXmlApplicationContext("application.xml");//從Spring容器中獲得1個(gè)對(duì)象User user= (User) app.getBean("myuser");//Object-->Useruser.setName("小白");System.out.println(user); }?
? ? ?實(shí)體類User,配合測(cè)試代碼和配置文件閱讀
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() ... }總結(jié)
以上是生活随笔為你收集整理的1.搭建Spring环境的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 指数上涨股票却在跌什么原因
- 下一篇: 铁建银信是商业承兑吗