天天看点

[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>