Spring第一天

来源:互联网 发布:清镇市各乡镇人口数据 编辑:程序博客网 时间:2024/06/07 13:01

一、spirng认识

spring一战式框架
正是因为spring框架性质是属于容器性质的
容器装有什么对象就有什么功能,所以可以一站式
不仅不排斥其他框架,而且还能帮助其他框架管理对象
aop支持
ioc思想
spring jdbc
aop事务
junit测试

2.spring搭建

hello项目搭建:
1.导包
分为四大类
beans core context spel
2.实施
beans context expression core comming-logging(日志包) com.springsource.org.apache.log4j-1.2.15.jar(防止旧版本不支持)
3.书写实体类代码

public class User {            private String name;            private int age;            public String getName() {                return name;            }            public void setName(String name) {                this.name = name;            }            public int getAge() {                return age;            }            public void setAge(int age) {                this.age = age;            }            @Override            public String toString() {                return "User [name=" + name + ", age=" + age + "]";            }

4.书写配置文件,文件名任意 建议使用applicaitonContext.xml


5.代码测试` @Test
public void test() {
ApplicationContext ap = new ClassPathXmlApplicationContext(“classpath:applicationContext.xml”);
User bean = (User) ap.getBean(“user”);
System.out.println(bean);
}

IOC DI概念

ioc:inverse of control 反转控制
将我们创建对象的方式反转,
以前是对象的创建是由开发人员自己维护,包括依赖关系也是自己注入,
eg:使用spring之后,对象的创建以及依赖关系可以有spring完成创建以及注入
反转控制就是反转了对象的创建方式,从我们自己创建反转给了程序(spring)。
DI:denpendency injection依赖注入
实现ioc思想需要di做支持
注入方式:
set方法注入

                构造方法注入                字段注入(不推荐)                注入类型:                    值类型注入  :8大基本数据类型                    引用类型注入:封装的对象    spring概念:        appliactionContext&BeanFactory            BeanFactory接口:spring最原始接口,针对原始接口实现类功能较为单一            BeanFactory接口实现类特点是每次在获得对象时候才会创建对象。        AppliactionContext接口:特点  每次容器启动时候都会创建容器中配置的所有对象

还提供了更多丰富的功能。
从类路径下家再配置文件 ClassPathXmlApplicationContext
从硬盘绝对路径下加载配置:FileSystemXmlApplicationContext(“d://xx//”)
结论: web开发中使用ApplicationContext 资源匮乏中使用BeanFactory 接口

spring配置详解

a,bean的详解






b.spirng创建对象的方式
1)。空参构造方式(重要)

        2)静态工厂方式(了解)        <!-- 创建方式二:静态工厂创建            调用userFactory的create方法创建名为user2的userFactory对性爱那个         -->        <bean name="user2" class="com.spring.test.b_create.UserFactory" factory-method="CreateUser">        </bean>        测试代码        // 创建方式二、静态工厂方式创建user对象        public void test2() {            // 创建容器            ApplicationContext ap = new ClassPathXmlApplicationContext("com/spring/test/b_create/applicationContext.xml");            // 从容器中获取对象             User bean = (User) ap.getBean("user2");             System.out.println(bean);        }        3)实例工厂配置方式            <!-- 实例工厂的配置                调用UserFactory对象的createUser2的方法创建名称为User3的对象。放入容器             -->            <bean name="userFacotry" class="com.spring.test.b_create.UserFactory">            </bean>            <bean name="user3" factory-bean="userFacotry" factory-method="CreateUser2">            </bean>        测试代码:                        @Test            // 创建方式二、动态工厂方式创建user对象            public void test3() {                // 创建容器                ApplicationContext ap = new ClassPathXmlApplicationContext("com/spring/test/b_create/applicationContext.xml");                // 从容器中获取对象                User bean = (User) ap.getBean("user3");                System.out.println(bean);            }        c.bean元素的scope属性:            1)singleton(默认值)                单例对象:被标识为单例对象,在spring只会存在一个实例。                    <bean name="user" class="com.spring.bean.User" scope="singleton">                    </bean>                    测试代码:                        @Test// bean的scope属性为scope 表示单例的。所以会打印出true

public void test4() {
// 创建容器
// ClassPathXmlApplicationContext(“classpath:applicationContext.xml”);
ApplicationContext ap = new ClassPathXmlApplicationContext(“com/spring/test/b_create/applicationContext.xml”);
// 从容器中获取对象
User bean = (User) ap.getBean(“user”);
User bean2 = (User) ap.getBean(“user”);
User bean3 = (User) ap.getBean(“user”);
User bean4 = (User) ap.getBean(“user”);
System.out.println(bean==bean2);
}
2)prototype
多例原型:被标识为多例的对象,每次再获得才会创建并且每次创建都是新的对象
User bean = (User) ap.getBean(“user”);
User bean2 = (User) ap.getBean(“user”);
User bean3 = (User) ap.getBean(“user”);
User bean4 = (User) ap.getBean(“user”);
System.out.println(bean==bean2);
打印输出false
在整合struts时候要使用该属性值。

            3)request(了解)                request请求生命周期一致。            4)session(了解)            web环境下,对象与session对象一致

spring的生命周期 ##

配置一个方法作为生命周期的初始化方法,spring会在对象创建之后立即调用配置一个方法作为生命周期的销毁方法,

        inint       spring容器关闭之前会调用。        destroy    <bean name="user" class="com.spring.bean.User" init-method="init" destroy-method="destroy">    </bean>

spring的分模块配置

在spring配置文件中引入其他spring配置文件。

在以后的开发中需要许多配置文件,我们可以通过一个住配置文件通过 标签来引用其他标签,便于更改,提高效率

spring的属性注入

1)        set方法注入                        <!-- set方法的属性注入 -->                <bean name="user" class="com.spring.bean.User">                    <!-- 为user对象中的name属性注入Tom的值 -->                    <property name="name" value="Tom">                    </property>                    <!-- 为user对象中的age属性注入21的值 -->                    <property name="age" value="21">                    </property>                    <!-- 引用类型使用ref属性来引用其他的bean -->                    <property name="car" ref="car"></property>                </bean>                <bean name="car" class="com.spring.bean.Car">                    <property name="name" value="audi"></property>                    <property name="color" value="red"></property>                </bean>    2)        构造函数注入            name属性  构造函数的参数名那个            index属性:构造函数的参数索引从0开始            type属性:构造函数的参数类型    <bean name="user1" class="com.spring.bean.User">        <constructor-arg name="name" value="魏殿伦"></constructor-arg>        <constructor-arg name="age" value="22"></constructor-arg>        <constructor-arg name="car" ref="car"></constructor-arg>    </bean>    3)        p名称空间注入(其实还是走set方法)                <!-- p名称空间的注入方式 1.首先要导入命名空间-->        2.导入p命名空间 xmlns:p=http://www.springframework.org/schema/p        3.使用p:属性完成注入            |-值类型:p:属性名称="值"            |-对应类型:p:属性名-ref="bean名称"                <bean name="user3" class="com.spring.bean.User" p:name="jack" p:age="20" p:car-ref="car"></bean>    4)        spel注入<!-- spel方式的注入:spring Expression Language spring表达式语言     注意spel注入方式针对的是值类型 对于对象类型  我们可以采取  ref=“对象的name名称”-->    <bean name="user4" class="com.spring.bean.User">        <!-- 下边就是寻找name属性为user的bean 的naem属性值 -->        <property name="name" value="#{user.name}"></property>        <property name="age" value="#{user3.age}"></property>        <property name="car" ref="car"></property>    </bean>8.复杂类型注入Object []   List   Map   Property  四种类型复杂注入                    <!-- 复杂数据类型注入 -->            <!-- array属性注入 -->            <bean name="cb" class="com.spring.test.inject.Collection">                <!-- 如果数组中只准备注入一个值 ,那么可以直接使用value属性注入即可 <property name="arr" value="tom"></property> -->                <!-- 多元素的注入 -->                <property name="arr">                    <array>                        <value>tom</value>                        <value>jerry</value>                        <ref bean="user4" />                    </array>                </property>                <!-- <property name="list" value="单个list"> </property> -->                <!-- list集合里有多个内容 -->                <property name="list">                    <list>                        <value>list1</value>                        <value>list12</value>                        <ref bean="user4" />                    </list>                </property>                <!-- map类型的注入 -->                <property name="map">                    <map>                        <entry key="url" value="jdbc:mysql://localhost"></entry>                        <entry key="driver" value="com.mysql.jdbc.Driver"></entry>                        <!-- <entry key="user" value-ref="user4"></entry> -->                        <entry key-ref="user3" value-ref="user4"></entry>                    </map>                </property>                <!-- properties -->                <property name="pro">                    <props>                        <prop key="driverClass">com.jdbc.mysql.Driver</prop>                        <prop key="userName">root</prop>                        <prop key="password">123456</prop>                    </props>                </property>            </bean>

将spring容器应用到struts_crm项目中

目标:管理service对象机器Dao对象
步骤
1)导包4+2(本机测试Spring) +1(webjar包)
2)将service对象以及Dao对象配置到spring配置容器 a、编写配置文件 applicationContext.xml文件
配置Dao


<bean name="customerDao" class="cn.itheima.dao.impl.CustomerDaoImpl">
</bean>
<!-- 配置linkmandao -->
<bean name="linkManDao" class="cn.itheima.dao.impl.LinkManDaoImpl">
</bean>
<!-- 配置usersDao -->
<bean name="userDao" class="cn.itheima.dao.impl.UserDaoImpl"></bean>
<!-- 配置service dao -->
<bean name="linkManService" class="cn.itheima.service.impl.LinkManServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
<bean name="linkManService" class="cn.itheima.service.impl.LinkManServiceImpl">
<property name="customerDao" ref="customerDao"></property>
<property name="linkManDao" ref="linkManDao"></property>
</bean>
<bean name="userService" class="cn.itheima.service.impl.UserServiceImpl">
<property name="ud" ref="userDao"></property>
</bean>
3).在Action中获取容器中的service对象
4). // 创建容器
ApplicationContext ac = new ClassPathXmlApplicationContext(“classpath:ApplicationContext.xml”);
/// 获取cs(customerService对象)
CustomerService cs = (CustomerService) ac.getBean(“customerService”);
在实际项目中这样管理AppliactionContext是不合理的。严格来说这是错误的因为比较麻烦,如果这样写 每个
Action类都写如上代码。
5)是的容器随着项目的启动而启动


org.springframework.web.context.ContextLoaderListener



contextConfigLocation
classpath:applicationContext.xml

6)我们可以从启动后的spring容器中,获取在Application对象
// 获取spring容器 —>从Application域获取
// 1.获取ServletContext对象
ServletContext sc = ServletActionContext.getServletContext();
// 2.从sc中获取ac容器
WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
// 3.从容器中获取相应的bean对象
CustomerService bean = (CustomerService) ac.getBean(“customerService”);

总结

总结:
spring_day01

1.spring介绍
spring并不局限于某一层.
spring是对象的容器,帮我们”管理”项目中的所有对象

2.spring搭建
1>导包 4+2
2>准备类
3>书写配置(src/applicationContext.xml)
4>书写代码测试

3.spring中的概念
ioc: 反转控制. 创建对象的方式反转了.从我们自己创建对象,反转给spring(程序)来创建.

di: 依赖注入.将必须的属性注入到对象当中.是实现ioc思想必须条件.beanFactory与ApplicationContext

4.配置文件详解

bean元素    id: 给Bean起个名字       不能重复,不能使用特殊字符.早期属性.    name:给Bean起个名字      能重复,能使用特殊字符.后来属性.    class:类的完整类名生命周期属性    init-method     指出初始化方法    destory-method  指出销毁方法作用范围    scope:         singleton(默认值):单例.创建容器时会立即创建单例对象        prototype :多例.每次获得对象时,才会创建对象,并且每次都会创建新的对象分模块开发    <import  />

5.Bean的创建方式
*空参构造创建
静态工厂
实例工厂

6.注入方式
*set方法
*构造方法
p名称空间
spEL表达式

7.复杂属性注入
Array
List
Map
Properties
8.在WEB环境中使用Spring容器
1>导包 4+2+1(spring-web)
2>在web.xml中配置listener => ContextLoaderListener
|-配置参数,指定spring配置路径
3>在Action中,使用工具类获得容器.
|-WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)

0 0
原创粉丝点击