创建一个Spring 4 MVC项目,带有注释,没有xml文件 [英] Creating a Spring 4 MVC project with annotations and no xml files
问题描述
我已经下载最新的STS和我确实看到了创建Spring MVC项目的选项。但是它基于Spring 3,仍然使用XML文件。从我所看到的,从Spring 3开始,它看起来就像一个没有XML文件的方法。我更喜欢对XML文件的注释。
如何创建一个基于注释的Spring MVC 4应用程序,并最终依赖于xml文件?
编辑:
我想创建一个web项目
这是一个完整的java配置的一个例子。您将需要:
- 扩展一个类
AbstractAnnotationConfigDispatcherServletInitializer
替换旧的web.xml
文件 - 一个或多个
@Configuration
注释类(es)进行初始化一个或多个@Configuration
annotated class(es)来初始化DispatcherServlet
context(替换旧的dispatcher-servlet.xml
)
这是 web.xml
:
public class WebAppConf extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?> [ ] getRootConfigClasses(){
//声明根上下文配置类
返回新的类<?> [] {RootConf.class};
@Override
protected Class<?> [] getServletConfigClasses(){
//声明servlet上下文配置类
return new Class& ?> [] {ServletConf.class};
}
@Override
protected String [] getServletMappings(){
// DispatcherServlet的映射
return new String [] {/} ;
}
@Override
protected void customizeRegistration(动态注册){
//附加配置,此处为MultipartConfig
super.customizeRegistration(注册);
MultipartConfigElement multipartConf = new MultipartConfigElement(,200000L,-1L,0);
registration.setMultipartConfig(multipartConf);
}
}
RootConf
将声明商业模式,服务和豆豆,并不在这里显示。
ServletConf
声明控制器和servlet配置:
@Configuration
@EnableWebMvc
//声明在哪里找到注释控制器
@ComponentScan({org.example.web})
public class ServletConf extends WebMvcConfigurerAdapter {
@Bean
MultipartResolver multipartResolver(){
返回新的StandardServletMultipartResolver();
}
@Bean
ViewResolver internalViewResolver(){
//视图解析器bean ...
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix(/ WEB-INF / jsp /);
resolver.setSuffix(。jsp);
return resolver;
}
}
如上所述,它是squeletal,但它来了从一个工作的最小化的例子,所以你应该能够从这开始,并随意扩展。在我的示例中,上述三个类生活在一个 org.example.config
包中,永远不会被扫描以自动检测其他配置类或注释的bean。
希望它有帮助...
I'm new to Spring MVC and Hibernate. I'm trying to start a project by following tutorials but I have been running into problems as my project structure is not consistent with the tutorials I am reading.
I have downloaded the latest STS and I do see the option of creating an Spring MVC project. However it is based on Spring 3 and still uses XML files. From what I have read it looks like there is a way to do it without XML files since Spring 3. I prefer annotations over XML files greatly.
How can I create a Spring MVC 4 application that is based on annotations and relies on xml files minimally?
EDIT: I want to create a web project
Here is a squeletal example of full java configuration. You will need :
- a class extending
AbstractAnnotationConfigDispatcherServletInitializer
to replace the oldweb.xml
file - one or more
@Configuration
annotaded class(es) to initialize the root context (replaces the oldapplicationContext.xml
) - one or more
@Configuration
annotaded class(es) to initialize theDispatcherServlet
context (replaces the olddispatcher-servlet.xml
)
This is the web.xml
:
public class WebAppConf extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// declare root context configuration classes
return new Class<?>[]{ RootConf.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
// declare servlet context configuration classes
return new Class<?>[]{ ServletConf.class };
}
@Override
protected String[] getServletMappings() {
// mapping of DispatcherServlet
return new String[]{"/"};
}
@Override
protected void customizeRegistration(Dynamic registration) {
// additional configuration, here for MultipartConfig
super.customizeRegistration(registration);
MultipartConfigElement multipartConf = new MultipartConfigElement("", 200000L, -1L, 0);
registration.setMultipartConfig(multipartConf);
}
}
RootConf
will declare business model, service and dao beans and is not shown here.
ServletConf
declares the controllers and servlet configuration :
@Configuration
@EnableWebMvc
// declare where to find annotated controllers
@ComponentScan({"org.example.web"})
public class ServletConf extends WebMvcConfigurerAdapter {
@Bean
MultipartResolver multipartResolver() {
return new StandardServletMultipartResolver();
}
@Bean
ViewResolver internalViewResolver() {
// the view resolver bean ...
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
}
As said above, it is squeletal, but it comes from a working minimal example so you should be able to start with that and extend it at will. In my example, the above three classes live in a org.example.config
package that will never be scanned for autodetecting other configuration classes or annotated beans.
Hope it helps ...
这篇关于创建一个Spring 4 MVC项目,带有注释,没有xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!