IDEA创建第一个spring项目

项目创建和项目结构

  • 创建工程
    在这里插入图片描述

-鼠标右击src文件,一般取名为applicationContext
在这里插入图片描述

  • 项目结构
    在这里插入图片描述

Spring项目和普通java项目对比

Spring的优良特性

  • Spring是一个轻量级框架,可以在其中写原始的ava语言
  • Spring是一个IOC和AOP容器框架
  • 面向切面编程AOP,OOP的补充
  • Spring是一 个容器,包含并且并管理应用对象的生命周期
  • 组件化: Spring的组件是Spring管理的对象(降低耦合)
  • 一站式:在IOC和AOP的基础上可以整合各种开源框架和第三方库

普通的Java程序

  • younghd包中创建一个Hello

在这里插入图片描述

package younghd;

public class Hello {
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    private String name;
    private String id;
    public Hello()
    {

    }
    public Hello(String m_id,String m_name)
    {
        this.id=m_id;
        this.name=m_name;
    }
}

  • 普通的测试输出类:Test
package younghd;

public class Test {
    public static void main(String[] args) {
        Hello helloChinese =new Hello("中文","你好");
        Hello helloEnglish =new Hello();
        helloEnglish.setId("英语");
        helloEnglish.setName("hello");
        System.out.println("普通的java程序:");
        System.out.println(helloEnglish.getId()+":"+helloEnglish.getName());
        System.out.println(helloChinese.getId()+":"+helloChinese.getName());
    }
}

在这里插入图片描述

Spring的Java程序

  • Hello类相同
  • Spring 配置文件:applicationContext
<?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="chinese" class="younghd.Hello">
        <property name="id" value="中文"/>
        <property name="name" value="你好"/>
    </bean>
    <bean id="english" class="younghd.Hello">
        <property name="id" value="英语"/>
        <property name="name" value="hello"/>
    </bean>
    <bean id="korea" class="younghd.Hello">
        <constructor-arg value="韩语"></constructor-arg>
        <constructor-arg value="안녕"></constructor-arg>
    </bean>
</beans>
  • Spring的测试输出类:TestIOC
package younghd;

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

public class TestIOC {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("spring的控制反转:");
        //方法一:强制转换
        System.out.print("强制转换:  ");
        Hello hello1 = (Hello) ac.getBean("chinese");
        System.out.println(hello1.getId()+":"+hello1.getName());
        //方法二:反射
        System.out.print("类的反射:  ");
        Hello hello2 =  ac.getBean("english",Hello.class);
        System.out.println(hello2.getId()+":"+hello2.getName());
        //方法三:构造函数
        System.out.print("构造函数:  ");
        Hello hello3=ac.getBean("korea",Hello.class);
        System.out.println(hello3.getId()+":"+hello3.getName());

    }
}

在这里插入图片描述

总结

IOC:控制反转

  • DI:依赖注入,是IOC的一种实现
  • 普通的Java程序,当我们需要该类时,我们通过new方法,构造出一个对象,每一个对象都需要我们自己管理
  • Spring的java程序,我们的每一个对象都是由Spring来管理,当我们需要某个对象时,就通过Spring容器获取该对象的实例

Spring容器的两种实现方法

  • 在通过IOC容器读取Bean的实例之前,需要先将IOC容器本身实例化Spring 提供了IOC容器的两种实现方式。
  • BeanFactory: IOC容器的基本实现,是Spring内部的基础设施,是面向Spring本身的,不是提供给开发人员使用的。
  • ApplicationContextBeanEactory 的子接口,提供了更多高级特性。面向Spring的使用者。
  • 几乎所有场合都使用ApplicationContext而不是底层的BeanFactory。
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

ApplicationContext的主要实现类

  • ClassPathXmlApplicationContext: 对应类路径下的XML格式的配置文件(相对路径
  • FileSystemXmlApplicationContext :对应文件系统中的XML格式的配置文件(绝对路径

bean的配置与获取

  • 获取bean实例
    在这里插入图片描述
  • 配置bean实例
    在这里插入图片描述
  • bean与property的属性
 <!--
       <bean>定义Spring管理的一个对象
       id:对象唯一标识,通过类型获取bean的过程中不可以设置
       class:次对象所属类的全限定名
 -->
 <!--
        <property>为对象的某个属性赋值
        name:属性值
    value:属性名
 -->
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值