userimage

how to configuration spring without xml file

how to configuration spring without xml file, where will configure the spring project with annotation.
userimage

To configure the front end controller follow below code with URL pattern

 

package com.configuration;

 

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

 

/**

 * Initial Startup/Spring loader.

 *

 * @author

 *

 */

public class Configuration extends AbstractAnnotationConfigDispatcherServletInitializer {

 

                @Override

                protected Class<?>[] getRootConfigClasses() {

                                return new Class[] { Resolver.class };

                }

 

                @Override

                protected Class<?>[] getServletConfigClasses() {

                                return null;

                }

 

                @Override

                protected String[] getServletMappings() {

                                return new String[] { "/" };

                }

}

 

and to configure the beans or bean configuration please follow below steps including the common configuration details:

 

package com.configuration;

 

import java.util.List;

 

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Import;

import org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver;

import org.springframework.mobile.device.DeviceResolverHandlerInterceptor;

import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver;

import org.springframework.web.method.support.HandlerMethodArgumentResolver;

import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import org.springframework.web.servlet.view.InternalResourceViewResolver;

import org.springframework.web.servlet.view.JstlView;

 

/**

 * Bean configuration and resource handler.

 *

 * @author

 *

 */

@Configuration

@EnableWebMvc

@ComponentScan(basePackages = { "com.controller", "com.configuration" })

@Import({ SpringSecurity.class })

public class Resolver extends WebMvcConfigurerAdapter {

 

                @Bean

                public CommonsMultipartResolver multipartResolver() {

                                CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();

                                commonsMultipartResolver.setMaxUploadSize(2048);

                                return commonsMultipartResolver;

                }

 

                @Override

                public void addResourceHandlers(ResourceHandlerRegistry registry) {

                                registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");

                }

 

                @Bean

                public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {

                                return new DeviceResolverHandlerInterceptor();

                }

 

                @Bean

                public DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver() {

                                return new DeviceHandlerMethodArgumentResolver();

                }

 

                @Override

                public void addInterceptors(InterceptorRegistry registry) {

                                registry.addInterceptor(deviceResolverHandlerInterceptor());

                }

 

                @Override

                public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {

                                argumentResolvers.add(deviceHandlerMethodArgumentResolver());

                }

 

                @Bean

                public LiteDeviceDelegatingViewResolver liteDeviceAwareViewResolver() {

                                InternalResourceViewResolver delegate = new InternalResourceViewResolver();

                                delegate.setViewClass(JstlView.class);

                                delegate.setPrefix("/WEB-INF/view/");

                                delegate.setSuffix(".jsp");

                                LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(delegate);

                                resolver.setMobilePrefix("mobile/");

                                resolver.setTabletPrefix("tablet/");

                                resolver.setNormalPrefix("desktop/");

                                return resolver;

                }

}

 

If you want some custom beans to create you can add in Resolver class and start working without xml file.

Answer is