1、spring-01-heilospring

1、建一个Hello类

public class Hello {
    private String name;

    public String getName() {
        return name;
    }

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

2、beans.xml
● bean就是java对象 , 由Spring创建和管理
● id=“hello” 是要创建的对象的变量名字
● class=“cn.zhy.pojo.Hello” 是要创建的对象

● name="name"对应Hello类中的属性名
● value=“Spring” 是给对应的属性设置值,是通过set方法设置的

<?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创建和管理
    id="hello" 是要创建的对象的变量名字
    class="cn.zhy.pojo.Hello" 是要创建的对象
    <property name="name" value="Spring"/>
    name="name"对应Hello类中的属性名
    value="Spring"  是给对应的属性设置值,是通过set方法设置的
    -->
    <bean id="hello" class="cn.zhy.pojo.Hello">
        <property name="name" value="Spring"/>
    </bean>

</beans>

3、测试

public class MyTest {
    public static void main(String[] args) {
        //获取spring上下文对象,beans.xml是java对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //我们的 对象都在spring中管理,要使用直接去spring中取出
        //getBean中传的是在bean中设置的id,这些就可以将Hello对象返回出来
        //然后就可以直接获取这个对象中的方法和属性,不用new 来创建对象
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello);
    }
}

控制反转 :
● 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
● 反转 : 程序本身不创建对象 , 而变成被动的接收对象 .
依赖注入 : 就是利用set方法来进行注入的.
IOC是一种编程思想,由主动的编程变成被动的接收

第二个ioc案例
创建UserDao接口

public interface UserDao {
    void getUser();
}

实现UserDao接口
1、

public class UserFirstImpl implements UserDao{
    @Override
    public void getUser() {
        System.out.println("第一个实现1111111");
    }
}

2、

public class UserSecondImpl implements UserDao {
    @Override
    public void getUser() {
        System.out.println("第二个实现222222222");
    }
}

建一个UserService接口

public interface UserService {
    void getUser();
}

对UserService接口进行实现

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void getUser() {
        userDao.getUser();
    }
}

编写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="firstImpl" class="cn.zhy.cn.zhy.dao.UserFirstImpl">

    </bean>
    <bean id="secondImpl" class="cn.zhy.cn.zhy.dao.UserSecondImpl">

    </bean>

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

测试

public class MyIocTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserService serviceImpl = (UserService) context.getBean("serviceImpl");
        serviceImpl.getUser();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值