Spring-2-第一个项目

第一步:创建一个空项目

在这里插入图片描述

第二步骤:指定他的文件夹:

新建一个module,然后选择Maven
在这里插入图片描述

然后next接着finish

第三步:在Main下面创建resource的文件夹快捷键Alt+insert

在这里插入图片描述

接着在右击申明一下‘
在这里插入图片描述

第四步:改造一下pom.xml里面文件

<?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>org.example</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>

在这里插入图片描述

第五步:在resource下面申明一个配置Spring的配置文件

Alt+insert快捷键
在这里插入图片描述
在这里插入图片描述


<?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">
<!--申明我的第一个bean-->
<!--  id 是名字 class是包名.类名  创建的好的对象放到map里面对象里面 -->
<!-- SpringMap.put(id,对象)   -->
    <bean id="java01" class="org.example.first_class" />
<!--不能是接口,因为是反射机制-->
<!--    class是包名加类名-->
</beans>
<!--
Spring的配置文件
1.beans是根标签 Spring把Java对象成为bean
2.beans xmlns:是约束文件
3.bean只能申明一个类对象


-->

第六步:实例化

	 //第一步申明Spring的对象文件名称
	        String config="beans.xml";
	        //创建Spring容器的对象ApplicationContent
	        //ApplicationContent表示Spring的容器,通过容器实现对象
	        //这个时候会创建对象,会创建配置文件的所有对象
	        ApplicationContext ac =new ClassPathXmlApplicationContext(config);
	        //调用对象
	        first_class f=(first_class) ac.getBean("java01");
	        f.doSome();
package org.example;

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

public class MyTest {

    @Test
    public void test01() {

    first_class c=new first_class();
    c.doSome();
    }
    @Test
    public void test02(){

//第一步申明Spring的对象文件名称
        String config="beans.xml";
        //创建Spring容器的对象ApplicationContent
        //ApplicationContent表示Spring的容器,通过容器实现对象
        ApplicationContext ac =new ClassPathXmlApplicationContext(config);//ctrl+p是查看参数
        //调用对象
        first_class f=(first_class) ac.getBean("java01");
        f.doSome();

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码浪人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值