Spring框架入门操作

本文详细介绍了Spring框架的搭建步骤,包括Maven工程中导入核心jar包,创建配置文件及使用Spring容器获取对象的过程。通过实例演示了如何配置Bean并进行依赖注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学了几个框架之后,学习Spring框架的时候就很简单了,这篇博客先说一下spring框架的操作步骤,他的IOC、DI、AOP在以后的文章中再分享。

如何创建一个spring工程,请大家耐心操作。

1.导入jar包 

在一个maven工程中的pom.xml文件中导入 

spring-core   spring-beans  spring-context  commons-logging
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.8.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>

2.创建spring的配置文件

在resources包下创建.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mc="http://www.springframework.org/schema/mvc"
       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/mvc
		        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
</beans>

3.使用spring容器获取对象

创建一个实体类

package com.jie;

public class HelloWorld {
    private String name;
    private int age;
    private String address;
    public void sayHello(){
        System.out.println("Hello World!!!!");
    }

    public HelloWorld(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public HelloWorld(String name, int age,String address) {
        this.name = name;
        this.age = age;
        this.address=address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public HelloWorld() {
    }
}

在.xml文件中配置

<!--
id 当前类的名字  唯一的  不可重复
class  类的全限定名称
scorp  默认:singleton(单例模式)  prototype多例模式  request  session
-->
    <bean class="com.jie.HelloWorld" name="helloWorld2 world hello" id="helloWorld" scope="prototype"></bean>
    <bean id="hw" class="com.jie.HelloWorld" scope="prototype">
<!--
        property 给指定属性赋值
        第一种方法:通过setter方法注入
        DI依赖注入
        -->
        <!--<property name="name" value="浪子一秋"></property>-->
        <!--<property name="age" value="21"></property>-->
        <!--
        第二种方法:通过有参 !前提是要有有参构造函数
        index 通过参数的索引注入
        type  通过参数的类型注入
        -->
        <!--<constructor-arg index="0" value="一叶知秋"></constructor-arg>-->
        <!--<constructor-arg index="1" value="22"></constructor-arg>-->
        <!--经过测试,这个是按照类型顺序赋值-->
        <constructor-arg type="int" value="111"></constructor-arg>
        <constructor-arg type="java.lang.String" value="啧啧啧"></constructor-arg>
        <constructor-arg type="java.lang.String" value="郑州"></constructor-arg>
    </bean>

上面的两个bean配置的是同一个类,是为了后年的测试,里面的测试不再单独写了,不明白的再问

测试类

 /**
 * Spring IOC 控制反转  控制权的反向转移
 * 原来:谁用水创建  new 类名();
 * 现在:提前创建好所需要的对象,当使用时,不是自己创建,而是从容器中获取对象
 *
 * 使用步骤:
 * 1.导入jar包     spring-core   spring-beans  spring-context  commons-logging
 * 2.创建spring的配置文件
 * 3.使用spring容器获取对象
 *
 * 类  :单例模式  多例模式
 * 默认情况下是单例模式
 * 在配置文件中可以设置为其他模式
 *
 * DI依赖注入
 * 原始:通过有参构造直接给属性赋值 或者是通过无参+set方法赋值
 * 现在:spring容器中
 */

package com.jie;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestHello {
    @Test
    public void test1(){
        //谁使用,谁创建
        HelloWorld world=new HelloWorld();
        HelloWorld world1=new HelloWorld();
        System.out.println(world);//com.jie.HelloWorld@2077d4de
        System.out.println(world1);//com.jie.HelloWorld@7591083d
        //不同对象
        world.sayHello();
    }

    @Test
    public void test2(){
        //spring 从spring容器中获取对象
        //读取spring配置文件,创建bean工厂
        BeanFactory factory=new ClassPathXmlApplicationContext("/applicationContext.xml");
        //从bean工厂中获取bean对象
        //通过id查找
        HelloWorld world=(HelloWorld)factory.getBean("helloWorld");
        HelloWorld world1=(HelloWorld)factory.getBean("helloWorld");
        //通过类型得到对象
        //HelloWorld world2=factory.getBean(HelloWorld.class);
        //通过name查找
        HelloWorld world3=(HelloWorld)factory.getBean("helloWorld2");
        //通过id+类查找(官方推荐)
        HelloWorld world4=factory.getBean("hw",HelloWorld.class);
        System.out.println(world);//com.jie.HelloWorld@5a1c0542
        System.out.println(world1);//com.jie.HelloWorld@5a1c0542
        //同一个对象  推断为单例模式
        world.sayHello();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值