當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
01Spring的helloworld程序
生活随笔
收集整理的這篇文章主要介紹了
01Spring的helloworld程序
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一:Spring的helloworld程序(環(huán)境已搭建)
1.導(dǎo)入spring包后,勾選spring配置文件
2.創(chuàng)建Dao.testDao接口,創(chuàng)建它的實(shí)現(xiàn)類testDaolmpl,里面有個(gè)sayhello方法
package com.Dao; public interface testDao {public void sayhello(); } package com.Dao;public class testDaoImpl implements testDao{@Overridepublic void sayhello() {System.out.println("這是一個(gè)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.測(cè)試類
注意:目前有兩種參數(shù)通過getBean獲取對(duì)象
5.IOC的原理
6.默認(rèn)參數(shù)
注意:在配置文件中聲明有參數(shù)的對(duì)象,實(shí)體類必須依賴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>測(cè)試類:
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());} }總結(jié)
以上是生活随笔為你收集整理的01Spring的helloworld程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Java设计模式]期末作业参考
- 下一篇: batch spring 重复执行_Sp