Spring

Spring成长之路【一】:创建Spring项目 - 知乎 (zhihu.com)

IDEA2021新建第一个Spring项目(使用两种方法)_idea创建spring项目-CSDN博客

〇)Spring项目的搭建

        1新建一个Maven项目

        2在pom.xml中添加依赖

引入spring-context依赖Maven会自动引入springbasic的其他依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

         3新建Spring的配置文件(在src根目录下或者放在resources根目录下

<?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">

    <bean id="helloWorld" class="HelloWorld" scope="prototype">
        <property name="name" value="Hello World!" />
    </bean>
</beans>

一)Spring 简介

        Spring是一个容器框架()

        IOC:

                控制反转,由spring框架来创建对象;使用Bean对象时DI依赖注入,DI一定是在IOC的基础上完成。

                IOC利用java的反射机制:

                        1.获取class的方式 

                                 类名.class  

                                对象.getClass(),

                                Class.forName("全限定类名")

                IOC的原理:

                        1.读取spring的配置文件,id=stu ,class="com.zz.bean.Student"

                        2.读取Student的Class, Class.forName("com.zz.bean.Student")

                        3.使用默认无参构造器实例化对象Student newInstance()

                        4.容器 <k,v> <"stu",Student>

        AOP:

                面向切面编程,在不修改源代码情况下扩展系统功能,使用动态代理(jdk和cglib)

二) SpringBean 实例化方式:

     1  默认使用无参的构造器实例化Bean对象 

     2  使用静态化工厂 获取Student类对象

     3  使用实例化工厂,获取工厂对象后获取Student类对象

                静态化工厂直接调用类方法创建学生类对象

                实例化工厂需要先创建工厂对象,工厂对象调用方法返回值生成学生类对象

<bean id="stu" class="com.xja.bean.Studnet"> </bean>

<bean id="stu2" class="com.xja.bean.StudentFactory" factory-method="getStudent"></bean>

<bean id="stuFactory" class="com.xja.bean.StudentFactoryIsNotStatic"></bean>

<bean id="stu3" factory-bean="stuFactory" factory-method="getStudent"> </bean>

 


public class Student{

    public String stuName = "zz"

  //使用IOC默认使用无参构造器创建对象,没有无参构造器会报错      
 //   public Student(String stuName){
 //       this.stuName = stuName;
 //   }

   public Student(){
        System.out.println("student被创建。。。")
    }

    public void init(){
        System.out.println("init被创建。。。")
    }
    public void destory(){
        System.out.println("destory被创建。。。")
    }

}

   

public class StudentFactory{
    public static Student getStudent(){
        return new Student();
    }
}
public class StudentFactory2{

    public Student getStudent(){
        return new Student();
    }
}

   

三)SpringBean对象查找的方式

//加载spring的配置文件
ApplicationContext context =  new ClassPathXmlApplicationContext("applicationContext.xml");

Student stu1 =  (Student)context.getBean("stu1")

Student stu2 = (Student)context.getBean("stu2")

Student stu3 = (Student)context.getBean("stu3")

Student bean = context.getBean("stu",Student.class);

四)SpringBean对象的作用域scope

          singleton:默认单例模式 (多次创建的对象完全一样)

           prototype:多例,每一次获取返回的都是最新的bean对象

            在web应用程序中scope:request,session,globleSession

<bean id="stu" class="com.xja.bean.Student" scope="singleton"></bean>

五)SpringBean对象生命周期

                web应用自动调用:对象的创建,初始化init-method,使用,销毁destory-method

                在web应用程序中destory方法才可以被显式调用

               在测试类中只能手动调用destory方法。

        

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值