溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

使用Spring怎么實(shí)現(xiàn)獲取方法的返回值

發(fā)布時(shí)間:2021-02-19 16:01:45 來(lái)源:億速云 閱讀:246 作者:Leah 欄目:編程語(yǔ)言

使用Spring怎么實(shí)現(xiàn)獲取方法的返回值?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎ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è)試類(lèi)

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");
  }
}

關(guān)于使用Spring怎么實(shí)現(xiàn)獲取方法的返回值問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI