2017-07-14 8 views
0

アプリケーションをコンフィグレーションする際にcontext.xmlファイルでオーバーロードされたメソッドを使用する方法を教えてください。 は、私は自分のアプリケーションのメインフレームを設定するには、このようなコードを持っている:context.xmlのコンフィグレーション中にjava springでオーバーロードされたメソッドを使用する

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <bean id="todo_Default" class="com.yurets_y.todo_list.components.MainFrame" init-method="init" lazy-init="true"> 
     <property name="size" ref="dimention"/> 
    </bean> 

    <bean id="dimention" class="java.awt.Dimension"> 
     <constructor-arg index="0" value="200"/> 
     <constructor-arg index="1" value="300"/> 
    </bean> 


</beans> 

をし、私の代わりに、メソッドのsetSize(DIMENTIONのd)を使用するのでは、クラスのJFrameのmehtodのsetSize(int型のx、int型のy)を使用します。オーバーロードされたメソッドの異なる忠実さを使用するにはどうすればよいですか?

答えて

1

<?xml version="1.0" encoding="UTF-8"?> 

を使用したsetSize(int型のx、int型のy)を呼び出すためにHTTP org.springframework.beans.factory.config.MethodInvokingFactoryBean

可能です:// WWW。 >「springframework.org/schema/beans/spring-beans.xsd

<bean id="todo_Default" class="com.yurets_y.todo_list.components.MainFrame" init-method="init" lazy-init="true"> 
</bean> 

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 
    <property name="targetObject"> 
     <ref bean="todo_Default"/> 
    </property> 
    <property name="targetMethod"> 
     <value>setSize</value> 
    </property> 
    <property name="arguments"> 
     <list> 
      <value>300</value> 
      <value>200</value> 
     </list> 
    </property> 
</bean> 

しかし、私はむしろあなたのソリューションをjava.awt.Dimension Beanで使用したいと思います。

+0

あなたの答えをありがとう、私はそれが少し簡単になると思った。しかし、今はMethodInvokingFactoryBeanの例を使ってBeanからさまざまなメソッドを呼び出す方法を知っています –

0

これは少しトリッキーな質問です...

1)あなたはJavaの設定を使用するように試みることができます。 JavaでこのようなBeanを作成し、必要なすべてのメソッドを呼び出す方が簡単です。

2)強制的にxmlのみを使用する場合は、フレームBean自体でこのメソッドを呼び出そうとする可能性があります。 Followintクラスを使用する必要があります: org.springframework.beans.factory.config.MethodInvokingFactoryBean

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <bean id="frame" class="spring.CustomFrame"/> 

    <bean id="data" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 
    <property name="targetObject" ref="frame"/> 
    <property name="targetMethod"> 
     <value>setSize</value> 
    </property> 
    <property name="arguments"> 
     <list> 
     <value>100</value> 
     <value>200</value> 
     </list> 
    </property> 
    </bean> 


</beans> 

getSizesetSizeメソッドがクラスjava.awt.Componentの基礎となるwidthheightフィールドを使用しているので、それが起こります。 (あなたはこのpropretyをやっているとき= ...春は、このフィールドのセッターメソッドを検索しようとする)

<property name="width" value="1"/> 
<property name="height" value="1"/> 

しかし、彼らはsetterメソッドを持っていない:次のようにこれらの値を注入するために素晴らしいことです。したがって、不可能で、注入する唯一の方法 - 適切なメソッドを呼び出します。これにより、xmlでの使用が難しくなります。

関連する問題