當前位置:
首頁 >
一个Spring入门小案例
發布時間:2025/3/21
33
豆豆
生活随笔
收集整理的這篇文章主要介紹了
一个Spring入门小案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
創建兩個類,配置類的bean對象。從容器取對象,調用方法。
Spring類
package com.southstar;public class Spring {private String name;public void setBye(Bye bye) {this.bye = bye;}public Bye bye;public void setName(String name) {this.name = name;}public void sayHello(){System.out.println("hello:"+ name);bye.sayBye();}Bye類
package com.southstar;public class Bye {private String name;public void setName(String name) {this.name = name;}public void sayBye(){System.out.println("bye:"+name);} }Test.java
package com.test;import com.southstar.Spring;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test{public static void main(String[] arg){ApplicationContext ac =new ClassPathXmlApplicationContext("Test.xml");Spring sp = (Spring)ac.getBean("spring");sp.sayHello();} }Test.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"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><bean id="spring" class="com.southstar.Spring"> <property name="name"><value>沒魚</value> </property><property name="bye" ref="bye"/></bean><bean id="bye" class="com.southstar.Bye"><property name="name" value="weib"></property></bean></beans>結果:
hello:沒魚
bye:weib
?
總結
以上是生活随笔為你收集整理的一个Spring入门小案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 框架:spring总结
- 下一篇: Spring boot 入门篇