spring入門

  1. 建立項目
        項目名稱:spring092601
    2.引入相關jar包java

        --日誌記錄
        commons-logging.jarspring

        --單元測試
        junit-4.4.jarexpress

        --日誌
        log4j.jar
        --SpringIoC(依賴注入)的基礎實現
        spring-beans-3.2.0.RELEASE.jar
        --Spring提供在基礎IoC功能上的擴展服務,此外還提供許多企業級服務的支持
        spring-context-3.2.0.RELEASE.jar
        --Spring3.0的核心工具包
        spring-core-3.2.0.RELEASE.jar
        --Spring表達式語言
        spring-expression-3.2.0.RELEASE.jar
    3.添加配置文件
        在項目中建立conf源文夾,並在conf目錄下建立applicationContext.xml文件
        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:p="http://www.springframework.org/schema/p"
               xmlns:util="http://www.springframework.org/schema/util"
               xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
        </beans>
    4.建立業務bean
        在src目錄下建立HelloSpring業務Bean
        包名:cn.jbit.spring092601.domain
        package cn.jbit.spring092601.domain;
        import java.io.Serializable;
        /**
         * spring入門
         * @author Administrator
         *
         */
        public class HelloSpring implements Serializable {
            private String name;//名稱
        
            public void setName(String name) {
                this.name = name;
            }
        
            public String getName() {
                return name;
            }
        }
    5.在配置文件中配置HelloSpring業務bean,在applicationContext.xml文件中的beans節點下配置,  配置完成後的applicationContext.xml文件以下:
        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:p="http://www.springframework.org/schema/p"
               xmlns:util="http://www.springframework.org/schema/util"
               xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
            <!-- spring入門
                控制反轉IOC
             -->
            <bean id="helloSpring" class="cn.jbit.spring092601.domain.HelloSpring">
                <!--
                    給屬性注入一個值:hello
                 -->
                <property name="name">
                    <value>hello</value>
                </property>
            </bean>
        </beans>    
    6.測試spring是否工做
        在項目中建立test源文件夾,在test目錄下建立測試類
        包名:cn.jbit.spring092601.domain
        package cn.jbit.spring092601.domain;
        import org.junit.Test;
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        public class HelloSpringTest {
            /**
             * 依賴注入方式實現
             */
            @Test
            public void testHellpSpring3(){
                /*
                 * 對象由spring建立
                 */
                ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
                HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring");
                System.out.println(helloSpring.getName());
            }
            /**
             * 控制反轉方式實現
             * IOC方式
             */
            @Test
            public void testHellpSpring2(){
                /*
                 * 對象由spring建立
                 */
                ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
                HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring");
                helloSpring.setName("張三");
                System.out.println(helloSpring.getName());
            }
            /**
             * 傳統方式實現
             */
            @Test
            public void testHellpSpring1(){
                HelloSpring helloSpring = new HelloSpring();
                helloSpring.setName("zhangsan");
                System.out.println(helloSpring.getName());
            }
        }app

    QQ截圖20181014143201.jpg

相關文章
相關標籤/搜索