spring 项目创建对象

本文介绍了如何建立一个简单的Spring项目,通过配置beans.xml文件实现对象的创建和管理。详细步骤包括创建Maven项目,添加Spring和Junit依赖,定义类和接口,以及编写测试类验证对象的创建。在Spring容器启动时,所有配置的bean会被实例化。通过ClassPathXmlApplicationContext获取并使用这些对象。

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

建立简单的spring项目,实现容器创建对象

只要由类就可以创建对象

  1. 新建maven项目:以maven-archetype-quickstart为模板删除多余的配置
  2. 加入spring依赖、junit依赖
  3. 创建类(含接口)
  4. 创建spring配置文件:beans.xml,声明类的信息,由spring创建和管理
  5. 测试类,测试声明对象

项目结构

在这里插入图片描述

pom文件

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.phy</groupId>
  <artifactId>quickstart</artifactId>
  <version>1.0-SNAPSHOT</version>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- spring 依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>

  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
    spring的配置文件
    1、beans:是跟标签,spring把Java对象称为bean
    2、spring-beans.xsd:是约束文件,和mybatis指定的dtd是一样的
-->
<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">
    <!--
        告诉spring声明对象
        id:对象的自定义名称,唯一值,spring通过这个名称找到对象
        class:类的全限定名称(不能是接口,因为spring通过反射机制创建对象,必须使用类)
        一个bean标签声明一个对象;

        通过该配置就完成 Dog dog = new Dog()的创建;
        并把创建好的对象放进到map中,该map是spring框架用来存放对象的
        相当于 map.put("dog","new Dog()")

    -->
    <bean id="dog" class="com.phy.service.impl.Dog"></bean>
    <!--
        spring创建一个非自定义的对象
    -->
    <bean id="mydata" class="java.util.Date"></bean>
</beans>

测试类

package com.phy.service.impl;

import com.phy.service.Animal;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DogTest {
    @Test
    public void run(){
        Animal dog = new Dog();
        dog.run();
    }

    //使用spring容器创建的对象
    @Test
    public void run1(){
        //指定bean配置文件
        String beanConfig = "beans.xml";
        //创建ApplicationContext spring容器对象,通过它获取dog对象
        //ClassPathXmlApplicationContext:表示从类路径中加载spring的配置文件,是ApplicationContext的子类
        ApplicationContext ac = new ClassPathXmlApplicationContext(beanConfig);
        //从容器中获取对象,getBean("配置文件中的id");
        Dog dog = (Dog) ac.getBean("dog");
        dog.run();

    }
}

对象的创建过程

  1. ClassPathXmlApplicationContext类读取bean配置文件,遇到bean标签根据class类名运用反射创建名为id的对象,并存到spring的map中
  2. ClassPathXmlApplicationContext类通过getBean(id)来获取创建的对象

spring默认创建对象的时间

在创建spring的容器时,会创建配置文件中所有的对象。

获取spring容器中Java对象的一些信息

 	@Test
    public void getInfo(){
        //指定bean配置文件
        String beanConfig = "beans.xml";
        //创建ApplicationContext spring容器对象,通过它获取dog对象
        //ClassPathXmlApplicationContext:表示从类路径中加载spring的配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext(beanConfig);
        //获得容器中定义对象的数量
        int beanDefinitionCount = ac.getBeanDefinitionCount();
        //获得容器中每个对象的名称
        String[] beanDefinitionNames = ac.getBeanDefinitionNames();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值