Spring實戰(zhàn)之獲取方法返回值操作示例
本文實例講述了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">
<!-- 下面配置相當(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ū)動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ū)動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ū)動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ū)動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ū)動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>
二 測試類
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");
}
}
三 測試結(jié)果

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
- 詳解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)文章
Mybatis中isNotNull與isNotEmpty的使用心得
這篇文章主要介紹了Mybatis中isNotNull與isNotEmpty的使用心得,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
java求100之內(nèi)的素數(shù)(質(zhì)數(shù))簡單示例
這篇文章主要介紹了java求100之內(nèi)的素數(shù)簡單示例,素數(shù)是一個大于1的自然數(shù),如果除了1和它自身外,不能被其他自然數(shù)整除的數(shù);否則稱為合數(shù)2014-04-04
Hibernate中Session.get()方法和load()方法的詳細(xì)比較
今天小編就為大家分享一篇關(guān)于Hibernate中Session.get()方法和load()方法的詳細(xì)比較,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03

