Spring入门简介 和bean 配置1

一,Spring 是什么?

Spring是一个开源框架,为简化企业级应用开发而成,使用Spring可以是简单的JavaBean实现以前只有EJB才能实现的功能,Spring是一个IOC(DI)和AOP 容器框架,总而言之 Spring就是一个容器,它里面封装了很多的对象,

 

 

二,具体描述Spring

  1. 轻量级:Spring是非侵入式的-基于Spring开发的应用中的对象可以不依赖Spring的API
  2. 依赖注入:(DI-Dependency injection、IOC)
  3. 面向切面编程:(AOP-aspect oriented programming)
  4. 容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期
  5. 框架:Spring实现了使用简单的组件配置组合成一个复杂的应用,在Spring中可以使用XML和java注解组合这些对象

一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring自身也提供了展现层的SpringMVC和持久层的Spring JDBC

三,搭建Spring的环境

 下载spring的jar包,现在spring的jar包,需要到github上面下载,百度即可

xmlns:p="http://www.springframework.org/schema/p"

 

1.创建一个实体类

package com.atguigu.spring.beans;

public class HelloWorld {

    private String name;
    
    public void setName(String name) {
        System.out.println("setName...");
        this.name = name;
    }
    
    public void hello(){
        System.out.println("Hello " + name);
    }
    
    
    public HelloWorld() {
        System.out.println("HelloWorld's construct...");
    }
    
    
}

2.编写applicationContext.xml文件(最好放在src目录xia)

这里通过xml文件的形式将对象方法spring这个容器中

里面的约束需要自己导入命名空间,jar包中有具体的百度即可很多

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    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
                       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <bean id="helloworld" class="com.atguigu.spring.beans.HelloWorld">
        <property name="name" value="spring"></property>
    </bean>

</beans>

3.书写测试类也就是说我们以前用这个对象的话,需要自己来new,但是用spring框架,他来帮助我们创建对象()

package com.atguigu.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        /*
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.setName("spring");
        helloWorld.hello();
        */
      //1.创建容器
        ApplicationContext ctx = new     ClassPathXmlApplicationContext("appliactionContext.xml");
    //2.从容器中获取bean
        HelloWorld hello = (HelloWorld) ctx.getBean("helloworld");
    //3.调用bean的方法
        hello.hello();
    }
}

测试成功

四,IOC &DI

IOC (inversion of Control );思想史反转资源获取方式,以前的时候我们需要创建对象来获取某种资源,例如在web层我们需要向service层请求D, xxxService xs = new xxxServiece();  xs.save(); 需要我们自己创建对象,但是现在我们可以向sprin容器中获取,DI (dependency injection) 依赖注入 ,就是给对象赋值,

2.在spring容器中配置bean

1.通过xml配置文件的方式  id 和name是相同的效果

2.spring容器 spring提供了两种基本类型的容器实现

Beanfactory 和 ApplicationContext  后者是BeanFactory 的子接口, 在以前的时候计算机内存比较小 ,都是用Beanfactory,但时候在计算机内存变大了,开发者都是使用ApplicationContext,BeanFactory 是每次需要使用的时候才会创建,,占用内存较少,ApplicationContext 可以在初始化的时候就创建; 他们之间的关系如下,beanfactory是最顶层的一个接口

ApplicationContext 主要的实现类有ClasspathXmlApplicationContext 从类路径加载配置文件

FileSystemXmlAppicationContext:从文件系统中加载配置文件;

4.2Bean元素进阶

scope属性 singleton 默认值, 被表示为单利的对象内存中只用一个,

prototype 多例模式  注意 actionBean 也就是在struts中action必须是多例模式

request:session对象 和web中一

4.3生命周期属性 初始化 init-method   destroy -method 销毁

<bean name = "user" class ="com.liangxin.bean.User" init -method ="init " destroy-method="destory">

</bean>

spring 创建对象的三种方式

第一种方式空参构造的方式

<bean name ='user" class="" ></bean>

第二种方式 静态工厂的方式

第三种方式实力工厂

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值