Spring-how to assign values to different data type parameters

bean tag

Function:
Used to configure the object for spring to create, by default it calls the parameterless constructor in the class. If there is no parameterless constructor, it cannot be created successfully.
Adding id and name attributes to the bean tag can be obtained by calling the getBean (id attribute value or name attribute value) method to obtain the object in the Spring IOC container; but you need to pay attention to the following points:

  1. The id attribute value can only define one identifier, and the name attribute value can be defined with a semicolon (";"), a space (" ") or a comma (",") to define multiple, as shown in the following code
<bean id="helloWorld" name="hello;world" class="com.spring.test.HelloWorld"></bean>
可通过如下方式获取Spring IOC中的对象:
ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext("application.xml");
System.out.println(application.getBean("helloWorld"));//注意用这种方法获得的对象是Object类型的
System.out.println(application.getBean("hello"));
System.out.println(application.getBean("world"));
application.close();
  1. The id and name attribute values ​​cannot be repeated;
  2. There can be no id and name attributes in the bean tag. At this time, the object in the Spring IOC container can be obtained by calling the getBean (Class object or full name of the class) method, as shown below:
<bean class="com.spring.test.HelloWorld"></bean>
可通过如下方式获取Spring IOC中的对象:
ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext("application.xml");
System.out.println(application.getBean(HelloWorld.class));
System.out.println(application.getBean("com.spring.test.HelloWorld"));
application.close();

constructor-arg subtag

Constructor -arg subtag : Specify which constructor is used when creating a class object. Each pair or each constructor-arg subtag configures a parameter value in the parameter list; if no subtag is configured, the parameterless constructor instance is used by default Object
attributes

name属性:通过参数名找到参数列表中对应参数
index属性:通过参数在参数列表中的索引找到参数列表中对应参数,index从0开始:
type属性:通过参数数据类型找到参数列表中对应参数
value属性:设置参数列表参数对应的值,用于设定基本数据类型和String类型的数据
package com.spring.test;
public class Student {
    
    
	public Student(String name, double score) {
    
    
		System.out.println(name + ":" + score);
	}
}
第一种配置方式:按照参数在参数列表中的正常顺序进行配置,此时不需要设置name、index或type属性:
<bean class="com.spring.test.Student">
	<constructor-arg value="张三" ></constructor-arg>
	<constructor-arg value="100" ></constructor-arg>
</bean>
第二种配置方式:如果不按照参数在参数列表中的正常顺序进行配置,则需要设定name、index或type属性,否则配置错误:
	a、name属性
<bean class="com.spring.test.Student">
	<constructor-arg name="score" value="100"></constructor-arg>
	<constructor-arg name="name" value="张三"></constructor-arg>
</bean>

	b、index属性
<bean class="com.spring.test.Student">
	<constructor-arg index="1" value="100"></constructor-arg>
	<constructor-arg index="0" value="张三"></constructor-arg>
</bean>
	c、type属性
<bean class="com.spring.test.Student">
	<!-- 此时type可以为java.lang.Double吗?——不可以,要和参数数据类型严格一致 -->
	<constructor-arg value="100" type="double"></constructor-arg>
	<!-- 此时type可以为java.lang.Object吗?——不可以,要和参数数据类型严格一致 -->
	<constructor-arg value="张三" type="java.lang.String"></constructor-arg>
</bean>

Assign values ​​to parameters of different data types through the construction method

No-parameter construction method

Using the default parameterless constructor, the class object will be created based on the default parameterless constructor. If there is no default parameterless constructor in the bean, the creation will fail.
Such as:

<bean id = "Student" class = "com.jd.vo.Student"></bean>

At this time, there must be a constructor with no parameters in the Student.

Parameter construction method

1. Basic data types or String use value attribute assignment

public Student(int age,String name){
    
    
	System.out.println(age+","+name);
}
<bean  class="com.jd.vo.Student">
    <constructor-arg name = "age" value="12"></constructor-arg>
    <constructor-arg name = "name" value="Jom"></constructor-arg>
</bean>

Array type

public Student(int[] age){
    
    
   for (int s:age) {
    
    
        System.out.println(s);
    }
}
<!--4.array 使用array标签-->
<bean name="in" class="java.lang.Integer">
    <constructor-arg value="20"></constructor-arg>
