Spring 配置使用介绍

Posted 戴泽supp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 配置使用介绍相关的知识,希望对你有一定的参考价值。

Spring 配置使用介绍

文章目录

一、集合类型注入

1、注解形式

package com.apress.prospring5.ch3.annotated;

import com.apress.prospring5.ch3.ContentHolder;
import org.springframework.stereotype.Service;

/**
 * Created by iuliana.cosmina on 2/19/17.
 */
@Service("lyricHolder")
public class LyricHolder implements ContentHolder

	private String value = "'You be the DJ, I'll be the driver'";

	@Override public String toString() 
		return "LyricHolder:  " + value + "";
	


package com.luo.spring.guides.iocdi.collections.annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

@Service("injectCollection")
public class CollectionInjection 

    /**
     *      @Resource(name="map") is equivalent with @Autowired @Qualifier("map")
     */
    @Autowired
    @Qualifier("map")
    private Map<String, Object> map;

    @Resource(name="props")
    private Properties props;

    @Resource(name="set")
    private Set set;
    
    @Resource(name="list")
    private List list;

    public void displayInfo() 
        System.out.println("Map contents:\\n");
        map.entrySet().stream().forEach(e -> System.out.println("Key: " + e.getKey() + " - Value: " + e.getValue()));

        System.out.println("\\nProperties contents:\\n");
        props.entrySet().stream().forEach(e -> System.out.println("Key: " + e.getKey() + " - Value: " + e.getValue()));

        System.out.println("\\nSet contents:\\n");
        set.forEach(obj -> System.out.println("Value: " + obj));

        System.out.println("\\nList contents:\\n");
        list.forEach(obj -> System.out.println("Value: " + obj));
    



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

    <context:component-scan 
          base-package="com.apress.prospring5.ch3.annotated"/>

    <util:map id="map" map-class="java.util.HashMap">
        <entry key="someValue" value="It's a Friday, we finally made it"/>
        <entry key="someBean" value-ref="lyricHolder"/>
    </util:map>
    
    <util:properties id="props">
        <prop key="firstName">John</prop>
        <prop key="secondName">Mayer</prop>
    </util:properties>
    
    <util:set id="set" set-class="java.util.HashSet">
        <value>I can't believe I get to see your face</value>
        <ref bean="lyricHolder"/>
    </util:set>	
    
    <util:list id="list" list-class="java.util.ArrayList">
        <value>You've been working and I've been waiting</value>
        <ref bean="lyricHolder"/>
    </util:list>
</beans>

测试

package com.luo.spring.guides.iocdi.collections.annotation;

import com.luo.spring.guides.iocdi.annotation.setter.SetterInjectionConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

/**
 * @author : archer
 * @date : Created in 2022/11/30 17:41
 * @description :
 */
public class Main 

    public static void main(String... args) 
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        ctx.load("classpath:iocdi/injection/collections/app-context-annotation.xml");
        ctx.refresh();

        CollectionInjection instance = (CollectionInjection) ctx.getBean("injectCollection");
        instance.displayInfo();

        ctx.close();
    


输出

Map contents:

Key: map - Value: someBean=LyricHolder: ‘You be the DJ, I’ll be the driver’, someValue=It’s a Friday, we finally made it

Properties contents:

Key: firstName - Value: John
Key: secondName - Value: Mayer

Set contents:

Value: LyricHolder: ‘You be the DJ, I’ll be the driver’
Value: I can’t believe I get to see your face

List contents:

Value: You’ve been working and I’ve been waiting
Value: LyricHolder: ‘You be the DJ, I’ll be the driver’

2、xml 形式

package com.luo.spring.guides.iocdi.collections;

public interface ContentHolder 
package com.luo.spring.guides.iocdi.collections;


/**
 * Created by iuliana.cosmina on 2/19/17.
 */
public class LyricHolder implements ContentHolder
	private String value = "'You be the DJ, I'll be the driver'";


	@Override public String toString() 
		return "LyricHolder:  " + value + "";
	

package com.luo.spring.guides.iocdi.collections;

