【Spring框架03】DI依赖注入

【Spring框架03】依赖注入和构造器注入

思维导图

在这里插入图片描述

一.set注入(必须存在set方法)

1.编写set方法

public class UserService {

   private UserDao userDao;
    public void test(){
        userDao.add();
   }
   public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

}

2.编写引用的对象类

public class UserDao {
    public void add(){
        System.out.println("UseDao.add>>>>>>>>>");
    }
}

3.spring.xml配置

标签内 name表示创建对象的名字
标签内 ref表示中的id

<?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">
<!--      set注入-->
    <bean id="userDao" class="com.lcySpring.dao.UserDao"></bean>
    <bean id="userService" class="com.lcySpring.service.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>
</beans>

二.构造器注入(必须存在构造方法)

1.编写构造方法

这里是传参构造

public class AccountService {
    public AccountDao accountDao;

    public AccountService(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void test(){
        accountDao.add();
    }
}

2.编写引用的对象类

public class AccountDao {
    public void add(){
        System.out.println("AccountDao.add>>>>>>>>>");
    }
}

3.spring.xml配置

标签内 name表示传入参数的名字
标签内 ref表示中的id

<?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="accountDao" class="com.lcySpring.dao.AccountDao"></bean>
    <bean id="accountService" class="com.lcySpring.service.AccountService">
        <constructor-arg name="accountDao" ref="accountDao"></constructor-arg>
    </bean>
</beans>

三.p标签和c标签

1.简化< property >使用p 标签

这里我们创建一个类存在set方法

public class UserService {
    private String username;
    private int age;

    public void show(){
        System.out.println("姓名:"+username+" 年龄:"+age);
    }
    public String getUsername() {
        return username;
    }

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

    public int getAge() {
        return age;
    }

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

配置spring.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.lcySpring.service.UserService">
    <property name="username" value="mlrcj"></property>
    <property name="age" value="21"></property>
</bean>

</beans>

通过设置value 属性成功传入参数
在这里插入图片描述
但是这样写太麻烦了我们可以使用p标签
在spring.xml中配置命名空间加入p插件功能(p:是写在< bean >里面的)

<?xml version="1.0" encoding="UTF-8"?>
<!--使用的命名空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    使用p标签-->
<bean id="userService" class="com.lcySpring.service.UserService"
    p:username="lmrcj"
      p:age="21"
>
</bean>

</beans>

2.简化< constructor-arg >使用c 标签

和p标签的功能差不多,但是它还可以提供索引赋值

<!--使用的命名空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:c="http://www.springframework.org/schema/c"
        xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    使用c标签-->
<bean id="accountService" class="com.lcySpring.service.AccountService"
        c:aname="aaaaaaaaaaa"
        c:money="100000000000"
>
</bean>
    <bean id="accountService02" class="com.lcySpring.service.AccountService"
          c:_0="bbbbbbbbbbbbbb"
          c:_1="10000000000000"
    >
</bean>

关于Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/c]报错
可以参考这篇文章
文章链接

四.集合注入

1.集合list注入

构建一个含集合的java类

public class CollectionService {
    private List list;

    public void setList(List list) {
        this.list = list;
    }
    public void show(){
        System.out.println(list);
    }
}

配置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="collectionService" class="com.lcySpring.service.CollectionService">
        <property name="list">
            <list>
                <value>a</value>
                <value>b</value>
                <value>c</value>
                <value>d</value>
            </list>
        </property>
    </bean>
</beans>

编写测试类,检测成功注入
在这里插入图片描述

2.集合set注入

直接将xml中list配置改为set就好了

<?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="collectionService" class="com.lcySpring.service.CollectionService">
        <property name="set">
            <set>
                <value>a</value>
                <value>b</value>
                <value>c</value>
                <value>d</value>
            </set>
        </property>
    </bean>
</beans>

3.map类型属性注入

在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="collectionService" class="com.lcySpring.service.CollectionService">
        <property name="map">
            <map>
                <entry>
                    <key><value>lmr</value></key>
                    <value>菜鸡</value>
                </entry>
                <entry>
                    <key><value>lcy</value></key>
                    <value>超厉害</value>
                </entry>
            </map>
        </property>
    </bean>
</beans>

编写测试类查看
在这里插入图片描述

4.properties属性注入

在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="collectionService" class="com.lcySpring.service.CollectionService">
        <property name="prop">
           <props>
               <prop key="lmr">小菜鸡</prop>
               <prop key="lcy">超厉害</prop>
           </props>
        </property>
    </bean>
</beans>

编写测试类查看
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值