注入的几个方法

本文演示如何在Spring框架中使用实体类,并通过配置文件完成依赖注入,展示实体类中不同类型的属性值,包括特殊字符、JavaBean、List、数组、Set、Map、Properties类型及空字符串、null值的注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class TestEntity {
    private String specialCharacter1; // 特殊字符值1
    private String specialCharacter2; // 特殊字符值2
    private User innerBean; // JavaBean类型
    private List<String> list; // List类型
    private String[] array; // 数组类型
    private Set<String> set; // Set类型
    private Map<String, String> map; // Map类型
    private Properties props; // Properties类型
    private String emptyValue; // 注入空字符串值
    private String nullValue = "init value"; // 注入null值
        忽略set、get
    public void showValue() {
        System.out.println("特殊字符1:" + this.specialCharacter1);
        System.out.println("特殊字符2:" + this.specialCharacter2);
        System.out.println("内部Bean:" + this.innerBean.getUsername());
        System.out.println("List属性:" + this.list);
        System.out.println("数组属性[0]:" + this.array[0]);
        System.out.println("Set属性:" + this.set);
        System.out.println("Map属性:" + this.map);
        System.out.println("Properties属性:" + this.props);
        System.out.println("注入空字符串:[" + this.emptyValue + "]");
        System.out.println("注入null值:" + this.nullValue);
    }
}

 

 

<?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-3.2.xsd 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ">
     <bean id="userDao" class="com.bdqn.dao.UserDaoImpl"></bean>
     <bean id="userService" class="com.bdqn.service.UserService">
         <!-- setter方法 -->
         <property name="dao" ref="userDao"></property>
         <!-- 构造方法注入    index编号-->
        <constructor-arg index="0" ref="userDao"></constructor-arg>         
         <!-- ref引用 -->
     </bean>
     
     <bean id="entity" class="com.bdqn.entity.TestEntity">
         <!-- 特殊字符一 -->
         <property name="specialCharacter1" >
             <value><![CDATA[&"!\/]]></value>
         </property>
         <!-- 特殊字符二 -->
         <property name="specialCharacter2" >
             <value>&amp;</value>
         </property>
         <!-- 内部bean -->
         <property name="innerBean">
             <bean class="com.bdqn.entity.User">
                 <property name="username" value="admin"></property>
                 <property name="password" value="123"></property>
             </bean>
         </property>
         <!-- list -->
         <property name="list">
             <list>
                 <value>123</value>
                 <value>456</value>
                 <value>789</value>
             </list>
         </property >
         <!-- 数组类型array --> 
         <property name="array">
             <list>
                 <value>a123</value>
                 <value>a456</value>
                 <value>a789</value>
             </list>
         </property>
         <!-- set -->
         <property name="set">
             <set>
                 <value>123</value>
                 <value>456</value>
                 <value>789</value>
                 <value>123</value>
                 <value>456</value>
                 <value>789</value>
             </set>
         </property >
         <!-- map -->
         <property name="map">
             <map>
                 <entry key="a">
                     <value>a1</value>
                 </entry>
                 <entry key="b">
                     <value>b1</value>
                 </entry>
                 <!-- 通常用这个  比较直观-->
                 <entry key="c" value="c1"></entry>
             </map>
          </property>
         <!-- Properties类型 -->
         <property name="props">
             <props>
                 <prop key="user">admin</prop>
                 <prop key="uri">/a/b/c.jsp</prop>
             </props>
         </property>
         <!-- 注入空字符串值 -->
         <property name="emptyValue"><value></value></property>
         <!-- 注入null值 -->
         <property name="nullValue"><null></null></property>
     </bean>
     
 </beans>

 

 1 public class EntityTest {
 2 
 3     @Test
 4     public void testEntity(){
 5         ApplicationContext ctx= new ClassPathXmlApplicationContext(
 6                 "applicationContext.xml");
 7         TestEntity entity = (TestEntity) ctx.getBean("entity");
 8         entity.showValue();
 9     }
10 
11 }

 

转载于:https://www.cnblogs.com/xuerong/p/4915756.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值