其他分享
首页 > 其他分享> > 给力啊!这篇Spring Bean的依赖注入方式笔记总结真的到位,没见过写的这么细的

给力啊!这篇Spring Bean的依赖注入方式笔记总结真的到位,没见过写的这么细的

作者:互联网

1. Bean的依赖注入概念

依赖注入(Dependency Injection):它是 Spring 框架核心 IOC 的具体实现。在编写程序时,通过控制反转,把对象的创建交给了 Spring,但是代码中不可能出现没有依赖的情况。IOC 解耦只是降低他们的依赖关系,但不会消除。例如:业务层仍会调用持久层的方法。那这种业务层和持久层的依赖关系,在使用 Spring 之后,就让 Spring 来维护了。简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。

2. Bean的依赖注入方式

(1)set方法

在UserServiceImpl中添加setUserDao方法

public class UserServiceImpl implements UserService {
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;  
        } 
    @Override    
    public void save() {      
   		 userDao.save();
	}
}

<bean id="userDao" class="com.wange.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.wange.service.impl.UserServiceImpl">
    <property name="userDao" ref="userDao"></property>
</bean>

(2)构造方法

创建有参构造

public class UserServiceImpl implements UserService {
        @Override
	public void save() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");       
		UserDao userDao = (UserDao) applicationContext.getBean("userDao");
		userDao.save();
	}
}

<bean id="userDao" class="com.wange.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.wange.service.impl.UserServiceImpl">      		   	
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>

3. Bean的依赖注入的数据类型

注入数据的三种数据类型

普通数据类型
引用数据类型
集合数据类型

Bean的依赖注入的数据类型

(1)普通数据类型的注入

package com.wange.dao.impl;
import com.wange.dao.UserDao;

public class UserDaoImpl implements UserDao{   

    private String username;
    private int age;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void save() {
        System.out.println(username+"----"+age);
        System.out.println("save方法正在执行");
    }
}

<bean id="userDao" class="com.wange.dao.impl.UserDaoImpl">
    <property name="username" value="lisi"></property>
    <property name="age" value="20"></property>
</bean>

package com.wange.test;

import com.wange.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//测试
public class UserController {
    public static void main(String[] args) {
            ApplicationContext applicationContext =
                    new ClassPathXmlApplicationContext("applicationContext.xml");
            UserDao userDao = (UserDao) applicationContext.getBean("userDao");
            userDao.save();
    }
}

(2)集合数据类型(List)String 的注入

package com.wange.dao.impl;

import com.wange.dao.UserDao;
import com.wange.domain.User;
import java.util.List;

public class UserDaoImpl implements UserDao{
    private List<String> stringList;

    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    public void save() {
        System.out.println(stringList);
        System.out.println("save方法正在执行");
    }
}

<bean id="userDao" class="com.wange.dao.impl.UserDaoImpl">
	<property name="stringList">
		<list>
			<value>str1</value>
			<value>str2</value>
			<value>str3</value>
		</list>
	</property>
</bean>

(3)集合数据类型(List)User 的注入

package com.wange.dao.impl;

import com.wange.dao.UserDao;
import com.wange.domain.User;
import java.util.List;

public class UserDaoImpl implements UserDao {
	private List<User> userList;

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

public void save() {
	System.out.println(userList);
	System.out.println("save方法正在执行");
	}
}

<bean id="user1" class="com.wange.domain.User"/>
<bean id="user2" class="com.wange.domain.User"/>
<bean id="userDao" class="com.wange.dao.impl.UserDaoImpl">
    <property name="userList">
        <list>
            <bean class="com.wange.domain.User"/>
            <bean class="com.wange.domain.User"/>
            <ref bean="user1"/>
            <ref bean="user2"/>       
        </list>
    </property>
</bean>

4)集合数据类型( Map<String,User> )的注入

package com.wange.dao.impl;

import com.wange.dao.UserDao;
import com.wange.domain.User;

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

public class UserDaoImpl implements UserDao{
    private Map<String, User> userMap;

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

    public void save() {
        System.out.println(userMap);
        System.out.println("save方法正在执行");
    }
}

<bean id="user1" class="com.wange.domain.User"/>
<bean id="user2" class="com.wange.domain.User"/>
<bean id="userDao" class="com.wange.dao.impl.UserDaoImpl">
    <property name="userMap">
        <map>            
            <entry key="u1" value-ref="user1"/>
            <entry key="u2" value-ref="user2"/>
        </map>
    </property>
</bean>

(5)集合数据类型(Properties)的注入

package com.wange.dao.impl;

import com.wange.dao.UserDao;
import java.util.Properties;

public class UserDaoImpl implements UserDao{
    private Properties properties;

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

    public void save() {
        System.out.println(properties);
        System.out.println("save方法正在执行");
    }
}

<bean id="userDao" class="com.wange.dao.impl.UserDaoImpl">
      <property name="properties">
            <props>
                <prop key="p1">properties1</prop>
                <prop key="p2">properties2</prop>
                <prop key="p3">properties3</prop>
            </props>
      </property>  
</bean>

package com.wange.test;

import com.wange.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//测试
public class UserController {
    public static void main(String[] args) {
            ApplicationContext applicationContext =
                    new ClassPathXmlApplicationContext("applicationContext.xml");
            UserDao userDao = (UserDao) applicationContext.getBean("userDao");
            userDao.save();
    }
}

运行结果

最后

大家看完有什么不懂的可以在下方留言讨论,也可以关注我私信问我,我看到后都会回答的。也欢迎大家关注我的公众号:前程有光,马上金九银十跳槽面试季,整理了1000多道将近500多页pdf文档的Java面试题资料,助你圆梦BAT!文章都会在里面更新,整理的资料也会放在里面。谢谢你的观看,觉得文章对你有帮助的话记得关注我点个赞支持一下!

标签:wange,UserDao,Spring,给力,Bean,import,save,com,public
来源: https://www.cnblogs.com/lwh1019/p/13433053.html