美文网首页Java
Spring 项目的创建以及Bean对象的存取

Spring 项目的创建以及Bean对象的存取

作者: 喝杯Java润润BUG | 来源:发表于2023-04-19 10:27 被阅读0次

1.创建 Spring 项目

这里创建项目的环境:IDEA 2021.3.1(2021.X版本创建过程应该是相同的,如果是2022及以上IDEA其页面可能不同)、jdk1.8、spring-context 5.x。

1.1 创建一个Maven项目

打开IDEA、新建项目:


注意这里选jdk1.8版本的,没有jdk1.8可以下载: Download JDK。

然后next。

之后Finish。加载成功后如下:

1.2 添加 Spring 依赖

由于依赖或资源加载时候是从国外服务器下载的,速度很慢,所以可以配置一下国内源。

1.2.1 配置国内源

可能有的读者在如下路径没有settings.xml文件,这是第一种情况。第二种情况是默认存在,但是我们需要修改里面的内容。

① 对于第一种情况:先在C:\Users\这里是你的用户名.m2\下创建一个名为settings.xml的文件,将下面的代码全部复制进去。

② 对于第二种情况:现在是C:\Users\这里是你的用户名.m2\下默认有一个settings.xml文件,现在打开这个文件。(可以用记事本,但更推荐VSCode)

找到 <mirrors>

将下面代码复制进去(上面图片我已经复制了):

   <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>        
    </mirror>

注意!我们还需要再一次设置,上面是全局的设置,下面的是当前项目的设置。

最后点ok就行了。

1.2.2 添加 Spring 框架⽀持

在项⽬的 pom.xml 中添加 Spring 框架的支持,xml 配置如下(注意这里的版本必须是5.X版本的,可以去Maven Repository: Search/Browse/Explore (mvnrepository.com)网站,搜索context):

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

再点击右边的Maven,点刷新,等待加载依赖完成,出现了Dependencies就加载成功了。

1.2.2 添加启动类

在src\main\java下右键创建一个类。


2.存储 Bean 对象

2.1 创建 Bean 对象

Spring是什么?IoC是什么?DI又是什么? - 掘金 (juejin.cn)

在Java中,Bean是指一种Java类,它有一些属性和方法,并且符合一定的命名规范,能够被复用和组合。也可以这么理解:在Java中一个对象如果被使用多次,就可以称为 Bean。

Spring 可以存取对象,怎么存呢?我们先创建一个Bean对象:

创建完成后,我们还需要配置一下环境才能把Bean对象存入容器中。

2.2 将 Bean 存到容器中

在resources下创建一个配置文件,可以随便起名,但是最好要望文生义,这里取名为 spring-config.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">
    
</beans>

下面的内容很重要!要想把Student存入Spring容器中,我们需要在 spring-config.xml 配置文件中添加如下形式的标签:

<beans> 
    
    
    <bean id = "" class = "" >
            
</beans>

其中id标识这个类在容器中的名字,以后我们取它的时候就需要这个名字。class表示这个类的路径,注意这个路径是从java下开始。比如:我现在创建几个包,然后将Student放入。

那么这时候的class就为com.spring.demo1.Student。

3.获取、使用 Bean 对象

3.1 创建 Spring 上下文

在经过了繁杂的配置后,我们终于到了这里,现在开始就可以敲代码了。我们已经将Student注册到Spring容器中了,那我们如何将对象取出来用呢?要想取出来,我们得先有一个上下文,可以理解为就是一个Spring。

这里创建上下文有两种方法:

① 第一种 用ApplicationContext:

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

public class AppTest {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        
    }
}

② 第二种 用BeanFactory:

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class AppTest {
    public static void main(String[] args) {
        
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
    }
}

这里要注意的是 "spring-config.xml" 这里面必须是刚才的配置文件的名字!这里是从spring-config.xml中读取有哪些类要存到Spring中。

3.2 获取指定的 Bean 对象

获取Bean对象有三种方式:

public class AppTest {
    public static void main(String[] args) {

        //创建一个 Spring
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

        //第一种:形参为id,用 id 获取对象,取出 Student
        //但是这种方式必须要进行强制类型转换,因为这种getBean方法 的返回类型是 Object。
        Student student = (Student) context.getBean("student");
        student.Hi();

        //第二种:形参为 class 类型,用类型获取对象,这种形式不需要进行强制类型转换。
        Student student1 = context.getBean(Student.class);
        student1.Hi();
        
        //第三种:形参为 id + class 类型,这是最推荐的写法。
        Student student2 = context.getBean("student",Student.class);
        student2.Hi();
    }
}

结果:

当spring-config.xml有相同id时,第一行代码就会报错,Spring创建失败;对于第二种方式,当spring-config.xml里有多个相同类型的类的时候会报错;

3.3 ApplicationContext 与 BeanFactory 的区别

为了更清楚地描述它们的区别,我们这里多创建几个Bean类,并为每个类写一个构造方法,并把它们存入到Spring中:

① 对于ApplicationContext:

public class AppTest {
    public static void main(String[] args) {

        //创建一个 Spring
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

        Student student = (Student) context.getBean("student",Student.class);
        student.Hi();
    }
}

结果:

② 对于BeanFactory:

public class AppTest {
    public static void main(String[] args) {

        //创建一个 Spring
        BeanFactory context = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));

        Student student = (Student) context.getBean("student",Student.class);
        student.Hi();
    }
}

可以看出来BeanFactory 在应用启动时并不会初始化所有的 Bean,而是在第一次请求时才会初始化。而 ApplicationContext 在启动时就会初始化所有的 Bean。这是它们的最主要的区别。

其它区别(了解):ApplicationContext接口继承了BeanFactory接口,它除了继承了 BeanFactory 的所有功能之外,它还拥有独特的特性,还添加了对国际化⽀持、资源访问⽀持、以及事件传播等⽅⾯的⽀持。

当然,存取 Bean 的方式方法还有更简单更常用的,上面的只是给大家介绍一下 Spring 的基本操作。

`

相关文章

网友评论

    本文标题:Spring 项目的创建以及Bean对象的存取

    本文链接:https://www.haomeiwen.com/subject/nvtljdtx.html