Monday, February 1, 2021

Spring: contextConfigLocation context-param and init-param in web.xml

https://stackoverflow.com/questions/15818047/spring-namespace-vs-contextconfiglocation-init-parameters-in-web-xml

https://stackoverflow.com/questions/27539610/order-of-loading-contextconfiglocation-in-web-xml-of-spring-servlet-project

https://howtodoinjava.com/spring-mvc/contextloaderlistener-vs-dispatcherservlet/


DispatcherServlet – Child application contexts

DispatcherServlet is essentially a Servlet (it extends HttpServlet) whose primary purpose is to handle incoming web requests matching the configured URL pattern. It take an incoming URI and find the right combination of controller and view. So it is the front controller.

When you define a DispatcherServlet in spring configuration, you provide an XML file with entries of controller classes, views mappings etc. using contextConfigLocation attribute.

web.xml
<servlet>
    <servlet-name>employee-services</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:employee-services-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>



ContextLoaderListener – Root application context

ContextLoaderListener creates the root application context and will be shared with child contexts created by all DispatcherServlet contexts. You can have only one entry of this in web.xml.

web.xml
<listener>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>
  
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>

The context of ContextLoaderListener contains beans that globally visible, like services, repositories, infrastructure beans, etc.


@Autowired Conflicts Management

https://howtodoinjava.com/spring-core/spring-beans-autowiring-concepts/

https://stackoverflow.com/questions/40830548/spring-autowired-and-qualifier/48134386

https://www.baeldung.com/spring-qualifier-annotation

https://mkyong.com/spring/spring-autowiring-qualifier-example/


@Qualifier Example

To fix above problem, you need @Quanlifier to tell Spring about which bean should autowired.


package com.mkyong.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Customer {

    @Autowired
    @Qualifier("personA")
    private Person person;
    //...
}

In this case, bean “personA” is autowired.

spring PropertyPlaceholderConfigurer and context:property-placeholder

<context:property-placeholder ... />

 is the XML equivalent to the PropertyPlaceholderConfigurer


https://dzone.com/articles/spring-core-diving-into-the-propertyplaceholdercon

https://stackoverflow.com/questions/7542506/spring-propertyplaceholderconfigurer-and-contextproperty-placeholder


@Configuration
public class AppConfig {

    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new 
PropertySourcesPlaceholderConfigurer();
        propertySourcesPlaceholderConfigurer.setLocations(new 
ClassPathResource("application-db.properties"));
        //propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
        //propertySourcesPlaceholderConfigurer.setIgnoreResourceNotFound(true);
        return propertySourcesPlaceholderConfigurer;

    }
}
--------------------------------------------
@Configuration
@PropertySource("/foo/bar/services.properties")
public class ServiceConfiguration { 

    @Autowired Environment environment; 

    @Bean public javax.sql.DataSource dataSource( ){ 
        String user = this.environment.getProperty("ds.user");
        ...
    } 
}

How to refer to another bean as a property in annotation-based configuration file

https://stackoverflow.com/questions/42386957/how-to-refer-to-another-bean-as-a-property-in-annotation-based-configuration-fil


 @Configuration
    class GlobalConfiguration {
        @Bean
        @Autowired
        public Example createExample(AnotherBean anotherBean){
            final Example example = new Example();
            example.setSomeProp(anotherBean);
            return example;
        }

        @Bean
        public AnotherBean createAnotherBean(){
            return new AnotherBean();
        }
    }

Azure - Pipeline - Add Approver for Stage

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/approvals?view=azure-devops&tabs=check-pass