1、Spring之HelloWorld
2. 開發 HelloWorld
1). 搭建環境:
①. 加入 jar 包:
②. 通過 Spring 外掛加入 Spring 的配置檔案。檔名任意beans.xml
</beans>
2). 建立一個 POJO:必須有一個 public 型別的無引數的構造器
public class HelloWorld {
private String username;
public void setUsername(String username) {
System.out.println("setUsername: " + username);
this.username = username;
}
public void hello(){
System.out.println("hello: " + username);
}
public HelloWorld() {
System.out.println("HelloWorld's Constructor...");
}
}
3). 配置 Spring 的配置檔案
<!-- 1. 配置 bean -->
<!--
id: bean 在 IOC 容器中的 id
class: 指向 bean 的全類名. IOC 容器通過反射的方式建立物件. 並把該物件放在 IOC 容器中. 使用 id 屬性來應用該 bean
-->
<bean id="helloWorld" class="com.atguigu.spring.helloworld.HelloWorld">
<!--
2. 配置屬性
name: 屬性名. setter 風格的.
value: 屬性值
-->
<property name="username" value="Spring"></property>
</bean>
4). 在 Main 方法中測試使用 Spring
1). 搭建環境:
①. 加入 jar 包:
commons-logging-1.1.1.jar(Spring 日誌依賴的包)
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
②. 通過 Spring 外掛加入 Spring 的配置檔案。檔名任意beans.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">
</beans>
2). 建立一個 POJO:必須有一個 public 型別的無引數的構造器
public class HelloWorld {
private String username;
public void setUsername(String username) {
System.out.println("setUsername: " + username);
this.username = username;
}
public void hello(){
System.out.println("hello: " + username);
}
public HelloWorld() {
System.out.println("HelloWorld's Constructor...");
}
}
3). 配置 Spring 的配置檔案
<!-- 1. 配置 bean -->
<!--
id: bean 在 IOC 容器中的 id
class: 指向 bean 的全類名. IOC 容器通過反射的方式建立物件. 並把該物件放在 IOC 容器中. 使用 id 屬性來應用該 bean
-->
<bean id="helloWorld" class="com.atguigu.spring.helloworld.HelloWorld">
<!--
2. 配置屬性
name: 屬性名. setter 風格的.
value: 屬性值
-->
<property name="username" value="Spring"></property>
</bean>
4). 在 Main 方法中測試使用 Spring
//1. 建立 IOC 容器: Spring 會讀取配置檔案. 建立 bean 例項, 併為屬性賦值
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
//2. 從 IOC 容器中獲取 bean
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//3. 呼叫 bean 的方法
helloWorld.hello();
相關文章
- 【web】myeclipse+spring boot+maven之helloworldWebEclipseSpring BootMaven
- FASM之HelloWorldASM
- React入門-1.helloworldReact
- netty 之 telnet HelloWorld 詳解Netty
- 學習Source Generators之HelloWorld
- Flask之 安裝與HelloWorldFlask
- Kubernetes與Spring Boot簡單HelloWorld教程 - MarounSpring Boot
- 快速開始HelloWorld和Python之禪Python
- golang學習筆記(1):安裝&helloworldGolang筆記
- helloworld
- 微信小遊戲開放域之helloworld遊戲
- Servlet-HelloWorldServlet
- dubbo-HelloWorld
- 區塊鏈2.0以太坊智慧合約solidity之helloworld區塊鏈Solid
- Rust 問答之從 HelloWorld 中可以學到什麼Rust
- Spring核心原理分析之MVC九大元件(1)SpringMVC元件
- 2021-1-02--------ssm之第4章 Spring核心SSMSpring
- Flutter系列(四)——HelloWorldFlutter
- ReactNative之從HelloWorld中看環境搭建、元件封裝、Props及StateReact元件封裝
- Parcel 打包示例 – React HelloWorldReact
- redis整合springboot的helloworldRedisSpring Boot
- 彙編程式碼Helloworld
- 從 HelloWorld 說開去
- Spring Boot 原理<1>Spring Boot
- Spring Boot 使用1Spring Boot
- Spring MVC1SpringMVC
- 1、Spring入門Spring
- Spring之ApplicationContextSpringAPPContext
- django新增路由訪問helloworldDjango路由
- RabbitMQ學習筆記-HelloWorldMQ筆記
- Netty5 入門HelloWorldNetty
- 30個類手寫Spring核心原理之環境準備(1)Spring
- Spring Cloud系列之Commons - 1. 背景與基礎知識準備SpringCloud
- .Net 8.0 除gRPC之外的另一個選擇,IceRPC之快速開始HelloWorldRPC
- spring(1)-ApplicationContextAware詳解SpringAPPContext
- Spring初步認識-(1)Spring
- Go語言學習(2) - HelloWorldGo
- 機器語言編寫helloworld
- 用eclipes寫第一個HelloWorld