3.Spring学习笔记_第一个spring程序

1.导入依赖:

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>5.1.10.RELEASE</version>
</dependency>
  1. 编写一个Hello实体类
public class Hello {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Hello{" +
                "name='" + name + '\'' +
                '}';
    }
}
  1. 编写spring文件 , 这里命名为beans.xml,编写在resources目录下:
?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

    类型 变量名 = new 类型();
    Hello hello = new Hello();
    id : 变量名
    class :相当于new的对象,即new Hello()
    property :相当于对象属性中的一个值

    -->

    <bean id="hello" class="com.wwj.Hello">
        <property name="name" value="wwj"/>
    </bean>


</beans>
  1. 测试一下:
public class MyTest {
    public static void main(String[] args) {
        //获取String的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //现在的对象实例都在beans.xml进行管理,要使用直接取出,对应的id
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());


    }

}

思考:

  1. Hello 对象是谁创建的 ? hello 对象是由Spring创建的

  2. Hello 对象的属性是怎么设置的 ? hello 对象的属性是由Spring容器设置的

这个过程就叫控制反转 :

  1. 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的

  2. 反转 : 程序本身不创建对象 , 而变成被动的接收对象 .

  3. 依赖注入 : 就是利用set方法来进行注入的.

IOC是一种编程思想,由主动的编程变成被动的接收

修改上一节的案例:
增加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">

    <bean id="pythonImpl" class="com.wwj.dao.UserDaoPythonImpl"/>
    <bean id="javaImpl" class="com.wwj.dao.UserDaoJavaImpl"/>

    <bean id="UserServiceImpl" class="com.wwj.service.UserServiceImpl">
        <!--ref:引用spring中创建好的对象,-->
        <!--value:具体的值,基本数据类型-->
        <property name="userDao" ref="javaImpl"/>
    </bean>


</beans>

测试一下:

public class MyTest {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
        userServiceImpl.getUser();


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值