Spring(22) 跟着大佬做

  1. 这是书上的实例,做了些解释
  2. action
    <?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.xsd">
    
        <!--JFrame jframe = new JFrame("window")-->
        <!--创建 一个javax.swing.JFrame实例,并且指定它的构造器参数是String类型,值为win
        然后色设置visible变量的值为true-->
        <bean id="win" class="javax.swing.JFrame">
            <constructor-arg value="win" type="java.lang.String"/>
            <property name="visible" value="true"/>
        </bean>
    
        <!--JTextArea jea = JTextArea(7,40)-->
        <!--创建一个javax.swing.JTextArea,同时指定构造器参数-->
        <bean id="jta" class="javax.swing.JTextArea">
            <constructor-arg value="7" type="int"/>
            <constructor-arg value="40" type="int"/>
        </bean>
    
        <!--win.add(new JScrollPane(jta))-->
        <!--MethodInvokingFactoryBean的作用是为 让某个实例的某个方法的返回值注入为Bean的实例,
        也就是说,这里是为win的add方法注入实例,同时指定这个被注入的实例的参数-->
        <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>
    
        <!--JPane jp = new JPane()-->
        <!--创建一个JPanel实例-->
        <bean id="jp" class="javax.swing.JPanel"/>
    
        <!--win.add(jp, BorderLayourt.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>
    
        <!--<JButton jb1 = new JButton("ok");-->
    
        <bean id="jb1" class="javax.swing.JButton">
            <constructor-arg value="ok" type="java.lang.String"/>
        </bean>
    
        <!--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>
    
    
        <!--win.pack()-->
        <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
            <property name="targetObject" ref="win"/>
            <property name="targetMethod" value="pack"/>
        </bean>
    
    
    </beans>
    package testPackage;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SpringTest {
        public static void main(String []args){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        }
    }
    

    这是我看李刚编著的《轻量级javaEE企业应用实战(第五版)-Struts2+Spring5+Hibernate5/JAP2》后总结出来的。

  3. 今日十更,休息。

猜你喜欢

转载自blog.csdn.net/weixin_39452731/article/details/84865199
22
今日推荐