Spring学习(2)——写一个简单的spring项目

创建一个maven项目

创建项目,这里我们选择quickstart就行了
在这里插入图片描述

修改pom.xml文件

<build>标签中的代码先删掉,因为我们还用不到这些,然后导入spring依赖
(这里有个小bug,我想用当前最新版的5.3.6的Spring,但是maven似乎没找到这个版本的jar包,之后被迫改为了5.2.5的)

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

完整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</groupId>
  <artifactId>ch01-hello-spring</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.5.RELEASE</version>
    </dependency>

  </dependencies>

  <build>

  </build>
</project>

创建接口

定义一个简单的接口

public interface SomeService {
    void doSome();
}

选中接口名,按alt+Enter可以快速生成接口的实现类(IDEA的一个小技巧)
在这里插入图片描述

import com.service.SomeService;

public class SomeServiceImpl implements SomeService {
    @Override
    public void doSome() {
        System.out.println("执行了SomeServiceImpl的doSome方法");
    }
}

编写配置文件

在 resource 目录下创建 beans.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">

    <!--告诉spring创建对象
        声明bean,就是告诉spring要创建某个类的对象
        id:对象的自定义名称,唯一值。spring通过这个名称找到对象
        class:类的全限定名称(不能是接口,因为spring是反射机制创建对象,必须使用类)

        spring就完成 SomeService someService=new SomeServiceImpl();
        spring是把创建好的对象放入到map中,spring框架有一个map存放对象的。
            springMap.put(id的值,对象);
            例如 springMap.put(“someService“,new SomeServiceImpl());

        一个bean标签声明一个对象,
    -->
    <bean id="someService" class="com.service.impl.SomeServiceImpl"/>
    <bean id="someService1" class="com.service.impl.SomeServiceImpl"/>

    <!--
        spring能创建一个非自定义类的对象吗,创建一个存在的某个类的对象
    -->
    <bean id="mydate" class="java.util.Date"/>

</beans>
<!--
    spring配置文件
    1.beans:是根标签,spring中把java对象称为bean。
    2.spring-beans.xsd是约束文件,和mybatis中的那个 dtd是一样的
-->

测试方法

@Test
    public void test01(){
        SomeService someService=new SomeServiceImpl();
        someService.doSome();
    }

    /**
     * spring默认创建对象的时间:在创建spring容器时,会创建配置文件中的所有的对象
     * spring创建对象:默认调用的是无参构造方法
     */
    @Test
    public void test02(){
        //使用spring容器创建的对象
        //1.指定spring配置文件的名称
        String config="beans.xml";
        //2.创建表示spring容器的对象,ApplicationContext
        //ApplicationContext就是表示spring容器,通过容器对象就能获取对象了
        //ClassPathXmlApplicationContext表示从类路径中去加载spring的配置文件的
        ApplicationContext ac=new ClassPathXmlApplicationContext(config);

        //从容器中获取某个对象,你要调用对象的方法
        //getBean("配置文件中bean的id值")
        SomeService service=(SomeService) ac.getBean("someService");

        //使用spring创建好的对象
        service.doSome();

    }

运行成功!
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值