Spring Bean值注入 - 基于XML配置文件

1.单个属性注入
public class User {
    
    private Integer id;
    
    private String name;
    
    private Date birthday;
    
    private Double price;
    
    private boolean sex;
    
    // 包含 无参、有参、get、set方法
}
<bean id="user" class="com.test.entity.User">
    <property name="id" value="1" />
    <property name="name" value="ZhangSan" />
    <property name="price" value="10.5" />
    <property name="birthday" value="2020/02/20 20:20:20" />
    <property name="sex" value="false" />
</bean>
2.引用类型注入
public class UserService {
    
    private UserDao userDao;
        
    // ...
}
<bean id="userDao" class="com.test.dao.UserDao"></bean>
<bean id="userService" class="com.test.service.UserService">
	<property name="userDao" ref="userDao"></property>
</bean>
<!-- 注入匿名内部类 -->
<bean id="apple" class="com.test.fruit.Apple">
   <property name="applePackage">
   	<bean class="com.test.fruit.ApplePackage"></bean>
   </property>
</bean>
3.数组类型注入
public class Numbers{
    
    private String[] str;
    
}
<property name="str">
	<array>
    	<value>zhangsan</value>
        <value>lisi</value>
    </array>
</property>
4.集合类型注入
public class ListAll{
    
    private Set<String> sets;
    
    private List<String> strList;
    
    private List<Person> plist;
    
    private Map<String,String> map;
    
}
<!-- set -->
<property name="sets">
    <set>
        <value>one</value>
        <value>two</value>
        <value>three</value>
    </set>
</property>

<!-- strList -->
<property name="strList">
	<list>
    	<value>one</value>
        <value>two</value>
        <value>three</value>
    </list>
</property>

<!-- plist -->
<bean id="user" class="com.test.entity.User" scope="prototype">
<bean id="listAll" class="com.test.ListAll">
    <property name="plist">
        <list>
        	<ref bean="user"></ref>
        </list>
	</property>
</bean>

<!-- map -->
<property name="map">
    <map>
        <entry key="one" value="1" />
        <entry key="two" value="2" />
    </map>
</property>
5.构造注入
public class User {
    
    private Integer id;
    
    private String name;
    
    private Date birthday;
    
    private Double price;
    
    private boolean sex;
    
    // 包含 无参、有参、get、set方法
}
<!-- 根据下标注入 -->
<bean id="user" class="com.test.entity.User">
	<constructor-arg index="0" value="1" />
    <constructor-arg index="1" value="Tom" />
    <constructor-arg index="2" value="2020/02/20 20:20:20" />
</bean>

<!-- 根据类型注入 -->
<bean id="user" class="com.test.entity.User">
	<constructor-arg type="Integer" value="1" />
    <constructor-arg type="String" value="Tom" />
    <constructor-arg type="Date" value="2020/02/20 20:20:20" />
</bean>

<!-- 根据名称注入 -->
<bean id="user" class="com.test.entity.User">
	<constructor-arg name="id" value="1" />
    <constructor-arg name="name" value="Tom" />
    <constructor-arg name="birthday" value="2020/02/20 20:20:20" />
</bean>
附:Spring注入值的不同方式

可参考此博主博客:https://blog.csdn.net/qq_40962416/article/details/88628596


版权声明:本文为zxcbnm7089原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。