Java:Idea创建项目和Spring工程基本使用

一、创建项目

1、创建新的空的项目:

在这里插入图片描述

Empty Project–next

在这里插入图片描述

2、定义项目的名称,并指定位置

在这里插入图片描述

3、对项目进行设置,JDK版本、编译版本

在这里插入图片描述

4、添加模块信息

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5、修改maven路径

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6、项目目录结构

在这里插入图片描述

二、搭建Spring的框架

1、在核心配置文件中添加Spring的jar包

在这里插入图片描述

1.1 Application context not configured for this file警告处理

在这里插入图片描述
spring配置文件中时常会出现这个提示,翻译过来大概意思就是没有配置该文件到项目中
进入到File →Project Structure中查看
在这里插入图片描述
在这里插入图片描述

2、创建一个Student类

在这里插入图片描述

3、编写Spring核心配置文件

  • 配置文件名称:applicationContext.xml【beans.xml或spring.xml】

  • 配置文件路径:src/main/resources

在这里插入图片描述
在这里插入图片描述

  • 将以下代码复制到applicationContext.xml文件中。
  • 将对象装配到IOC容器中,记得根据实际项目修改下
<?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">

    <!-- 将对象装配到IOC容器中-->
    <!--  id是唯一标识。class是定义bean的类型【class全类名】  -->
    <bean id="stuZhenzhong" class="com.atguigu.spring.pojo.Student">
    <!--    property:为对象中属性赋值【set注入】    -->
    <!--    name属性:设置属性名称。value属性:设置属性数值    -->
        <property name="stuId" value="101"></property>
        <property name="stuName" value="zhenzhong"></property>
    </bean>

</beans>

4、使用核心库类

  • 创建一个测试类

在这里插入图片描述

  • 创建容器对象
    ApplicationContext iocObj = new ClassPathXmlApplicationContext("applicationContext.xml");
  • 通过容器对象,获取需要对象
    getBean(String beanId,Clazz clazz):通过beanId和Class获取对象(推荐用这个)
import com.atguigu.spring.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    @Test
    public void testSpring(){
//        使用Spring之前
//        Student student = new Student();

//        使用Spring之后
//        创建容器对象
        ApplicationContext iocObj = new ClassPathXmlApplicationContext("applicationContext.xml");

//        通过容器对象,获取需要对象

//        方式一:getBean(String beanId):通过beanId获取对象
//        - 不足:需要强制类型转换,不灵活
//        Student stuZhenzhong = (Student)iocObj.getBean("stuZhenzhong");
//        System.out.println("stuZhenzhong = " + stuZhenzhong);

//        方式二:getBean(Class clazz):通过Class方式获取对象
//        - 不足:容器中有多个相同类型bean的时候,会报如下错误:expected single matching bean but found 2: stuZhenzhong,stuZhouxu
//        Student bean = iocObj.getBean(Student.class);
//        System.out.println("bean = " + bean);

//        方式三:getBean(String beanId,Clazz clazz):通过beanId和Class获取对象(推荐用这个)
        Student stuZhenzhong = iocObj.getBean("stuZhenzhong",Student.class);
        System.out.println("stuZhenzhong = " + stuZhenzhong);

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值