Spring依赖注入的set方式注入、命名空间注入

一:set方式依赖注入

1. 注入普通字段

 //pojo层: 
 private String name;
<!--spring配置文件-->
<bean id="student" class="com.spring.pojo.Student">
    <property name="name" value="jack"/>
</bean>
	//测试类
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student)Context.getBean("student");
        System.out.println(student.getName());

    }



2. 引用类型注入

 //实体类
 private Address address;
    <bean id="address" class="com.spring.pojo.Address"/>
    <!--spring配置文件-->
    <bean id="student" class="com.spring.pojo.Student">
        <!--bean注入-->
        <property name="address" ref="address"/>
    </bean>



3. 数组类型注入

//数组类型
private String[] books;
  <!--spring配置文件-->
    <bean id="student" class="com.spring.pojo.Student">
        <!-- 数组注入-->
        <property name="books">
            <array>
                <value>数组元素1</value>
                <value>数组元素2</value>
                <value>数组元素3</value>
            </array>
        </property>
    </bean>



4. List类型注入

 //实体类
 private List<String> hobbies;
   <!--spring配置文件-->
    <bean id="student" class="com.spring.pojo.Student">
       <!--list注入--> 
        <property name="hobbies">
            <list>
                <value>value1</value>
                <value>value2</value>
                <value>value3</value>
            </list>
        </property>
    </bean>



5. Map类型注入

 //实体类
 private Map<String,String> card;
   <!--spring配置文件-->
    <bean id="student" class="com.spring.pojo.Student">
       <!--list注入--> 
          <property name="card">
            <map>
                <entry key="key01" value="value01"/>
                <entry key="key02" value="value02"/>
                <entry key="key03" value="value03"/>
            </map>
        </property>
    </bean>



6. Set类型注入

 //实体类
 private Set<String> games;
   <!--spring配置文件-->
    <bean id="student" class="com.spring.pojo.Student">
       <!--set注入-->    
         <property name="games">
            <set>
                <value>set01</value>
            </set>
         </property>
    </bean>



7. null类型注入

 //实体类
 private Set<String> games;
   <!--spring配置文件-->
    <bean id="student" class="com.spring.pojo.Student">
       <!--空值注入-->    
        <property name="kong">
            <null></null>
        </property>
    </bean>



8. property类型注入

 //实体类
 private Properties info;
   <!--spring配置文件-->
    <bean id="student" class="com.spring.pojo.Student">
       <!--property类型注入-->    
        <property name="info">
            <props>
                <prop key="admin">adminxxx@xxx</prop>
            </props>
        </property>
    </bean>





二:p命名空间方式依赖注入

使用命名空间需要先导入约束: xmlns:p=“http://www.springframework.org/schema/p”

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>
  1. p命名注入
  //实体类
    private String name;
    private int age;
<!--spring配置文件-->
<bean id="user" class="com.spring.pojo.User" p:name="admin1" p:age="99"/>





三:c命名空间方式依赖注入

约束: xmlns:c=“http://www.springframework.org/schema/c”

<?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"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>
<!--bean配置,c依赖构造器,pojo中需要有参构造-->
<bean id="user" class="com.spring.pojo.User" c:name="name01" c:age="88"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Deeeelete

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

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

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

打赏作者

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

抵扣说明:

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

余额充值