关键词:
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: MayerSet contents:
Value: LyricHolder: ‘You be the DJ, I’ll be the driver’
Value: I can’t believe I get to see your faceList 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: MayerSet 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 <springspring入门介绍(代码片段)
文章目录1.Spring介绍1.1Spring是什么?1.2IoC是什么?1.3DI是什么?1.4Spring的核心功能1.5Spring的应用上下文2.Spring项目的创建和使用2.1创建Maven项目2.2存储Bean对象2.3获取并使用Bean对象3.Spring更简单的读取和存储对象方式3.1存... 查看详情
spring注解开发--spring新注解(完全替换spring配置文件使用配置类)(代码片段)
1.Spring新注解使用原始注解还不能全部替代xml配置文件,还需要使用注解替代的配置如下:新注解+原始注解就可以完全脱离配置文件开发1.1新注解介绍1.2使用新注解进行开发1.2.1新建数据库配置文件创建数据源文件:jdbc... 查看详情
spring使用xml配置开发springaop(代码片段)
XML方式开发AOP与注解开发原理是相同的,所以这里主要介绍一些用法即可。这里需要在XML中引入AOP的命名空间,所以先来了解一下AOP可配置的元素 代码清单:切面类packagecom.ssm.chapter11.xml.aspect;publicclassXmlAspectpublicvoid... 查看详情
spring的介绍引用spring的依赖包resources下xml的配置简单示例(代码片段)
文章目录前言一、spring简介二、spring特点三、IOC概念四、DI概念五、使用spring实现一个简单程序(在jdk,还有maven安装好的前提下)1.引用spring的依赖包2.引用springcontext的依赖包3.编写dog类4.在resources文件夹中创建spring-2... 查看详情
day02-spring基本介绍02(代码片段)
Spring基本介绍025.简单模拟Spring基于XML配置的程序5.1需求说明自己写一个简单的Spring容器,通过读取beans.xml,获取第一个Javabean:Monster的对象,给该对象属性赋值,放入到容器中,并输出该对象信息也就是说,不使用spring原生框... 查看详情
spring框架学习----声明式事务管理(代码片段)
文章目录Spring框架学习(十)----声明式事务管理一、事务介绍二、Spring-Mybatis使用事务0、前置准备工作1、声明式事务(xml配置)2、注解开发使用事务三、事务相关的xml配置模板Spring框架学习(十)----声... 查看详情
springioc基本介绍(代码片段)
一、SpringIOC入门示例通过代码项目演示SpringIOC示例需要以下几个过程:下载Spring5框架Idea开发工具创建Java工程导入Spring5相关的jar包编写普通演示类和普通方法编写Spring配置文件编写SpringIOC测试代码1.1、普通类和普通方法public... 查看详情
mybatis源码阅读mybatis与spring整合原理(代码片段)
在日常使用中,mybatis都是和spring整合使用的。本文将会从源码角度介绍mybatis与spring整合原理。整合配置在正式介绍之前,我们先来回忆一下将mybatis和spring框架进行整合需要进行哪些配置。为了看起来更加直观,我们... 查看详情
nacos介绍及使用(代码片段)
一、Nacos介绍1、Nacos是SpringCloudAlibaba架构中最重要的组件。2、Nacos是一个更易于帮助构建云原生应用的动态服务发现、配置和服务管理平台,提供注册中心、配置中心和动态DNS服务三大功能。能够无缝对接Springcloud、Spring、Dub... 查看详情
spring学习-ioc-xml关键配置介绍(代码片段)
介绍spring的xml配置项较多,这里作一个综述配置示例如下:<?xmlversion="1.0"encoding="UTF-8"?><!--xml的版本和编码方式,必须放在开始--><!--参考:https://www.cnblogs.com/zhao1949/ 查看详情
springresource框架体系介绍(代码片段)
Resource介绍在使用spring作为容器进行项目开发中会有很多的配置文件,这些配置文件都是通过Spring的Resource接口来实现加载,但是,Resource对于所有低级资源的访问都不够充分。例如,没有标准化的URL实现可用于访问需要从类路径... 查看详情
spring配置文件的详细介绍(代码片段)
目录1.Spring的配置文件的命名2.Spring配置文件中有什么3.set注入4.构造注入1.Spring的配置文件的命名答:Spring的配置文件是放在resources文件夹下面的,一般我们都会给他起一个默认的名字:applicationContext.xml。如下图所示&... 查看详情
一spring介绍以及版本历史演进(代码片段)
背景Spring的第一个版本于2002年10月发布,由一个带有易于配置和使用的控制反转(IoC)容器的小型内核组成。多年来,Spring已经成为JavaEnterpriseEdition(Java企业版JEE)的主要替代品,并且发展成一个由许... 查看详情
spring零配置之@configuration注解详解(代码片段)
@Configuration介绍Spring3.0之前要使用Spring必须要有一个xml配置文件,这也是Spring的核心文件,而Spring3.0之后可以不要配置文件了,通过注解@Configuration完全搞定。@Configuration即用来代替Spring配置文件的,它就是一个@Component组件,接... 查看详情
ipv6-基础介绍(代码片段)
?本系列文章:读源码,我们可以从第一行读起你知道Spring是怎么解析配置类的吗?配置类为什么要添加@Configuration注解?br/>本系列文章:读源码,我们可以从第一行读起你知道Spring是怎么解析配置类的吗?配置类为什么要添加@C... 查看详情
spring更简单的使用方法(代码片段)
...面介绍了一些最基本的存储和读取Bean对象的方法,但是在Spring中已经基本不这样使用了,因此这一篇博客就来介绍一下更简单的存储和读取Bean对象的方法而想要更简单的存储和读取最核心的就是使用注解,下面一起来看看吧!目录... 查看详情
数据库链接池durid的配置参数详解(代码片段)
...要的,对于值的大小,需要根据自己服务器情况,设置。spring.datasource.initialSize=5//数据库连接池初始化连接数spring.datasource.minIdle=5//数据库连接池中最小连接数,如果长时间不使用,连接池中也应该保持最小连接数个连接。spring.... 查看详情
spring项目xml文件使用常见介绍(代码片段)
1项目pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance 查看详情