Spring_day01

来源:互联网 发布:如何查看手机mac地址 编辑:程序博客网 时间:2024/06/11 09:43

Spring概念

  • 开源的轻量级框架
  • 框架特征:
    • 面向切面编程(aop):扩展功能不是修改源代码实现
    • 控制反转(ioc):把对象的创建不是通过new方式实现,而是交给spring配置创建类对象
  • spring是一站式框架
    • web层:springMVC
    • service层:spring的ioc
    • dao层:spring的jdbcTemplate
  • spring版本
    • hibernate5.x
    • spring4.x
    • struts2

Spring的ioc操作

  • 把对象的创建交给spring管理
  • ioc操作两部分:
    • ioc的配置文件方式
    • ioc的注解方式

IOC底层原理

  • ioc底层原理使用技术
  • xml配置文件
  • dom4j解决xml
  • 工程设计模式
  • 反射

IOC入门案例

  • 第一步:导入jar包(Beans、core、Context、SpEL)
  • 第二步:创建类,在类里面创建方法
public class User {    public void add() {        System.out.println("add......");    }    public static void main(String[] args) {        // 原始做法        User user = new User();        user.add();    }}
  • 第三步:创建spring配置文件,配置创建类
    • spring核心配置文件建议放到src下面,建议名称applicationContext.xml
    • 引入schema约束
<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.xsd"></beans>
    <!-- ioc入门 -->    <bean id="user" class="com.ioc.User"></bean>
  • 第四步:测试对象创建
    @Test    public void testUser() {        // 1 加载spring配置文件,根据创建对象        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");        // 2 得到配置创建的对象        User user = (User) context.getBean("user");        System.out.println(user);        user.add();    }

配置文件提示

  • spring引入schema约束,把约束文件引入到eclipse
  • 复制约束路径:http://www.springframework.org/schema/beans/spring-beans.xsd

Spring的bean管理

bean实例化方式

  • 在spring里面通过配置文件创建对象
  • bean实例化三种方式
    • 第一种:使用类的无参数构造器创建(建议)
      注意:类里面没有无参数构造器,会出现异常
    • 第二种:使用静态工厂创建
      • 创建静态方法,返回类的对象
    <!-- 使用静态工程创建对象 -->    <bean id="bean2" class="com.bean.Bean2Factory" factory-method="getBean2"></bean>
    // 静态方法,返回Bean2对象    public static Bean2 getBean2() {        return new Bean2();    }    @Test    public void testBean2() {        // 1 加载spring配置文件,根据创建对象        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");        Bean2 bean2 = (Bean2) context.getBean("bean2");        System.out.println(bean2);    }
  • 第三种:使用实例工厂创建
    • 创建非静态方法,返回类的对象
    <!-- 使用试力工厂创建对象 -->    <!-- 创建工厂对象 -->    <bean id="bean3Facotry" class="com.bean.Bean3Factory"></bean>    <bean id="bean3" factory-bean="bean3Facotry" factory-method="getBean3"></bean>
    // 非静态方法,返回Bean2对象    public Bean3 getBean3() {        return new Bean3();    }    @Test    public void testBean2() {        // 1 加载spring配置文件,根据创建对象        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");        Bean3 bean3 = (Bean3) context.getBean("bean3");        System.out.println(bean3);    }

bean属性

  • id属性
    起名称,id属性值名称任意命名;不能包含特殊符号;根据id值得到配置对象
  • class属性
    创建对象所在类的全路径
  • name属性
    功能和id属性一样,name属性可以包含特殊符号
  • scope属性
    • singleton:默认值,单例(对象只创建一次)
        <bean id="user" class="com.ioc.User"></bean>        // 1 加载spring配置文件,根据创建对象        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");        // 2 得到配置创建的对象        User user1 = (User) context.getBean("user");        User user2 = (User) context.getBean("user");        System.out.println(user1);        System.out.println(user2);        // 输出结果:        // com.ioc.User@384ad17b           com.ioc.User@384ad17b
  • prototype:多例的
    <bean id="user" class="com.ioc.User" scope="prototype"></bean>
        // 2 得到配置创建的对象        User user1 = (User) context.getBean("user");        User user2 = (User) context.getBean("user");        System.out.println(user1);        System.out.println(user2);        // 输出结果:        // com.ioc.User@384ad17b           com.ioc.User@61862a7f
  • request:创建对象,把对象放到request域里面
  • session:创建对象,把对象放到session域里面
  • globalSession:创建对象,把对象放到globalSession里面

属性注入

  • 创建对象时候,向类里面属性设置值
  • 属性注入的方式介绍(spring里面没有接口注入)
    • set方法注入
    • 有参数构造注入
    • 接口注入
// set方法注入public class User{    private String name;    public User(String name) {        this.name = name;        }    }User user = new User("lucy");
// 有参数构造注入public class User{    private String name;    public User(String name) {        this.name = name;        }    }User user = new User("lucy");
// 接口注入public interface Dao {    public void delete(String name);    }public class DaoImpl implements Dao {    private String name;    public void  delete(String name) {        this.name = name;        }    }

spring-有参数构造注入

    <!-- 使用参数构造注入属性 -->    <bean id="demo" class="com.property.PropertyDemo1">        <!-- 使用有参构造注入 -->        <constructor-arg name="username" value="小王小马"></constructor-arg>    </bean>
public class PropertyDemo1 {    private String username;    public PropertyDemo1(String username) {        this.username = username;    }    public void test1() {        System.out.println("demo1......" + username);    }}
    @Test    public void testUser() {        // 1 加载spring配置文件,根据创建对象        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");        // 2 得到配置创建的对象        PropertyDemo1 demo1 = (PropertyDemo1) context.getBean("demo");        demo1.test1();    }

spring-set方法注入

    <!-- set方法注入属性 -->    <bean id="book" class="com.property.Book">        <!-- 注入属性值            name属性值:类里面定义的属性名称            value属性:设置具体的值 -->        <property name="bookName" value="易筋经"></property>    </bean>
public class Book {    private String bookName;    public void setBookName(String bookName) {        this.bookName = bookName;    }    public void demobook() {        System.out.println("book......." + bookName);    }}
    @Test    public void testUser() {        // 1 加载spring配置文件,根据创建对象        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");        // 2 得到配置创建的对象        Book book = (Book) context.getBean("book");        book.demobook();    }

注入对象类型属性

  • 创建service类和dao类
  • 具体实现过程
    • 在service里面把dao作为类型属性
    • 生成dao类型的set方法
    • 配置文件中完成注入
    <!-- 对象类型属性注入 -->    <!-- 配置service和dao对象 -->    <bean id="userDao" class="com.ioc.UserDao"></bean>    <bean id="userService" class="com.ioc.UserService">        <!-- 注入dao对象            name属性值:service类里面属性名称            ref属性:dao配置bean标签中id值 -->        <property name="userDao" ref="userDao"></property>    </bean>

P名称空间注入

  • 引入P名称空间
<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"    xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  • 创建类
  • 配置文件
    <!-- P名称空间注入 -->    <bean id="person" class="com.property.Person" p:pname="lucy"></bean>

注入复杂类型

  • 数组
  • list集合
  • map集合
  • properties类型
    private String[] arrs;    private List<String> list;    private Map<String, String> map;    private Properties properties;
    <bean id="person" class="com.property.Person">        <property name="arrs">            <list>                <value>小王</value>                <value>小马</value>                <value>小宋</value>            </list>        </property>        <property name="list">            <list>                <value>小奥</value>                <value>小金</value>                <value>小普</value>            </list>        </property>        <property name="map">            <map>                <entry key="aa" value="lucy"></entry>                <entry key="bb" value="mary"></entry>                <entry key="cc" value="tom"></entry>            </map>        </property>        <property name="properties">            <props>                <prop key="driverclass">com.mysql.jdbc.Driver</prop>                <prop key="username">root</prop>            </props>        </property>    </bean>

IOC和DI区别

  • IOC:控制反转,把对象创建交给spring进行配置
  • DI:依赖注入,向类里面的属性中设置值
  • 两者之间的关系:
    依赖注入不能单独存在,西药在ioc基础之上完成操作

Spring整合web项目原理

  • 加载spring核心配置文件:ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
    new对象,功能可以实现,效率低
  • 实现思想:把加载配置文件和创建对象过程,在服务器启动时候完成
  • 实现原理
    • ServletContext对象
    • 监听器:监听对象何时创建
    • 具体使用:
      • 在服务器启动时候,为每个项目创建一个ServletContext对象
      • 在ServletContext对象创建时候,使用监听器可以具体到ServletContext对象在什么时候创建
      • 使用监听器监听到ServletContext对象创建时候
        • 加载spring配置文件,把配置文件配置对象创建
        • 把创建出来的对象放到ServletContext域对象里面(setAttribute方法)
      • 获取对象时候,到ServletContext域得到(getAttribute方法)
原创粉丝点击