import org.springframework.context.support.GenericXmlApplicationContext;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class CollectionInjection 

	private Map<String, Object> map;
	private Properties props;
	private Set set;
	private List list;

	public void displayInfo() 
		System.out.println("Map contents:\\n");
		map.entrySet().stream().forEach(e -> System.out.println("Key: " + e.getKey() + " - Value: " + e.getValue()));

		System.out.println("\\nProperties contents:\\n");
		props.entrySet().stream().forEach(e -> System.out.println("Key: " + e.getKey() + " - Value: " + e.getValue()));

		System.out.println("\\nSet contents:\\n");
		set.forEach(obj -> System.out.println("Value: " + obj));

		System.out.println("\\nList contents:\\n");
		list.forEach(obj -> System.out.println("Value: " + obj));
	

	public void setList(List list) 
		this.list = list;
	

	public void setSet(Set set) 
		this.set = set;
	

	public void setMap(Map<String, Object> map) 
		this.map = map;
	

	public void setProps(Properties props) 
		this.props = props;
	

<?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.xsd">

    <bean id="lyricHolder" class="com.luo.spring.guides.iocdi.collections.LyricHolder"/>
    
    <bean id="injectCollection" class="com.luo.spring.guides.iocdi.collections.CollectionInjection">
        <property name="map">
            <map>
                <entry key="someValue" value="It's a Friday, we finally made it"/>
                <entry key="someBean" value-ref="lyricHolder"/>
            </map>
        </property>
        <property name="props">
            <props>
                <prop key="firstName">John</prop>
                <prop key="secondName">Mayer</prop>
            </props>
        </property>
        <property name="set">
            <set>
                <value>I can't believe I get to see your face</value>
                <ref bean="lyricHolder"/>
            </set>
        </property>
        <property name="list">
            <list>
                <value>You've been working and I've been waiting</value>
                <ref bean="lyricHolder"/>
            </list>
        </property>
    </bean>
</beans>

测试

package com.luo.spring.guides.iocdi.collections;

import org.springframework.context.support.GenericXmlApplicationContext;

/**
 * @author : archer
 * @date : Created in 2022/11/30 17:27
 * @description :
 */
public class Main 

    public static void main(String... args) 
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        ctx.load("classpath:iocdi/injection/collections/app-context-xml.xml");
        ctx.refresh();

        CollectionInjection instance = (CollectionInjection) ctx.getBean("injectCollection");
        instance.displayInfo();

        ctx.close();
    


输出

Map contents:

Key: someValue - Value: It’s a Friday, we finally made it
Key: someBean - Value: LyricHolder: ‘You be the DJ, I’ll be the driver’

Properties contents:

Key: firstName - Value: John
Key: secondName - Value: Mayer

Set contents:

Value: I can’t believe I get to see your face
Value: LyricHolder: ‘You be the DJ, I’ll be the driver’

List contents:

Value: You’ve been working and I’ve been waiting
Value: LyricHolder: ‘You be the DJ, I’ll be the driver’

二、alias 使用

1、xml 形式

<?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.xsd">

    <bean id="john" name="jon johnny,jonathan;jim" class="java.lang.String"/>

    <alias name="john" alias="ion"/>

    <bean name="jon1 johnny1,jonathan1;jim1" class="java.lang.String"/>

    <bean id="jon1 johnny1,jonathan1;jim1" class="java.lang.String"/>
</beans>

测试

package com.luo.spring.guides.iocdi.alias;

import org.springframework.context.support.GenericXmlApplicationContext;

import java.util.Arrays;
import java.util.Map;

/**
 * @author : archer
 * @date : Created in 2022/11/30 19:09
 * @description :
 */
public class Main <

以上是关于Spring 配置使用介绍的主要内容,如果未能解决你的问题,请参考以下文章

Spring介绍及配置(XML文件配置和注解配置)

Spring介绍及配置(XML文件配置和注解配置)

Spring 使用介绍—— 零配置

Spring介绍

Spring框架介绍和IoC容器中Bean配置(Spring框架)

spring的配置介绍和bean的管理方式