Spring實戰(zhàn)之獲取方法返回值操作示例
更新時間:2019年12月03日 09:51:58 作者:cakincqm
這篇文章主要介紹了Spring實戰(zhàn)之獲取方法返回值操作,涉及spring配置文件與方法返回值操作相關使用技巧,需要的朋友可以參考下
本文實例講述了Spring實戰(zhàn)之獲取方法返回值操作。分享給大家供大家參考,具體如下:
一 配置文件
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 下面配置相當于如下Java代碼:
JFrame win = new JFrame("我的窗口");
win.setVisible(true); -->
<bean id="win" class="javax.swing.JFrame">
<constructor-arg value="我的窗口" type="java.lang.String"/>
<property name="visible" value="true"/>
</bean>
<!-- 下面配置相當于如下Java代碼:
JTextArea jta = JTextArea(7, 40); -->
<bean id="jta" class="javax.swing.JTextArea">
<constructor-arg value="7" type="int"/>
<constructor-arg value="40" type="int"/>
</bean>
<!-- 使用MethodInvokingFactoryBean驅動Spring調用普通方法
下面配置相當于如下Java代碼:
win.add(new JScrollPane(jta)); -->
<bean class=
"org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="win"/>
<property name="targetMethod" value="add"/>
<property name="arguments">
<list>
<bean class="javax.swing.JScrollPane">
<constructor-arg ref="jta"/>
</bean>
</list>
</property>
</bean>
<!-- 下面配置相當于如下Java代碼:
JPanel jp = new JPanel(); -->
<bean id="jp" class="javax.swing.JPanel"/>
<!-- 使用MethodInvokingFactoryBean驅動Spring調用普通方法
下面配置相當于如下Java代碼:
win.add(jp , BorderLayout.SOUTH); -->
<bean class=
"org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="win"/>
<property name="targetMethod" value="add"/>
<property name="arguments">
<list>
<ref bean="jp"/>
<util:constant static-field="java.awt.BorderLayout.SOUTH"/>
</list>
</property>
</bean>
<!-- 下面配置相當于如下Java代碼:
JButton jb1 = new JButton("確定"); -->
<bean id="jb1" class="javax.swing.JButton">
<constructor-arg value="確定" type="java.lang.String"/>
</bean>
<!-- 使用MethodInvokingFactoryBean驅動Spring調用普通方法
下面配置相當于如下Java代碼:
jp.add(jb1); -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jp"/>
<property name="targetMethod" value="add"/>
<property name="arguments">
<list>
<ref bean="jb1"/>
</list>
</property>
</bean>
<!-- 下面配置相當于如下Java代碼:
JButton jb2 = new JButton("取消"); -->
<bean id="jb2" class="javax.swing.JButton">
<constructor-arg value="取消" type="java.lang.String"/>
</bean>
<!-- 使用MethodInvokingFactoryBean驅動Spring調用普通方法
下面配置相當于如下Java代碼:
jp.add(jb2); -->
<bean class=
"org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jp"/>
<property name="targetMethod" value="add"/>
<property name="arguments">
<list>
<ref bean="jb2"/>
</list>
</property>
</bean>
<!-- 使用MethodInvokingFactoryBean驅動Spring調用普通方法
下面配置相當于如下Java代碼:
win.pack(); -->
<bean class=
"org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="win"/>
<property name="targetMethod" value="pack"/>
</bean>
</beans>
二 測試類
package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.crazyit.app.service.*;
public class SpringTest
{
public static void main(String[] args)
{
ApplicationContext ctx =
new ClassPathXmlApplicationContext("beans.xml");
}
}
三 測試結果

更多關于java相關內容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數據結構與算法教程》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
您可能感興趣的文章:
- 詳解springmvc之json數據交互controller方法返回值為簡單類型
- SpringMVC 方法四種類型返回值總結(你用過幾種)
- SpringBoot異步調用方法并接收返回值
- Spring MVC Controller返回值及異常的統(tǒng)一處理方法
- 詳解利用SpringMVC攔截器控制Controller返回值
- 詳解SpringCloud Zuul過濾器返回值攔截
- SpringMVC Controller 返回值的可選類型詳解
- Java中Spring獲取bean方法小結
- 監(jiān)聽器獲取Spring配置文件的方法
- springmvc之獲取參數的方法(必看)
- Spring 中優(yōu)雅的獲取泛型信息的方法
相關文章
全面掌握Java中的循環(huán)控制語句與條件判斷語句的使用
這篇文章主要介紹了Java中的循環(huán)控制語句與條件判斷語句的使用,循環(huán)和判斷是Java編程中流程控制的基礎,需要的朋友可以參考下2016-02-02
Idea Jrebel 報錯:Cannot reactivate,offline 
本文主要介紹了Idea Jrebel 報錯:Cannot reactivate,offline seat in use,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06
IntelliJ IDEA里找不到javax.servlet的jar包的解決方法
這篇文章主要介紹了IntelliJ IDEA里找不到javax.servlet的jar包的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
Java8優(yōu)雅的字符串拼接工具類StringJoiner實例代碼
這篇文章主要給大家介紹了關于Java8優(yōu)雅的字符串拼接工具類StringJoiner的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02
MyBatis saveBatch 性能調優(yōu)的實現
本文主要介紹了MyBatis saveBatch 性能調優(yōu)的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07

