Spring框架知识(1)

一、Spring概念:

Spring是一个控制反转(IOC)和面向切面的(AOP)的轻量级框架。

二、SpringIOC创建对象的三种方式:

1、通过构造器方式

①无参数构造器(创建一个没有初始化数据的对象)
②有参 数构造器(创建一个带有初始化数据的对象)
标签:< constructor-arg >

 <!--创建student的bean对象-->
    <!--构造器方式-->
    <!--
    无参构造器 
    特点:Spring容器默认使用无参构造方式创建对象
    使用:在配置文件中直接使用bean标签配置即可,无需过多声明
    -->
    <bean id="stu" class="com.xxx.pojo.Student"></bean>
    <!--有参数的构造器
    特点:Spring容器对根据配置调用的有参构造器创建一个带有初始化数据的对象
    使用:constructor-arg:使用bean的字标签来声明调用的构造器的形参的个数
    一个字标签表示一个参数
    属性:index:参数的下标
          type:参数的类型,全限定路径
          name:参数的形参名
          value:参数要给的值
    -->
     <bean id="stu2" class="com.xxx.pojo.Student">
        <constructor-arg index="0" type="java.lang.Integer" name="sid" value="1"></constructor-arg>
        <constructor-arg index="1" type="java.lang.String" name="sname" value="张三"></constructor-arg>
    </bean>

代码示例:

package com.bjsxt.controller;

import com.bjsxt.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author 8690
 * @date 2021/6/26 15:16
 */
public class testStu {
    
    public static void main(String[] args) {
        //创建容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
        //获取容器中的对象(创建容器的时候就已经创建好了对象。)
        //无参构造器方式
        Student student = (Student) ac.getBean("stu");
        System.out.println("无参构造:"+student);
        
        //有参构造器
        Student student1= (Student) ac.getBean("stu2");
        System.out.println("有参构造:"+student1);
        
    }
}
2、通过属性注入(get/set)

先通过空构造器创建一个对象,然后再使用set方法进行初始化赋值.
标签: < property >

<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">
    <!--创建student的bean对象-->
    <!--
    属性注入方式
    特点:相当于创建一个空对象然后使用set方法赋值
    使用:
    property:在bean标签下使用子标签property,表示调用set方法给某个属性赋值
    属性:name:要赋值的属性名
          value:值
    -->
    <bean id="stu3" class="com.bjsxt.pojo.Student">
        <property name="sid" value="2"></property>
        <property name="sname" value="李四"></property>
    </bean>
</beans>
3、工厂模式

    我们在使用Java代码处理某个问题的时候,需要创建A对象,调用 A对象中的某个方法,但是A对象的创建依赖B对象,而B对象的 创建又依赖于C对象,C对象的创建又依赖于D对象…,如下:
D d=new D();
C c=new C(d);
B b=new B©;
A a=new A(b);
这样造成,代码的阅读性极差

解决:
将对象的创建过程进行封装,直接返回创建好的对象使用.

实现:
工厂设计模式
本质:就是封装对象的创建过程的一种代码的编程思想
静态工厂:生产对象的方法是静态方法

public class AFactory{
public static A newInstance(){
D d=new D();
C c=new C(d)
B b=new B(c);
A a=new A(b);
return a;
}
}
动态工厂:生产对象的方法是非静态方法
public class AFactory{
public A newInstance(){
D d=new D();
C c=new C(d)
B b=new B(c);
A a=new A(b);
return a;
}
}

SpringIOC使用工厂创建对象:
①动态工厂模式
②静态工厂模式
applicationcontext.xml配置示例:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!--创建student的bean对象-->
    <!--工厂设计模式-->
    <!--动态工厂-->
        <bean id="factory" class="com.xxx.pojo.StudentFactory"></bean>
        <!--生产Student对象-->
        <bean id="stu4" factory-bean="factory" factory-method="newIntance"></bean>
    <!--静态工厂-->
    <!--可以理解为静态方法直接用类名调用-->
        <bean id="stu5" class="com.xxx.pojo.StudentFactory2" factory-method="newIntance"></bean>
</beans>

TestObject示例:

package com.bjsxt.controller;

import com.bjsxt.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author 8690
 * @date 2021/6/26 15:16
 */
public class testStu {
    
    public static void main(String[] args) {
        //创建容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
        //获取容器中的对象
        //工厂设计模式     
            //动态工厂
            Student student = (Student) ac.getBean("stu4");
            System.out.println("动态工厂:"+student);
            //静态工厂
            Student student1 = (Student) ac.getBean("stu5");
            System.out.println("静态工厂:"+student1);
    }
}

三、IOC的依赖注入DI

1、依赖注入DI的介绍

对象中有引用类型的属性,比如A对象中有属性B,我希望从Spring容器中获取一个B属性有值的A对象,怎么办?
也就是A类中是个属性是B,需要创建B的对象赋值给A中的B属性。
标签< ref >

2、DI依赖注入的代码实现

<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--
DI依赖的使用流程
①将依赖责任链上的所有的对象都配置为bean
②根据依赖关系完成对象之间的组装配置
     通过构造器方式:
         i.必须在类中声明对应的构造器
         ii.在bean标签下使用constructor-arg子标签完成以来注入
               使用constructor-arg的属性ref,ref的值为要注入的bean的ID
     通过set方法方式
          i.必须在类中声明引用属性对应的set方法
          ii.在bean标签下使用property子标签完成以来注入
               在property子标签中使用ref属性,属性值为要被注入的bean的ID
-->
    <!--配置学生bean对象-->
    <bean id="stu" class="com.xxx.pojo.Student">
        <!--构造器方式-->
        <!--<constructor-arg index="0" name="teacher" type="com.xxx.pojo.Teacher" ref="tea" ></constructor-arg>-->
        <!--set方式-->
        <property name="teacher" ref="tea"></property>
        <property name="sname" value="张三"></property>
        <property name="sid" value="1"></property>
    </bean>
    <bean id="tea" class="com.xxx.pojo.Teacher">
        <property name="tid" value="2"></property>
        <property name="tname" value="刘老师"></property>
    </bean>
</beans>

TestIocDI代码示例:

package com.bjsxt.contorller;

import com.bjsxt.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author wuyw2020
 * @date 2020/1/8 20:04
 */
public class TestIocDI {
    public static void main(String[] args) {
        //获取spring对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
        Student stu = (Student) ac.getBean("stu");
        System.out.println(stu);
        stu.testStu();

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值