Tuesday, November 17, 2015

Autowired beans in a web application

We started a new project in the previous Spring. And we chose Spring framework to manage many functionality, we were so lucky to make this decision.


The Spring annotations are very calm and comfortable. We created a configuration class:

@EnableScheduling
@Configuration
@ComponentScan(basePackages = {
"org.whatever.sthg",
"org.whatever.sthg.util"
})
public class BeansConfig {
....
}

With the @Configuration annotation you can mark your class, it contains the configuration.
With the @EnableScheduling you can define late chron like jobs.
With the @ComponentScan you can list your packages where Spring framework could find the @Autowired, desired beans.

For example a @Controller and an autowired @Component class:

@Controller
public class CommonController {

@Autowired

private DomainFacadeInterface domainFacade;
...
}

@Component
public class DomainFacadeImpl implements DomainFacadeInterface {...}


The entry point should be an xml file, or static main() function to bootstrap Spring framework.
We decided to use the web.xml with the following extra lines:

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            org.whatever.BeansConfig
        </param-value>
    </context-param>

You can forget to maintain massive XML files.

Monday, November 16, 2015

How to log Axis messages

If you use webservice calls in your system, the axis has very common 3rd party libraries to send and receive the messages: SOAP, XML, JSON whatever.

Sometimes desirable to see in the logs what kind of messages travel in your system.


Just create the following XML file, named: client-config.wsdd
Put the file to any resource folder. After a new deployment you will see the messages on the console.

I tried to log the messages to a log file, it was not working for me. But i did not want to spend more time with this analysis.

<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig" xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

   <handler name="log" type="java:org.apache.axis.handlers.LogHandler">
<parameter name="LogHandler.writeToConsole" value="true" />
   </handler>

   <globalConfiguration>
      <parameter name="disablePrettyXML" value="false" />
      <requestFlow>
         <handler type="log" />
      </requestFlow>
      <responseFlow>
         <handler type="log" />
      </responseFlow>
   </globalConfiguration>

   <transport name="http"  pivot="java:org.apache.axis.transport.http.HTTPSender" />
   <transport name="local" pivot="java:org.apache.axis.transport.local.LocalSender" />
   <transport name="java"  pivot="java:org.apache.axis.transport.java.JavaSender" />
</deployment>