Spring_02_配置文件_import和name

前文:Spring_概述与Hello world

 

import

为方便管理配置文件,推荐使用import来规划配置文件。

在applicationContext.xml中,通过配置<import>的resource来导入配置文件。

1、默认情况下,是使用相对路径来寻找配置文件。

(相对于applicationContext.xml)

2、Spring提供了前缀标记用于辅助查找配置文件。

[file:]:使用文件系统的路径方式查找。

[classpath:]:从classpath后查找。推荐使用该方式。

  • 演示

applicationContext.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">
    
   <import resource="classpath:com/hanaii/hello/hello.xml"/>
    
</beans>

hello.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="hello" class="com.hanaii.hello.Hello" />
</beans>

 

name

Spring配置文件中,要求<bean>中的id属性值是唯一的。

若bean需要其他的名字,可以配置name属性。

1、name属性可通过逗号、分号、空格来分割多个名字。

2、可通过BeanFactory的getAlias方法获取一个bean的所有名字。

3、一般使用id即可。在SpringMVC,可在name中配置多个名字来代表映射的URL地址。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Cloud Feign中配置FastJson序列化,您需要完成以下步骤: 1. 添加FastJson依赖 在项目的pom.xml文件中,添加FastJson依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> ``` 2. 创建FastJson转换器 创建一个FastJson转换器类,继承自Spring的HttpMessageConverter接口,并实现其方法。代码如下: ```java import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.parser.Feature; import com.alibaba.fastjson.serializer.SerializerFeature; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Type; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; public class FastJsonHttpMessageConverter implements HttpMessageConverter<Object> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private SerializerFeature[] serializerFeatures = new SerializerFeature[0]; private Feature[] parserFeatures = new Feature[0]; @Override public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) { return true; } @Override public boolean canWrite(Type type, Class<?> aClass, MediaType mediaType) { return true; } @Override public List<MediaType> getSupportedMediaTypes() { List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8); mediaTypes.add(MediaType.APPLICATION_JSON); mediaTypes.add(MediaType.TEXT_PLAIN); return mediaTypes; } @Override public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException { InputStream inputStream = inputMessage.getBody(); return JSON.parseObject(inputStream, DEFAULT_CHARSET, type, parserFeatures); } @Override public void write(Object o, Type type, MediaType mediaType, HttpOutputMessage outputMessage) throws IOException { HttpHeaders headers = outputMessage.getHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); String jsonString = JSON.toJSONString(o, serializerFeatures); OutputStream outputStream = outputMessage.getBody(); outputStream.write(jsonString.getBytes(DEFAULT_CHARSET)); outputStream.flush(); } public SerializerFeature[] getSerializerFeatures() { return serializerFeatures; } public void setSerializerFeatures(SerializerFeature[] serializerFeatures) { this.serializerFeatures = serializerFeatures; } public Feature[] getParserFeatures() { return parserFeatures; } public void setParserFeatures(Feature[] parserFeatures) { this.parserFeatures = parserFeatures; } } ``` 3. 配置FastJson转换器 在Spring的配置类中,创建一个FastJson转换器的bean,并将其注册到Spring的HttpMessageConverters中。代码如下: ```java import com.alibaba.fastjson.serializer.SerializerFeature; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate; import java.util.ArrayList; import java.util.List; @Configuration public class FeignConfiguration { @Bean public HttpMessageConverter fastJsonHttpMessageConverter() { return new FastJsonHttpMessageConverter(); } @Bean public RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate(); List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); converter.setObjectMapper(new ObjectMapper()); messageConverters.add(converter); messageConverters.add(fastJsonHttpMessageConverter()); restTemplate.setMessageConverters(messageConverters); return restTemplate; } } ``` 4. 配置Feign Client 在Feign Client的配置类中,使用@FeignClient注解的configuration属性,将FastJson转换器的bean引入到Feign Client中。代码如下: ```java import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableFeignClients(defaultConfiguration = {FeignConfiguration.class}) public class FeignClientConfiguration { } ``` 完成以上步骤之后,您就可以在Spring Cloud Feign中使用FastJson作为序列化工具了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值