Srping框架搭建

2 篇文章 0 订阅
1 篇文章 0 订阅

1.导包(导入依赖)

 

 

spring 依赖的结构图

spring

导入核心包

<properties>
    <spring.version>5.0.2.RELEASE</spring.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>

 

2.书写配置文件

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"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

3.准备一个类

public class User {
    private String username;
    private Integer uid;
    private Integer age;
    private Double balance;

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", uid=" + uid +
                ", age=" + age +
                ", balance=" + balance +
                '}';
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getBalance() {
        return balance;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }
}

4. 把创建好的类交给spring管理(在spring的配置文件中配置这个类)

<bean name="user" class="com.hd.bean.User"></bean>

5.书写测试代码

让spring帮我们new 出来一个对象

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) applicationContext.getBean("user");
System.out.println(user);

ClassPathXmlApplicationContext是spring框架的工厂类

 

 

BeanFactory 这个类是spring框架在最开始用的类,

现在用的是 FileSystemApplicatonContext (使用配置的绝对路径读取) ClassPathXmlApplicationContext(使用配置文件的相对路径)

BeanFactory适用于资源匮乏的时代,当使用对象的时候spring容器才帮助你创建一个对象

File… 和Class… 适用于资源富余的时代,当读取配置文件的时候,一次性将spring容器中的所有对象全部创建,随时取随时用。他的效率要比ApplicationContext高

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//该行代码执行完毕后,spring容器中的所有对象都被创建
如果使用的是ApplicationContext 则不会创建直到使用该对象时才会创建

 Spring配置文件详解

<!--
    name:对象起名字 (可以重复,)
    id:   (id唯一)
    class: 类的完整路径名
    scope: 范围  singleton 单例
                 prototype 多例
                 单例和多例:单例占用空间少,速度快,线程不安全
                 多例占用空间大,速度慢,线程安全
                  单例是默认的,只有在Action使用多例
    init-method: 在创建对象后调用(是创建前可能会配置一些信息)
    destroy-method : 在销毁对象前调用(为了安全,防止变量意外注销导致整个系统崩溃)
    如果想要测试destroy-method  要手动调用close方法
-->
<bean  name="user" class="com.hd.bean.User" scope="singleton" init-method="before" destroy-method="after"></bean>

<!--创建对象的方式-->
<!--默认的是空参的构造函数 (最常用的方式)-->
<!--使用静态工厂来创建对象 了解-->
<bean name="staticUserFactory" class="com.hd.B_create.StaticFactory" factory-method="createUser"></bean>
<!--使用动态工厂来创建对象 了解-->
<bean name="userFactory" class="com.hd.B_create.UserFactory" ></bean>
<bean name="user2" factory-bean="userFactory" factory-method="createUser"></bean>

属性注入

set方法的注入

<!--set 注入
    在类中必须有set方法 名字固定
    bean中间书写property标签 name对象属性名   
                            value对应值(8大数据类型及其包装类String)
                            ref 值(对象)

-->
    <bean name="computer" class="com.hd.C_di.Computer">
        <property name="color" value="黑色"></property>
        <property name="type" value="Think Pad"></property>
    </bean>
    <bean name="student" class="com.hd.C_di.Student">
        <property name="name" value="李欢"></property>
        <property name="age" value="19"></property>
        <property name="computer" ref="computer"></property>
    </bean>

构造函数的注入

<!--有参构造函数的注入-->
<!--name (构造函数中的参数名称) index(构造函数中参数的下标位置)  type(构造函数中参数的类型) value  ref  -->

<bean name="student1" class="com.hd.C_di.Student" >
    <constructor-arg name="name2" value="仉宏志"  index="0" type="java.lang.String"></constructor-arg>
    <constructor-arg name="age1" value=""></constructor-arg>
    <constructor-arg name="addr" value="asdf"></constructor-arg>
</bean>

p(propertity的缩写)空间的注入

<bean name="p_student" class="com.hd.C_di.Student" p:age="23" p:name="李琼辉" p:computer-ref="computer">
</bean>

spel的注入 适用于已知的对象输入注入

<!--spel的注入-->
<bean name="spel_student" class="com.hd.C_di.Student">
    <property name="name" value="#{student.name}"></property>
</bean>

复杂类型的注入

数组

<property name="scores">
    <array>
        <value>85</value>
        <value>70</value>
        <value>90</value>
        <value>65</value>
    </array>
</property>

list

<property name="list">
    <list>
        <ref bean="student"></ref>
        <ref bean="p_student"></ref>
    </list>
</property>

set

<property name="set">    <set>        <ref bean="p_student"></ref>        <value>hello</value>    </set></property>

map

<property name="map">
    <map>
        <entry key="name" value="胡井号"></entry>
        <entry key="student" value-ref="student"></entry>
    </map>
</property>

 

properties

<property name="properties" >
    <props>
        <prop key="user">root</prop>
    </props>
</property>

数组  list  set properties 如果只有值,就可以按照原有的方式注入

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值