spring学习笔记(三) --- bean中的引用赋值和数组/集合赋值

spring学习笔记(三) — bean中的引用赋值和数组/集合赋值

引用赋值

如果一个对象的某个成员需要使用另一个对象来赋值的话,在spring中的配置文件中只需要用ref引用另一个bean的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"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <bean id="person" class="com.beanlife.Person">
        <property name="name" value="森头"/>
        <!-- 引用下面Date的id -->
        <property name="birthday" ref="bir"/>
    </bean>

    <bean id="bir" class="java.util.Date"/>
</beans>
package com.beanlife;

import java.util.Date;

public class Person {
    private String name;
    private Date birthday;


    public final Date getBirthday() {
        return birthday;
    }

    public final void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public final String getName() {
        return name;
    }

    public final void setName(String name) {
        this.name = name;
    }

    public void sayHi() {
        System.out.println("你好, 我是" + name + "\n我的生日是" + birthday);
    }
}
package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.beanlife.Person;


public class Test {

    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) ac.getBean("person");
        person.sayHi();


    }

}

这里写图片描述

数组赋值

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

    <bean id="person" class="com.beanlife.Person">
        <property name="name" value="森头"/>
        <property name="friends">
            <list>
                <value>李雷</value>
                <value>韩梅梅</value>
            </list>
        </property>
    </bean>
</beans>
package com.beanlife;


public class Person {
    private String name;
    private String[] friends;


    public final String[] getFriends() {
        return friends;
    }

    public final void setFriends(String[] friends) {
        this.friends = friends;
    }

    public final String getName() {
        return name;
    }

    public final void setName(String name) {
        this.name = name;
    }

    public void sayHi() {
        System.out.println("你好, 我是" + name);
    }
}
package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.beanlife.Person;


public class Test {

    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) ac.getBean("person");
        person.sayHi();

        for(String f : person.getFriends()) {
            System.out.print(f + ",");
        }
        System.out.println("都是我的好朋友!");

    }

}

这里写图片描述

list赋值

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

    <bean id="person" class="com.beanlife.Person">
        <property name="name" value="森头"/>
        <property name="friends">
            <list>
                <ref bean="friend1"/>
                <ref bean="friend2"/>
            </list>
        </property>
    </bean>

    <bean id="friend1" class="com.beanlife.Person">
        <property name="name" value="小红"/>
    </bean>

    <bean id="friend2" class="com.beanlife.Person">
        <property name="name" value="小苗"/>
    </bean>

</beans>
package com.beanlife;

import java.util.List;

public class Person {
    private String name;
    private List<Person> friends;

    public final List<Person> getFriends() {
        return friends;
    }

    public final void setFriends(List<Person> friends) {
        this.friends = friends;
    }

    public final String getName() {
        return name;
    }

    public final void setName(String name) {
        this.name = name;
    }

    public void sayHi() {
        System.out.println("你好, 我是" + name);
    }
}
package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.beanlife.Person;


public class Test {

    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) ac.getBean("person");
        person.sayHi();

        for(Person f : person.getFriends()) {
            System.out.print(f.getName() + ",");
        }
        System.out.println("都是我的好朋友!");

    }

}

这里写图片描述

map赋值

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

    <bean id="person" class="com.beanlife.Person">
        <property name="name" value="森头"/>
        <property name="map">
            <map>
                <entry key="第一个" value-ref="friend1"/>
                <entry key="第二个" value-ref="friend2"/>
            </map>
        </property>
    </bean>

    <bean id="friend1" class="com.beanlife.Person">
        <property name="name" value="小红"/>
    </bean>

    <bean id="friend2" class="com.beanlife.Person">
        <property name="name" value="小苗"/>
    </bean>
</beans>
package com.beanlife;

import java.util.Map;

public class Person {
    private String name;
    private Map<String, Person> map;


    public final Map<String, Person> getMap() {
        return map;
    }

    public final void setMap(Map<String, Person> map) {
        this.map = map;
    }

    public final String getName() {
        return name;
    }

    public final void setName(String name) {
        this.name = name;
    }

    public void sayHi() {
        System.out.println("你好, 我是" + name);
    }
}
package com.test;

import java.util.Map.Entry;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.beanlife.Person;


public class Test {

    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) ac.getBean("person");
        person.sayHi();


        for(Entry<String, Person> entry : person.getMap().entrySet()) {
            System.out.print("key = " + entry.getKey());
            System.out.println("\tvalue = " + entry.getValue().getName());
        }

    }

}

这里写图片描述

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
注册 elasticsearchRestTemplate Bean 的方法可以通过使用 spring-boot-starter-data-elasticsearch 提供的 ElasticsearchRestTemplateAutoConfiguration 类来完成。以下是一个示例: ```java import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientProperties; import org.springframework.boot.autoconfigure.elasticsearch.RestClientBuilderCustomizer; import org.springframework.boot.autoconfigure.elasticsearch.RestClientProperties; import org.springframework.boot.autoconfigure.elasticsearch.RestClientPropertiesBuilderCustomizer; import org.springframework.boot.autoconfigure.elasticsearch.RestHighLevelClientBuilderFactory; import org.springframework.boot.autoconfigure.elasticsearch.RestHighLevelClientFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; @Configuration public class ElasticsearchConfig { @Bean public ElasticsearchRestTemplate elasticsearchRestTemplate( RestHighLevelClientFactory clientFactory, RestHighLevelClientBuilderFactory clientBuilderFactory) { return new ElasticsearchRestTemplate(clientFactory, clientBuilderFactory); } @Bean public RestClientBuilderCustomizer restClientBuilderCustomizer(RestClientProperties properties) { return (builder) -> { // 自定义 RestClientBuilder 的配置 // 可以根据需要进行配置,例如设置连接超时时间、认证等 builder.setRequestConfigCallback((requestConfigBuilder) -> { requestConfigBuilder.setConnectTimeout(properties.getConnectionTimeout()); // 其他自定义配置... return requestConfigBuilder; }); }; } @Bean public RestClientPropertiesBuilderCustomizer restClientPropertiesBuilderCustomizer( ElasticsearchRestClientProperties properties) { return (builder) -> { // 自定义 RestClientProperties 的配置 // 可以根据需要进行配置,例如设置连接池大小、连接保持活动时间等 builder.setMaxRetryTimeout(properties.getMaxRetryTimeout()); // 其他自定义配置... }; } } ``` 在上述示例,我们通过定义一个 `ElasticsearchRestTemplate` 的 bean 来注册 `elasticsearchRestTemplate`。同时,我们也可以通过自定义 `RestClientBuilderCustomizer` 和 `RestClientPropertiesBuilderCustomizer` 来对 RestClient 进行一些自定义配置。 你可以根据实际需求在 `restClientBuilderCustomizer` 和 `restClientPropertiesBuilderCustomizer` 方法添加适当的配置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值