[英]Spring bean definition class name is null when using JavaConfig
[英]Bean property list definition in JavaConfig class
我正在嘗試將我的context.xml配置文件轉換為純javaConfig,並且在大多數情況下,我認為我已經找到了答案,但有一件事對我來說確實是一個黑魔法。
我的context.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:property-placeholder location="classpath:app.properties"
system-properties-mode="OVERRIDE" />
<context:component-scan
base-package="mypackage.servlets.dao,mypackage.servlets" />
<mvc:annotation-driven />
<alias name="${dao}" alias="daoType"/>
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
</list>
</property>
</bean>
</beans>
我設法更換了所有東西,但:
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
</list>
</property>
</bean>
我得到的JavaConfig類如下所示:
package mypackage.servlets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import mypackage.servlets.dao.LibraryDAO;
@Configuration
@ComponentScan("mypackage.servlets.dao,mypackage.servlets")
@EnableWebMvc
public class ConfigClass {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Autowired
private ApplicationContext context;
@Bean
public LibraryDAO MyServiceAlias(@Value("${dao}") String qualifier) {
return (LibraryDAO) context.getBean(qualifier);
}
@Bean
public ContentNegotiatingViewResolver myContentNegotiation()
{
return new ContentNegotiatingViewResolver();
}
@Bean
public MappingJackson2JsonView myJackson()
{
return new MappingJackson2JsonView();
}
}
我試圖為我的問題找到答案的@Bean
錯了我的意思, @Bean
屬性經常被誤認為app.properties
文件中的屬性。 我是SPRING的新手,試圖了解javaConfig類。 我發現,可以這樣注入簡單的依賴項:
@Bean
public ContentNegotiatingViewResolver myContentNegotiation()
{
return new ContentNegotiatingViewResolver(myJackson());
}
@Bean
public MappingJackson2JsonView myJackson()
{
return new MappingJackson2JsonView();
}
但是在這種情況下,我的IDE拋出一個錯誤,即ContentNegotiatingViewResolver
僅具有默認構造函數。 和總結,我想知道如何定義這個ContentNegotiatingViewResolver
在javaConfig。
您需要使用ContentNegotiatingViewResolver.setViewResolvers(list),它接受視圖解析器列表。 請檢查以下配置。
* Configure ContentNegotiationManager
*/
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.ignoreAcceptHeader(true).defaultContentType(
MediaType.TEXT_HTML);
}
/*
* Configure ContentNegotiatingViewResolver
*/
@Bean
public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
resolver.setContentNegotiationManager(manager);
// Define all possible view resolvers
List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
resolvers.add(jsonViewResolver());
resolver.setViewResolvers(resolvers);
return resolver;
}
/*
* Configure View resolver to provide JSON output using JACKSON library to
* convert object in JSON format.
*/
@Bean
public ViewResolver jsonViewResolver() {
return new JsonViewResolver();
}
在此處查看本教程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.