Spring中bean的实例化与依赖注入的实现

Spring中bean的实例化与依赖注入的实现

准备:

Spring程序开发步骤

    ①导入 Spring 开发的基本包坐标

    ②编写 Dao 接口和实现类

    ③创建 Spring 核心配置文件

    ④在 Spring 配置文件中配置 UserDaoImpl

    ⑤使用 Spring 的 API 获得 Bean 实例

开始

1、Bean实例化三种方式(代码实现见1~3)

    ① 无参构造方法实例化

在这里插入图片描述

    ② 工厂静态方法实例化

在这里插入图片描述
在这里插入图片描述

    ③ 工厂非静态方法实例化

在这里插入图片描述
在这里插入图片描述

2、依赖注入的实现(代码实现见4~10)

注入数据的三种数据类型分别为:

    ① 普通数据类型
    ② 引用数据类型
    ③ 集合数据类型

3、项目目录

在这里插入图片描述

4、代码实现

pom文件

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cfay</groupId>
    <artifactId>spring01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <spring.version>5.0.5.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

StaticFactoryBean类

package com.cfay.dao;

/**
 * 2、静态工厂
 */
public class StaticFactoryBean {
    public static UserDao createUserDao() {
        return new UserDaoImpl();
    }

}

DynamicFactoryBean类

package com.cfay.dao;

/**
 * 3、非静态工厂
 */
public class DynamicFactoryBean {
    public UserDao createUserDao() {
        return new UserDaoImpl();
    }
}

User 类

package com.cfay.dao;

public class User {
    private String userName;
    private String password;

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

UserDao接口

package com.cfay.dao;

public interface UserDao {
    //初始化
    void initMethod();
    //打印注入信息
    void printMessage();
}

UserDaoImpl类

package com.cfay.dao;

import java.util.List;
import java.util.Map;
import java.util.Properties;

public class UserDaoImpl implements UserDao {
    private User user;
    private String height;
    private String weight;
    private List<String> strList;
    private List<User> userList;
    private Map<String,User> map;
    private Properties properties;

    //1、无参构造方法
    public UserDaoImpl() {
        System.out.println("无参构造被调用!");
    }

