Spring快速入门-02-第一个spring程序

3. HelloSpring

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

3.1. 快速入门案例

  1. 新建项目,项目结构

  1. 导入jar包

spring需要导入commons-logging进行日志记录,利用maven自动下载对应的加载项

<dependency>
  <groupId>commons-logging</groupId>
  <artifactId>commons-logging</artifactId>
  <version>1.2</version>
</dependency>
  1. 编写一个Hello实体类
package com.kuang.pojo;

public class Hello {
    private String src;

    public String getSrc() {
        return src;
    }

    public void setSrc(String src) {
        this.src = src;
    }

    @Override
    public String toString() {
        return "Hello{" +
        "src='" + src + '\'' +
        '}';
    }
}
  1. 编写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">

  <!--使用Spring来创建对象,在spring这些部称Bean-->
  <bean id="hello" class="com.kuang.pojo.Hello">
    <property name="src" value="Spring"/>
  </bean>

</beans>
  1. 测试
import com.kuang.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        //解析beans.xml文件,生成管理相应的Bean对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //getBean : 参数即为spring配置文件中bean的id
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());
    }
}

3.2. 思考

3.2.1. 两个问题
  • Hello对象是谁创建的?
    • Hello对象是由Spring创建的
  • Hello对象的属性是怎么设置的?
    • Hello对象的属性是由Spring容器设置的
3.2.2. 本质(这个过程就叫控制反转)
  • 控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象是由Spring创建的
  • 反转:程序本身不创建对象,而变成被动接收对象
3.2.3. 依赖注入:就是利用set方法进行注入的
  • IOC是一种编程思想,由主动的编程变成被动的接收。
  • 可以通过new ClassPathXmlApplicationContext去浏览一下底层源码。

3.2.4. 修改2.1引例
  1. 在引例中,新建一个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="mysqlImpl" class="com.kuang.dao.UserDaoImplMysql"/>

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

</beans>
  1. 业务层使用set方式编写,即引例中的注释部分代码
package com.kuang.service;

import com.kuang.dao.UserDao;
import com.kuang.dao.UserDaoImpl;
import com.kuang.dao.UserDaoImplMysql;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    //private UserDao userDao = new UserDaoImplMysql();

    private UserDao usreDao;

    //利用set进行动态实现值的注入
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void getUser() {
        userDao.getUser();
    }
}
  1. 测试
 @Test
    public void test2(){
        // 获取AppLicationcontext;拿到Spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserServiceImpl serviceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
        serviceImpl.getUser();
    }

注意:此时,彻底不用在程序中修改代码,要实现不同的操作,只需要在xml配置文件中进行修改;所谓IoC:对象由spring来创建,管理,装配!注意体会!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值