Spring~Spring的创建和使用(如何将对象储存到Spring、如何将对象从Spring中读取出来)

创建Spring

打开idea,创建一个maven文件,然后点击next,finsh.
在这里插入图片描述
然后File,打开设置Settings,搜索maven,勾中右下角的两个Override,然后检查对应目录下是否有settings.xml文件,如果有,检查是否为国内源,如果没有,将下面的xml复制过去。
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <pluginGroups>
  </pluginGroups>
  <proxies>    
  </proxies>
  <servers> 
    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>

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

 
  <profiles>
  
  </profiles>
</settings>

在新项目的pom.xml文件中,引入下列依赖:spring的核心jar包

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

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

如下:
在这里插入图片描述
在resources文件中,创建一个新的file,后缀名为xml
在这里插入图片描述
然后在其中写入基础的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>

这样spring文件的创建就完成了。

可以查看左下角是否有引入的jar包,如果有,那么就算创建完成
在这里插入图片描述

将对象储存到Spring

这里没有具体的业务代码,我们只是为了熟悉流程,所以创建一个测试类App,然后创建UserBean作为我们放入Spring的对象,里面可以写一些属性和方法。
在这里插入图片描述
在spring-config文件中,将对象存入Spring中:

<beans>
   //bean表示符 后面读取对象时会用到,class中是保存到spring中的对象。
 
   //如果想要放入多个对象,就创建多个bean标签,beans即bean对象列表
    <bean id="user" class="model.UserBean"></bean>
</beans>

从Spring中读取对象

先获取上下文对象,然后通过上下文对象获取到业务对象。

获取上下文对象

方式一:ApplicationContext

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

这里的ClassPathXmlApplicationContextApplicationContext的子类,拥有ApplicationContext的所有功能,可以通过xml配置来获取所有的Bean容器.
方式二:BeanFactory

BeanFactory beanFactory =new XmlBeanFactory(new ClassPathResource("spring-config.xml"));

BeanFactory beanFACTory=new ClassPathXmlApplicationContext("spring-config.xml");       

获取业务对象

方式一:通过bean对象的id获取

 context.getBean("user") 
 //这里的user是与spring-congfig.xml中的bean的id是对应的。

用UserBean接受,需要强制转换。

UserBean userBean=(UserBean) context.getBean("user");

方式二:通过类型获取

context.getBean("UserBean.class")

通过类型来获取业务对象:
优点:可以避免类型转换
缺点:当有多个bean对象是同一类型时,会报错。
在这里插入图片描述
方式三:通过id+类型获取

context.getBean("users",UserBean.class)
  UserBean userBean1= context.getBean("users",UserBean.class);

第三种方式无需类型转换,而且在多个bean对象是同一类型的时候也不会报错。

关于ApplicationContext和BeanFactory的联系与区别

  • 二者来自不同的包
    BeanFactory来自Spring.beans.jar包,而ApplicationContext来自于spring.context.jar包下。

  • BeanFactory和ApplicationContext都是Spring下的顶级接口,ApplicationContext是BeanFactory 的子类,BeanFactory提供了基础的访问容器的能力,而ApplicationContext不仅拥有BeanFactory的功能,还有支持很多的特性,如对国际化的支持,支持资源的访问,支持事件的传播…

  • 从执行性能角度考虑,ApplicationContext是一次性加载并初始化所有的Bean,启动过程较慢,但后续执行较快,而BeanFactory是需要哪个类就加载哪个类,因此更加轻量,启动的更快,但后续执行可能会慢一些。

猜你喜欢

转载自blog.csdn.net/Merciful_Lion/article/details/123996747