【Spring配置文件】依赖注入


一、引用数据类型

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();
    }
}

配置Spring容器调用set方法进行注入

<bean id="userDao" class="com.txt.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.txt.service.impl.UserServiceImpl">
    <property name="userDao" ref="userDao"/>
</bean>
//说明property name="userDao" ,name里的参数是将setUserDao(...)的UserDao的首字母小写

set之p方式注入

本质一样
引入P命名空间

xmlns:p="http://www.springframework.org/schema/p"

修改注入方式

<bean id="userService" class="com.txt.service.impl.UserServiceImpl" p:userDao-
 ref="userDao"/>

构造方式注入

创建有参构造

public UserServiceImpl(UserDao bean) {
        this.bean = bean;
    }

配置Spring容器调用有参构造时进行注入

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

二、普通数据类型

和引用同理,也有set和构造方法注入,区别如下

代码如下(set示例):

//传的值变为value
<bean id="userDao" class="com.txt.dao.impl.UserDaoImpl">
    <property name="company" value="txt"></property>
    <property name="age" value="15"></property>
</bean>

三、集合数据类型

和引用同理,也有set和构造方法注入

1.List

代码如下(set示例):

	//要注入的类
	private List<String> strList;
    public void setStrList(List<String> strList) {
        this.strList = strList;
    }
<bean id="userDao" class="com.txt.dao.impl.UserDaoImpl">
    <property name="strList">
        <list>
            <value>aaa</value>
            <value>bbb</value>
            <value>ccc</value>
        </list>
    </property>
</bean>

2.List <对象>

代码如下(set示例):

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("UserDao save method running....");
    }
}
<bean id="u1" class="com.txt.domain.User"/>
<bean id="u2" class="com.txt.domain.User"/>
<bean id="userDao" class="com.txt.dao.impl.UserDaoImpl">
    <property name="userList">
        <list>
            <bean class="com.txt.domain.User"/>
            <bean class="com.txt.domain.User"/>
            <ref bean="u1"/>
            <ref bean="u2"/>
        </list>
    </property>
</bean>

3.Map<String,User>

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("UserDao save method running....");
    }
}
<bean id="u1" class="com.txt.domain.User"/>
<bean id="u2" class="com.txt.domain.User"/>
<bean id="userDao" class="com.txt.dao.impl.UserDaoImpl">
    <property name="userMap">
        <map>
            <entry key="user1" value-ref="u1"/>
            <entry key="user2" value-ref="u2"/>
        </map>
    </property>
</bean>

4.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("UserDao save method running....");
    }
}
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
    <property name="properties">
        <props>
            <prop key="p1">aaa</prop>
            <prop key="p2">bbb</prop>
            <prop key="p3">ccc</prop>
        </props>
    </property>
</bean>

四、引入其他配置文件

<import resource="applicationContext-xxx.xml"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值