天天看點

[Java web]-- spring3(1)

1.spring  開源架構:管理項目中各個元件,為 j2ee開發提供更好的解決方案。

  M  model  hibernate        

  V  JSP                   

  C  Controller  struts2

===============================================================================================

2.spring:容器,工廠

        :工廠,項目中各個元件由spring負責建立

:容器,項目中的各個元件由spring統一管理

:spring的一切功能都建立在容器的基礎之上。

===============================================================================================

3.spring工廠建立流程:

  3.0 原理:通過反射建立bean+空參構造

  3.1 導入依賴

  3.2 配置檔案

      位置:任意

  名字:任意   applicationContext.xml   beans.xml 

 <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-3.2.xsd">​​

<!-- 指定在spring工廠中要建立哪些元件的bean 
bean 描述一個bean  
:id  bean的辨別
:class bean的全限定名
-->
<bean id="userDAO35" class="com.c35.dao.UserDAOImpl"></bean>
<bean id="userService" class="com.c35.service.UserServiceImpl"></bean>
</beans>      

  3.3 建立spring工廠

  ApplicationContext 工廠(接口)

  ApplicationContext context=new ClassPathXmlApplicationContext("/com/c35/spring/applicationContext.xml");

  3.4 從工廠中擷取bean:同過bean的ID擷取對應的bean

UserDAO userDAO=(UserDAO)context.getBean("userDAO35");
  UserService userService=(UserService)context.getBean("userService35");      

==============================================================================================

4.IOC :控制反轉,将成員變量的指派權,由代碼中反轉到了配置中,以注入(Deny Inject)

  的方式為成員變量指派。

  :降低了具有依賴關系的元件之間的耦合度。

    【4.1 set注入】:通過set方法為bean的屬性指派

           :為需要注入值得屬性添加set方法。

【注入自建類型:    

<bean id="userDAO35" class="com.c35.dao.UserDAOImpl"></bean>
<bean id="userService35" class="com.c35.service.UserServiceImpl">
<!-- 将id為userDAO35的bean指派給userService35内部的名為userDAO的成員變量 -->
<property name="userDAO" ref="userDAO35"></property>
</bean>      

【注入jdk類型:

<bean id="user35" class="com.c35.spring.ioc.di.User">
<!-- 數字,布爾,字元串 -->
<property name="id" value="35"></property>
<property name="name" value="c35spring"></property>
<property name="gender" value="true"></property>
<!-- 數組,list集合 -->
<property name="hobbies">
<list>
<value>football</value>
<value>basketball</value>
<value>volleyball</value>
</list>
</property>
<property name="addresses">
<list>
<value>bj</value>
<value>sh</value>
<value>baoding</value>
</list>
</property>
<!-- 泛型為自建類型的集合 -->
<property name="addresses2">
<!-- local:僅在目前檔案中查找
  bean:在所有的檔案中查找
 -->
<list>
<ref local="addr1"/>
<ref bean="addr2"/>
</list>
</property>
<!-- map -->
<property name="stu">
<map>
<entry key="sunzhen" value="corejava"></entry>
<entry key="shizhiyuan" value="javaweb"></entry>
<entry key="mengqing" value="framework"></entry>
<entry key="zhangxingyu" value="jquery"></entry>
</map>
</property>
<property name="stu2">
<map>
<entry key="sunzhen3" value="123"></entry>
<entry key="shizhiyuan3" value="true"></entry>
<entry key="mengqing3" value-ref="addr1"></entry>
</map>
</property>
<!-- properties -->
<property name="pro">
<props>
<prop key="driverclass35">oracle.jdbc.C35Driver</prop>
<prop key="url35">jdbc:c35:8888:xe</prop>
</props>
</property>
</bean>      

4.2 構造注入:通過構造方法為bean的屬性指派

            :需要為屬性添加構造方法。

<bean id="user352" class="com.c35.spring.ioc.di.User2">
<!-- index 從0開始 -->
<constructor-arg index="0" type="java.lang.Integer">
<value>1</value>
</constructor-arg>
<constructor-arg index="1" type="java.lang.String">
<value>c35</value>
</constructor-arg>
<constructor-arg index="2" type="java.lang.Boolean">
<value>true</value>
</constructor-arg>
</bean>      
*byName:如果bean中的屬性和工廠中的bean同名,則自動注入(可讀性較低)
*byType:如果bean中的屬性和工廠中的bean時同樣的類型,則自動注入(極易沖突)
<bean id="user354" class="com.c35.spring.ioc.di.User3" autowire="byName"></bean>
    <bean id="user355" class="com.c35.spring.ioc.di.User3" autowire="byType"></bean>