Spring1

Spring:
    Spring是一個一站式的分層輕量級框架.
Spring框架的優點:
    方便解耦,簡化開發
    AOP編程的支持-->面向切面編程
    聲明式事務的支持
    方便程序的測試-->對JUnit4測試整合
    方便集成各種優秀框架
    降低javaEE API的使用難度
環境搭建:
    1、jar包下載,spring.io,在spring3.0.2後不再提供依賴jar包
    2、導入必要jar包:
        bean相關
        core相關
        context相關
        spel相關
    3、在src下創建核心配置文件:applicationContext.xml
        約束:spring/spring-framework/docs/spring-framework-reference/html
    4、導入commons-loggin-1.2.jar/log4j-1.2.16.jar


IOC【*****】
    IOC:inversion of controller 控制反轉
        -->原來由我們自己實例化的對象交給Spring容器來實例化
    使用步驟:
    1、在覈心配置文件中配置bean
        <bean id/name="" class=""></bean>
    2、創建一個ApplicationContext對象-->BeanFactory的子接口
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    3、通過getBean獲取指定對象:
        Bean bean = (Bean)applicationContext.getBean("id/name");

DI【*****】
    DI:dependency injection 依賴注入
    IOC和DI的區別:【****】
        IOC控制反轉,是指對象實例化的權利由Spring容器來管理
        DI依賴注入,在Spring創建對象的過程中,對象所依賴的屬性通過配置注入對象中
    屬性注入:
        1、構造器注入:
            提供有參構造構造器:
                <bean id/name="" class="">
                    <constructor-arg name="屬性名" index="參數索引" type="參數類型" value="參數值"/>
                    <constructor-arg name="屬性名" index="參數索引" type="參數類型" ref="引用其他bean"/>
                    ...
                </bean>
        2、set方法注入:
            提供參數的set方法
                <bean id/name="" class="">
                    <property name="set後" value="屬性值"/>
                    <property name="set後" ref="引用其他bean"/>
                </bean>
        3、集合屬性的注入:
            List/[]  Set  Map  Properties
                <bean id/name="" class="">
                    <property name="List集合/數組屬性名">
                        <list>
                            <value>xx</value>
                            ...
                        </list>
                    </property>
                    <property name="Set集合屬性名">
                        <set>
                            <value>xx</value>
                            ...
                        </set>
                    </property>
                    <property name="Map集合屬性名">
                        <map>
                            <entry key="" value=""/>
                                ...
                        </map>
                    </property>
                    <property name="Property集合屬性名">
                        <props>
                            <prop key="">xx</value>
                            ...
                        </props>
                </property>
            </bean>
    4、名稱空間p/c對屬性注入:
        p/c不是真正的名稱空間,是虛擬的,是嵌入到Spring的內核中的.
        使用p名稱空間解決對set方法注入的<property>簡化開發-->p:屬性名
        使用c名稱空間解決對構造器注入的<constructor-arg>簡化開發-->c:屬性名
    5、SpEl簡化屬性注入:#{表達式}
        ref="其他bean的引用" --> value="#{其他bean的引用}"
        value="#{其他bean的引用.屬性名}"
        value="#{其他bean的引用.get方法}"-->可在表達式中做運算操作

Bean獲取與實例化:
    1、BeanFactory採取延遲加載的方案,只有真正getBean時纔會實例化Bean;
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
     2、ApplicationContext是BeanFactory的子接口,真正使用的是ApplicationContext的實現類【*****】
            FileSystemXmlApplicationContext  根據文件路徑獲取
            ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src/applicationContext.xml");
            ClassPathXmlApplicationContext   根據類路徑獲取
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            -->ApplicationContext會在加載配置文件時,初始化配置的所有Bean,並且ApplicationContex提供了不同的應用層的context實現,web層-->WebApplicationContext
Bean的實例化:
    1、無參構造【*****】
        Bean中提供無參構造
        <bean id/name="" class=""></bean>
    2、靜態工廠
        創建一個工廠類,提供靜態的返回Bean的方法
        <bean name/id="" class="BeanFactory" factory-method=""></bean>
    3、實例化工廠
        創建一個工廠類,提供非靜態的返回Bean的方法
        <bean id/name="factory" class="BeanFactory"></bean>
        <bean id/name="bean" factory-bean="factory" factory-method=""></bean>
    bean的作用域:
        在bean標籤設置scope=""
            singleton:單例(默認值)
            prototype:多例
            request:用在web開發中,將bean對象存儲到request域中
            session:用在web開發中,將bean對象存儲到session域中
    bean的生命週期:
        注意事項:
        destroy-method只對scope=singleton有效

Spring的註解開發【*****】
    使用註解前提條件:
        1、在覈心配置文件中引入context的約束
        2、在覈心配置文件中配置標籤
            <context:annotation-config/> -->使註解生效,但找不到註解的位置
            <context:component-scan base-package="掃描的包"></context:component-scan> --> 指定Spring掃描的包,包含上面標籤的功能,所有一般配置該標籤即可
        3、如果不是使用Spring的3.x版本,需要導入apring-aop-4.2.4.RELEASE.jar
    註解的使用:
    @component("") --> 在類上配置==<bean id/name="" class="">
        Spring2.5@component("")衍生的三個註解:
    @Repository 用於DAO層
    @Service 用於service層
    @Controller 用於表現層
    @Scope("") --> 在類上配置,Bean的作用域
    @Value("") --> 簡單屬性的注入
    @Autowired --> 根據類型的複雜屬性注入
    @Qualifier("") --> 與Autowired同用根據名稱的複雜屬性注入
    @Resource(name="") --> 等同於上兩個的結合使用,jdk提供
    @PostConstruct --> 在自定義初始化方法配置==<bean init-method=""> --> jdk提供
    @PreDestroy --> 在自定義銷燬方法配置==<bean destroy-method=""> -->jdk提供

Spring整合JUnit4測試:
    1、導入spring-test-4.2.4.RELEASE.jar
    2.在測試類上添加註解
        @Runwith(SpringJUnit4Runner.class)
        @ContextConfiguration(location="classpath:applicationContext.xml")


Spring在web開發中的應用:
    1、導入spring-web-4.2.4.RELEASE.jar
    2、在web.xml中配置Listener
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        ContextLoaderListener實現了ServletContextListener
        當服務器啓動時,將ApplicationContext對象的實現類WebApplicationContext對象存儲到ServletContext中
        我們可以再web層獲取ApplicationContext對象:
            -->ApplicationContext application = (ApplicationContext) this.getServletContext()
.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    3、在web.xml中配置Spring核心配置文件的位置
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章