02,Spring - BeanFactory快速入门

BeanFactory

1,BeanFactory快速入门

1.1 演示通过xml创建bean

在这里插入图片描述

1.1.1,创建maven工程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AsL7ozA1-1672903791017)(https://note.youdao.com/yws/res/3790/WEBRESOURCE4a7880ac793f49e0ae9d11011b012b29)]

1.1.2,引入maven坐标

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring</artifactId>
        <groupId>com.study</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring_ioc_demo01</artifactId>


    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.9</version>
        </dependency>
    </dependencies>

</project>

1.1.3 创建 UserService

package com.study.service;

public interface UserService {
}

1.1.4 创建 UserServiceImpl

package com.study.service.impl;

import com.study.service.UserService;

public class UserServiceImpl implements UserService {
}

1.1.5 创建 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="userService" class="com.study.service.impl.UserServiceImpl"></bean>
    
</beans>

1.1.6 创建 测试类

package com.study.demoTest;

import com.study.service.UserService;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

public class demoTest01 {
    public static void main(String[] args) {
        //创建工厂对象
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

        //创建一个读取器(xml文件)
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);

        //读取配置文件给工厂
        reader.loadBeanDefinitions("beans.xml");

        //更具id获取bean实例对象
        UserService userService = (UserService)beanFactory.getBean("userService");

        //输出对象地址
        System.out.println(userService);
    }
}

1.2 演示DI(在service中注入dao)

在这里插入图片描述

1.2.1 创建 userDao

package com.study.dao;

public interface UserDao {
}

1.2.2 创建 userDaoImpl

package com.study.dao.impl;

import com.study.dao.UserDao;

public class UserDaoImpl implements UserDao {
}

1.2.3 修改 UserServiceImpl

package com.study.service.impl;

import com.study.dao.UserDao;
import com.study.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    //BeanFactory调用该方法,从容器中获得UserDao设置到此处
    public void setUserDao(UserDao userDao){
        System.out.println("beanfactory调用该方法获取到userDao:"+userDao);
        this.userDao = userDao;
    }
}

1.2.4 修改 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="userService" class="com.study.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>

    <bean id="userDao" class="com.study.dao.impl.UserDaoImpl"></bean>
</beans>

1.2.5 测试类

package com.study.demoTest;

import com.study.service.UserService;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

public class demoTest01 {
    public static void main(String[] args) {
        //创建工厂对象
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

        //创建一个读取器(xml文件)
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);

        //读取配置文件给工厂
        reader.loadBeanDefinitions("beans.xml");

        //更具id获取bean实例对象
        UserService userService = (UserService)beanFactory.getBean("userService");

        //输出对象地址
        System.out.println(userService);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值