Image

Imagesoir wrote in Imageru_java

Spring joke

Дело было вечером, делать было нечего, в результате родилось Swing приложение написанное на Spring framework. В приложении 4 строчки Java кода и один xml файл с 20 Spring бинами:


import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.swing.JFrame;


public class Main {
    public static void main(String[] args) {
        JFrame frame = ((JFrame) new ClassPathXmlApplicationContext(
                "context.xml").getBean("frame"));
        frame.pack();
        frame.setVisible(true);
    }
}

******

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ExitActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
}

******

<?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-2.0.xsd"
>

        <!-- constants -->
        <bean id="defaultCloseOperation" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
                <property name="staticField" value="javax.swing.JFrame.EXIT_ON_CLOSE"/>
        </bean>
        <bean id="horizontalAlignment" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
                <property name="staticField" value="javax.swing.JLabel.CENTER"/>
        </bean>
        <bean id="title" class="java.lang.String">
                <constructor-arg value="Hello World!"/>
        </bean>
        <bean id="buttonText" class="java.lang.String">
                <constructor-arg value="OK"/>
        </bean>
        <bean id="centerInBorderLayout" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
                <property name="staticField" value="java.awt.BorderLayout.CENTER"/>
        </bean>
        <bean id="pageEndInBorderLayout" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
                <property name="staticField" value="java.awt.BorderLayout.PAGE_END"/>
        </bean>
        <!-- beans -->
        <bean id="frame" class="javax.swing.JFrame" init-method="pack">
                <property name="defaultCloseOperation" ref="defaultCloseOperation"/>
                <property name="title" ref="title"/>
        </bean>
        <bean id="layout" class="java.awt.BorderLayout" scope="prototype"/>
        <bean id="rootPanel" class="javax.swing.JPanel">
                <property name="layout" ref="layout"/>
        </bean>
        <bean id="msgPanel" class="javax.swing.JPanel">
                <property name="layout" ref="layout"/>
        </bean>
        <bean id="buttonPanel" class="javax.swing.JPanel"/>
        <bean id="greetings" class="javax.swing.JLabel">
                <property name="text" ref="title"/>
                <property name="horizontalAlignment" ref="horizontalAlignment"/>
        </bean>
        <bean id="okButton" class="javax.swing.JButton">
                <property name="text" ref="buttonText"/>
                <property name="horizontalAlignment" ref="horizontalAlignment"/>
        </bean>
        <bean id="exitActionListener" class="ExitActionListener"/>
        <!-- build -->
        <bean id="addListener2Button" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                <property name="targetObject" ref="okButton"/>
                <property name="targetMethod" value="addActionListener"/>
                <property name="arguments" ref="exitActionListener"/>
        </bean>
        <bean id="addGreetings2MsgPanel" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                <property name="targetObject" ref="msgPanel"/>
                <property name="targetMethod" value="add"/>
                <property name="arguments">
                        <list>
                                <ref local="greetings"/>
                                <ref local="centerInBorderLayout"/>
                        </list>
                </property>
        </bean>
        <bean id="addMsgPanel2RootPanel" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                <property name="targetObject" ref="rootPanel"/>
                <property name="targetMethod" value="add"/>
                <property name="arguments">
                        <list>
                                <ref local="msgPanel"/>
                                <ref local="centerInBorderLayout"/>
                        </list>
                </property>
        </bean>
        <bean id="addOkButton2ButtonPanel" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                <property name="targetObject" ref="buttonPanel"/>
                <property name="targetMethod" value="add"/>
                <property name="arguments" ref="okButton"/>
        </bean>
        <bean id="addButtonPanel2RootPanel" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                <property name="targetObject" ref="rootPanel"/>
                <property name="targetMethod" value="add"/>
                <property name="arguments">
                        <list>
                                <ref local="buttonPanel"/>
                                <ref local="pageEndInBorderLayout"/>
                        </list>
                </property>
        </bean>
        <bean id="addRootPanel2Frame" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                <property name="targetObject" ref="frame"/>
                <property name="targetMethod" value="add"/>
                <property name="arguments" ref="rootPanel"/>
        </bean>
</beans>