    //5、有参构造(对象的注入)
    public UserDaoImpl(User user) {
        System.out.println("有参构造被调用!");
        System.out.println(user);
        this.user = user;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public void setStrList(List<String> strList) {
        this.strList = strList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    public void setMap(Map<String, User> map) {
        this.map = map;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    //4、有参构造(非对象注入)
    public UserDaoImpl(String height, String weight) {
        System.out.println("height=" + height + ",weight=" + weight);
        this.height = height;
        this.weight = weight;
    }

    @Override
    public void initMethod() {
        System.out.println("初始化方法!");
    }


    @Override
    public void printMessage() {
        System.out.println("打印验证信息");
        //6、普通数类型
        System.out.println("height=" + height + ",weight=" + weight);
        //7、集合数据类型(List<String>)的注入
        System.out.println(strList);
        //8、集合数据类型(List<Object>)的注入
        System.out.println(userList);
        //9、集合数据类型(Map<String,User> )的注入
        System.out.println(map);
        //10、集合数据类型(Properties)的注入
        System.out.println(properties);
    }
}

userDao.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">
    <!--class:指定全限定类名-->
    <!--1、无参构造方法-->
    <bean id="userDao" class="com.cfay.dao.UserDaoImpl" init-method="initMethod">
    </bean>

    <!--2、静态工厂(不需要创建工厂本身)
    factory-method:指定对应工厂方法-->
    <bean id="staticFactory" class="com.cfay.dao.StaticFactoryBean" factory-method="createUserDao"></bean>

    <!--3、非静态工厂-->
    <bean id="dynamicFactoryBean" class="com.cfay.dao.DynamicFactoryBean"></bean>
    <bean id="dynamicFactory" factory-bean="dynamicFactoryBean" factory-method="createUserDao"></bean>


    <!--4、有参构造(非对象注入)-->
    <bean id="user-extendMessage" class="com.cfay.dao.UserDaoImpl">
        <constructor-arg name="height" value="180"></constructor-arg>
        <constructor-arg name="weight" value="60"></constructor-arg>
    </bean>

    <!--5、有参构造(对象的注入)-->
    <bean id="userDao-arg" class="com.cfay.dao.User">
        <property name="userName" value="lcb"></property>
        <property name="password" value="24"></property>
    </bean>

    <bean id="userDao-args" class="com.cfay.dao.UserDaoImpl">
        <constructor-arg name="user" ref="userDao-arg"></constructor-arg>
    </bean>


    <!--6、普通数类型-->
    <bean id="simpleUserMessage" class="com.cfay.dao.UserDaoImpl">
        <property name="height" value="185"></property>
        <property name="weight" value="65"></property>
    </bean>

    <!--7、集合数据类型(List<String>)的注入-->
    <bean id="simpleList" class="com.cfay.dao.UserDaoImpl">
        <property name="strList">
            <list>
                <value>list1</value>
                <value>list2</value>
                <value>list3</value>
            </list>
        </property>
    </bean>

    <!--8、集合数据类型(List<Object>)的注入-->
    <bean id="user1" class="com.cfay.dao.User">
        <property name="userName" value="lcb1"></property>
        <property name="password" value="123"></property>
    </bean>
    <bean id="user2" class="com.cfay.dao.User">
        <property name="userName" value="lcb2"></property>
        <property name="password" value="456"></property>
    </bean>
    <bean id="objectList" class="com.cfay.dao.UserDaoImpl">
        <property name="userList">
            <list>
                <ref bean="user1"></ref>
                <ref bean="user2"></ref>
            </list>
        </property>
    </bean>

    <!--9、集合数据类型(Map<String,User> )的注入-->
    <bean id="testMap" class="com.cfay.dao.UserDaoImpl">
        <property name="map">
            <map>
                <entry key="map1" value-ref="user1"></entry>
                <entry key="map2" value-ref="user1"></entry>
            </map>
        </property>
    </bean>

    <!--10、集合数据类型(Properties)的注入-->
    <bean id="testProperties" class="com.cfay.dao.UserDaoImpl">
        <property name="properties">
            <props>
                <prop key="p1">prop1</prop>
                <prop key="p2">prop2</prop>
                <prop key="p3">prop3</prop>
            </props>
        </property>
    </bean>

</beans>

TestDemo测试类

package com.cfay.dao;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo {
    //读取配置文件
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userDao.xml");
    
    private UserDao userDao;

    /**
     * 测试构造方法
     */
    @Test
    public void testConstructionMethod() {
        //1、无参构造方法
        context.getBean("userDao");
        //4、有参构造(非对象)
        context.getBean("user-extendMessage");
        //5、有参构造(对象)
        context.getBean("userDao-args");
    }

    /**
     * 测试工厂方法
     */
    @Test
    public void testFactory() {
        //2、工厂静态方法实例化
        context.getBean("staticFactory");
        //3、工厂非静态方法实例化
        context.getBean("dynamicFactory");
    }

    /**
     * 测试注入不同的数据类型
     */
    @Test
    public void testDataType() {
        //6、普通数类型
        userDao = (UserDao) context.getBean("simpleUserMessage");
        //7、集合数据类型(List<String>)的注入
        userDao = (UserDao) context.getBean("simpleList");
        //8、集合数据类型(List<Object>)的注入
        userDao = (UserDao) context.getBean("objectList");
        //9、集合数据类型(Map<String,User> )的注入
        userDao = (UserDao) context.getBean("testMap");
        //10、集合数据类型(Properties)的注入
        userDao = (UserDao) context.getBean("testProperties");
        userDao.printMessage();
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楚风岸影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值