Spring依赖注入

Spring依赖注入

创建对象的方式都是使用xml配置文件.

1.通过有参构造

dao层

package com.demo.dao;

public interface UserMapper {
	
}
package com.demo.dao.impl;

import com.demo.dao.UserMapper;

public class UserMapperImpl implements UserMapper {
	
}

service层

Dao层

package com.demo.service;

public interface UserServiceDao {
    void getUserService();
    void show();

}

Impl层

package com.demo.service.impl;

import com.demo.service.UserServiceDao;

public class UserServiceDaoImpl implements UserServiceDao {
    private String name;
    //dao层
    private UserMapper userMapper;

    
    //有参构造
    public UserServiceDaoImpl(String name,UserMapper userMapper){
        
    }

	//展示成员变量中的值
    @Override
    public void show() {
        System.out.println(name+"="+userMapper);
    }
}

配置文件

<?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="UserMapper" class="com.demo.dao.impl.UserMapperImpl"/>
    
    <bean id="UserServiceDao" class="com.demo.service.impl.UserServiceDaoImpl">
        <constructor-arg name="name" value="李四"/>
        <!--指定引用类型属性的值-->
        <constructor-arg name="userMapper" ref="UserMapper"/>
    </bean>
</beans>

注意:

在配置文件确定成员变量的三种方式:
    name:指定成员变量名
    type:指定成员变量类型
    index:指定成员变量所在索引位置
赋值:
	value:基本类型的属性赋值
	ref:引用类型的属性赋值,来自于spring创建的对象的唯一标识

测试类

import com.demo.dao.UserMapper;
import com.demo.service.UserServiceDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest05 {

    public static void main(String[] args) {
        /**
         * 通过有参构造依赖注入
         */
        //通过spring容器获取对象
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:UserBeans01.xml");
        UserServiceDao bean = (UserServiceDao) context.getBean("UserServiceDao");
        //打印获取到的对象
        System.out.println("bean = " + bean);
        //调用方法
        bean.show();//李四=com.demo.dao.impl.UserMapperImpl@55d56113
    }
}

2.通过set方法

service层

Impl层

package com.demo.service.impl;

import com.demo.dao.UserMapper;
import com.demo.service.UserServiceDao;

public class UserServiceDaoImplSet implements UserServiceDao {
    private String name;
    private UserMapper userMapper;

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

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public UserServiceDaoImplSet() {
        System.out.println("对象创建成功");
    }

    //打印成员变量值
    @Override
    public void show() {
        System.out.println(name+"="+userMapper);
    }
}

配置文件

<?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="UserMapper" class="com.demo.dao.impl.UserMapperImpl"/>
    
    <bean id="UserServiceDao" class="com.demo.service.impl.UserServiceDaoImplSet">
		
        <!--
			name:1.类中有对应属性(set方法名去掉set之后的驼峰名字)
                 2.类中无对应属性(set方法名去掉set之后的名字)
		-->
        <property name="name" value="李四"/>
        <!--指定引用类型属性的值-->
        <property name="userMapper" ref="UserMapper"/>
    </bean>
</beans>

测试类

import com.demo.dao.UserMapper;
import com.demo.service.UserServiceDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest05 {

    public static void main(String[] args) {
        /**
         * 通过有参构造依赖注入
         */
        //通过spring容器获取对象
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:UserBeans02.xml");
        UserServiceDao bean = (UserServiceDao) context.getBean("UserServiceDao");
        //打印获取到的对象
        System.out.println("bean = " + bean);
        //调用方法
        bean.show();//李四=com.demo.dao.impl.UserMapperImpl@a67c67e
    }
}

posted @ 2021-01-19 20:28  plum_wink  阅读(50)  评论(0)    收藏  举报