Spring實(shí)戰(zhàn)之獲取方法返回值操作示例
本文實(shí)例講述了Spring實(shí)戰(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">
<!-- 下面配置相當(dāng)于如下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>
<!-- 下面配置相當(dāng)于如下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驅(qū)動(dòng)Spring調(diào)用普通方法
下面配置相當(dāng)于如下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>
<!-- 下面配置相當(dāng)于如下Java代碼:
JPanel jp = new JPanel(); -->
<bean id="jp" class="javax.swing.JPanel"/>
<!-- 使用MethodInvokingFactoryBean驅(qū)動(dòng)Spring調(diào)用普通方法
下面配置相當(dāng)于如下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>
<!-- 下面配置相當(dāng)于如下Java代碼:
JButton jb1 = new JButton("確定"); -->
<bean id="jb1" class="javax.swing.JButton">
<constructor-arg value="確定" type="java.lang.String"/>
</bean>
<!-- 使用MethodInvokingFactoryBean驅(qū)動(dòng)Spring調(diào)用普通方法
下面配置相當(dāng)于如下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>
<!-- 下面配置相當(dāng)于如下Java代碼:
JButton jb2 = new JButton("取消"); -->
<bean id="jb2" class="javax.swing.JButton">
<constructor-arg value="取消" type="java.lang.String"/>
</bean>
<!-- 使用MethodInvokingFactoryBean驅(qū)動(dòng)Spring調(diào)用普通方法
下面配置相當(dāng)于如下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驅(qū)動(dòng)Spring調(diào)用普通方法
下面配置相當(dāng)于如下Java代碼:
win.pack(); -->
<bean class=
"org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="win"/>
<property name="targetMethod" value="pack"/>
</bean>
</beans>
二 測(cè)試類
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");
}
}
三 測(cè)試結(jié)果

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