</bean>
<bean class="com.jd.vo.Student">
    <constructor-arg>
        <array>
            <value>10</value>
            <ref bean="in"></ref>
            <bean class="java.lang.Integer">
                <constructor-arg value="18"></constructor-arg>
            </bean>
            <value>15</value>
        </array>
    </constructor-arg>
</bean>

Class type

public Student(Date birth){
    
    
  System.out.println(birth);
}
<!--2.类类型,则使用ref属性赋值,同样的是给name赋值,但是该值是bean类型,必须在配置文件中配置。-->
<bean class = "com.jd.vo.Student">
    <constructor-arg ref="d"></constructor-arg>
</bean>

List type

//参数为List类型
public Student(List<String> student){
    
    
    for (String s:student) {
    
    
        System.out.println(s);
    }
}
<!--3.List 使用list标签-->
<bean name="ss" class="java.lang.String">
    <constructor-arg value="王三"></constructor-arg>
</bean>
<bean class = "com.jd.vo.Student">
    <constructor-arg>
        <list>
            <value>张三</value>
            <value>李四</value>
            <value>王五</value>
            <bean class="java.lang.String"></bean>
            <ref bean="ss"></ref>
        </list>
    </constructor-arg>
</bean>

Set type

public Student(Set<String> mobiles){
    
    
   for (String mobile:mobiles) {
    
    
        System.out.println(mobile);
    }
}
<!--5.set 使用set标签-->
<bean name="s" class="java.lang.String">
    <constructor-arg value="119"></constructor-arg>
</bean>
<bean class="com.jd.vo.Student">
    <constructor-arg>
        <set>
            <value>133235</value>
            <bean class="java.lang.String">
                <constructor-arg value="110"></constructor-arg>
            </bean>
            <ref bean="s"></ref>
        </set>
    </constructor-arg>
</bean>

Map type

public Student(Map<String,String> map){
    
    
    Set<String> set = map.keySet();
    for(String key : set){
    
    
        System.out.println(key+" ,"+map.get(key));
    }
}
<!--6.Map 使用map标签-->
<bean name="k" class="java.lang.String">
    <constructor-arg value="张三"></constructor-arg>
</bean>
<bean name="v" class="java.lang.String">
    <constructor-arg value="张三"></constructor-arg>
</bean>
<bean class="com.jd.vo.Student">
    <constructor-arg>
        <map>
            <entry key="王五" value="110"></entry>
            <entry key-ref="k" value-ref="v"></entry>
        </map>
    </constructor-arg>
</bean>

Properties type

public Student(Properties properties){
    
    
    System.out.println(properties.getProperty("name"));//Jim
    System.out.println(properties.getProperty("mobile"));//110
}
<!--7.Properties 使用props标签-->
<bean class="com.jd.vo.Student">
    <constructor-arg>
        <props>
            <prop key="name">Jim</prop>
            <prop key="mobile">110</prop>
        </props>
    </constructor-arg>
</bean>

Assign values ​​to different data type parameters through the set method

method one:

class Student {
    
    

    private String name;
    private int[] scores;

    @Override
    public String toString() {
    
    
        return "Student{" +
                "name='" + name + '\'' +
                ", scores=" + Arrays.toString(scores) +
                '}';
    }
    public int[] getScores() {
    
    
        return scores;
    }
    public void setScores(int[] scores) {
    
    
        this.scores = scores;
    }
    public String getName() {
    
    
        return name;
    }
    public void setName(String name) {
    
    
        this.name = name;
    }
}

public class Test {
    
    
    public static void main(String[] args) {
    
    

        //默认情况下,new ClassPathXmlApplicationContext("application.xml")执行后,Spring自动调用类中无参构造方法;
        //lazy-init = true,则不会随new ClassPathXmlApplicationContext("application.xml")执行而创建对象,什么时候获取该对象,什么时候创建
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
        System.out.println(applicationContext.getBean("Student"));
        //Student{name='Jim', scores=[98, 100, 98]}
    }
}
<!--通过set方法赋值-->
<bean id="in" class="java.lang.Integer">
    <constructor-arg value="100"></constructor-arg>
</bean>
<bean id = "Student" class="com.jd.vo.Student">
    <property name="name" value="Jim"></property>
    <property name="scores">
        <array>
            <value>98</value>
            <ref bean ="in"></ref>
            <bean class="java.lang.Integer">
                <constructor-arg value="98"></constructor-arg>
            </bean>
        </array>
    </property>
</bean>

Method Two:

Guess you like

Origin blog.csdn.net/weixin_44630656/article/details/109534172