Spring笔记3--HelloSpring

3、HelloSpring

导入Jar包

注 : spring 需要导入commons-logging进行日志记录 . 我们利用maven , 他会自动下载对应的依赖项 .

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.0.RELEASE</version>
    </dependency>
</dependencies>

编写代码

1、编写一个Hello实体类

package com.cfeng.pojo;

public class Hello {

    private String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }


    public void show() {
        System.out.println("hello"+str);
    }

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }

}

2、编写我们的spring文件 , 这里我们命名为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就是java对象 , 由Spring创建和管理
    使用Spring来创建对象,这些对象在Spring中都称为bean
    id对象实体类的名字,class去找对应的实体类
    property中的name对应实体类中的属性,value给属性赋值
    -->
    <bean id="hello" class="com.cfeng.pojo.Hello">
        <property name="str" value="spring"/>
    </bean>

</beans>

3、测试

public class MyTest {

    public static void main(String[] args) {
        //解析beans.xml文件 , 生成管理相应的Bean对象,我们的对象都在Spring中管理了,我们要使用直接去里面取出来即可!
        //使用xml配置固定写法:ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //getBean : 参数即为spring配置文件中bean的id .
        Hello hello = (Hello) context.getBean("hello");
        hello.show();
    }
}

思考

  • Hello 对象是谁创建的 ? hello 对象是由Spring创建的
  • Hello 对象的属性是怎么设置的 ? hello 对象的属性是由Spring容器设置的

这个过程就叫控制反转 :

  • 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
  • 反转 : 程序本身不创建对象 , 而变成被动的接收对象 .

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

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

可以通过newClassPathXmlApplicationContext去浏览一下底层源码 .

修改案例一

我们在案例一中, 新增一个Spring配置文件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="UserDaoImpl" class="com.cfeng.dao.UserDaoImpl"></bean>
    <bean id="UserDaoMysqlImpl" class="com.cfeng.dao.UserDaoMysqlImpl"></bean>
    <bean id="UserDaoOracleImpl" class="com.cfeng.dao.UserDaoOracleImpl"></bean>


<!--
    ref:引用Spring容器中创建好的对象
    value:具体的值,基本数据类型!
    注意: 这里的name并不是属性 , 而是set方法后面的那部分 , 首字母小写
    引用另外一个bean , 不是用value 而是用 ref
-->
    <bean id="UserServiceImpl" class="com.cfeng.service.UserServiceImpl">
       <property name="userDao" ref="UserDaoOracleImpl"></property>
</bean>

测试!

public class MyTest {

    public static void main(String[] args) {
    
        //获取ApplicationContext;拿到spring容器
        ApplicationContext Context = new ClassPathXmlApplicationContext("beans.xml");

        UserServiceImpl userService =(UserServiceImpl)Context.getBean("UserServiceImpl");

        userService.getUser();
    }
}

现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改 , 所谓的IoC,一句话搞定 : 对象由Spring 来创建 , 管理 , 装配 !

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值