Skip to content

Spring Framework and Spring Boot Wrapper

Yash edited this page Aug 9, 2018 · 2 revisions

application-servlet.xml (vs) application.properties


ViewResolver

Here it means that when the controller returns a value, then it resolves the view like this.
EX: /WEB_INF/jsp/ + return value + .jsp

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<!-- OR -->
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name = "prefix" value = "/WEB-INF/jsp/"/>
   <property name = "suffix" value = ".jsp"/>
</bean>
spring.mvc.view.prefix= /WEB-INF/pages/
spring.mvc.view.suffix= .jsp

Context-ComponentScan

Activates various annotations to be detected in bean classes: Spring's @Required,@Resource,@Inject and @Autowired

Scans the class-path for annotated components that will be auto-registered as Spring beans. For example @Controller,@Repository,@Component and @Service. Make sure to set the correct base-package

<context:annotation-config/>
<context:component-scan base-package="com.github.controllers,com.github.service,com.github.dto,com.github.dao" />
@SpringBootApplication(scanBasePackages = {"com.github.yash777.*"})
//(OR)
@ComponentScan(value = {"com.github.yash777"})
@ComponentScan({"com.github.controllers","com.github.service", "com.github.dto"})

In Spring every request will go through the DispatcherServlet. To avoid Static file request through DispatcherServlet(Front contoller) we configure MVC Static content.

URL: localhost:8080/Project/staticFiles/path/file.js « static resources in the ${webappRoot}/resources directory.

<mvc:annotation-driven />
  <mvc:resources mapping="/staticFiles/path/**" location="/staticFilesFolder/js/" cache-period="60"/>

  <mvc:resources mapping="/css/**" location="/resources/css/"/>
  <mvc:resources mapping="/img/**" location="/resources/img/"/>
  <mvc:resources mapping="/js/**" location="/resources/js/"/>

URL: http://localhost:8090/resources/static/StaticView.html

spring.mvc.static-path-pattern=/resources/static/**

Spring 3.1. introduced the ResourceHandlerRegistry to configure ResourceHttpRequestHandlers for serving static resources from the classpath, the WAR, or the file system. We can configure the ResourceHandlerRegistry programmatically inside our web context configuration class.

Serving a Resource Stored in the WAR « Files located in the WAR’s webapp/resources folder.

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
          .addResourceHandler("/resources/**")
          .addResourceLocations("/resources/"); 
    }
}

Now that we have added the /js/** pattern to the ResourceHandler, lets include the foo.js resource located in the webapp/js/ directory

registry
	.addResourceHandler("/css/**")
	.addResourceLocations("/css/")
	.setCachePeriod(3600)
	.resourceChain(true)
	.addResolver(new GzipResourceResolver())
	.addResolver(new PathResourceResolver());

You can access from URL:http://localhost:8090/css/style.css.


Read multiple properties files.

Customizing bean factories with BeanFactoryPostProcessors « Use PropertyPlaceholderConfigurer to read multiple properties files.

<!-- <context:property-placeholder location="classpath:db.properties,app.properties" /> -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list value-type="org.springframework.core.io.Resource">
            <value>classpath:config.properties</value>
            <value>classpath:mongo.properties</value>
            <value>classpath:mail.properties</value>
        </list>
    </property>
    <property name="ignoreResourceNotFound" value="true"/>
</bean>

Spring Boot reading form programming configuration Spring4-@PropertySources, Spring3-@PropertySource`.

@PropertySources({
  @PropertySource("classpath:config.properties"),
  @PropertySource("classpath:db.properties")
})

Clone this wiki locally