當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
01Spring的helloworld程序
生活随笔
收集整理的這篇文章主要介紹了
01Spring的helloworld程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一:Spring的helloworld程序(環境已搭建)
1.導入spring包后,勾選spring配置文件
2.創建Dao.testDao接口,創建它的實現類testDaolmpl,里面有個sayhello方法
package com.Dao; public interface testDao {public void sayhello(); } package com.Dao;public class testDaoImpl implements testDao{@Overridepublic void sayhello() {System.out.println("這是一個hello方法");} }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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="test" class="com.Dao.testDaoImpl"></bean> </beans>4.測試類
注意:目前有兩種參數通過getBean獲取對象
5.IOC的原理
6.默認參數
注意:在配置文件中聲明有參數的對象,實體類必須依賴setget方法
Stdent類
package com.Dao;public class Student {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +'}';} }配置文件
<bean id="std" class="com.Dao.Student"><property name="id" value="191106044"></property><property name="name" value="damin"></property></bean>測試類:
package com.Test;import com.Dao.Student; import org.springframework.context.support.ClassPathXmlApplicationContext;public class test01 {public static void main(String[] args) {ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-config.xml");Student student = (Student) classPathXmlApplicationContext.getBean("std");System.out.println(student.toString());} }總結
以上是生活随笔為你收集整理的01Spring的helloworld程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Java设计模式]期末作业参考
- 下一篇: batch spring 重复执行_Sp