Archive

Posts Tagged ‘Spring’

Spring vs Dagger

Spring contains configurable factory classes called contexts. These contexts can read description (specified in XML, for example) about
creating objects and satisfying dependecies. Finally, your app can obtain instances of objects from context and start using
them.

Dagger requires you to describe if class requires depdendency to be injected via @Inject annotation (right, that’s intruzive).
If dependency is concrete class, then it may be created in default way, and no more work required for it. Otherwise, if you
have to inject an instance of interface, or you need to create injected class in some specific way, then you use special
factory class and annotate it with @Module annotation and annotate each factory method with @Provides annotation. Finally, you
can generate a factory class for any classes which need injected dependency. This class will have factory methods identified
by return types, and they need all necessary @Modules to be supplied.

Compare:

Spring Dagger
Classes which don’t have dependencies, but on which other classes depend Describe them in XML, init them via values If they require some specific way to create them, then you need a @Provides factory method in some @Module class. Otherwize they will be created automatically
Classes which have dependencies, and on which other classes depend Describe them in XML, inject them with references to other beans If they require some specific way to create them, then you need a @Provides factory method in some @Module class. Otherwize they will be created automatically. In both cases their dependencies will be satisfied
Classes which have dependencies, but which are not needed to be injected See previous These classes will be needed externally, so you need to generate factory methods for them
A way to make it work Load description, call refresh(), obtain objects which you need. All beans will be created and injected in each other in a way which you describe Invoke generated factory methods for objects which you need and supply specific @Module factories to customize the result
Advertisements

Essentials of Java web stack

March 15, 2017 Leave a comment

Jetty 8

1. start.jar is a launcher. Use –help command-line key to list available command-line options.
2. Launcher can start server in the same process (by default), or, if –exec command-line arg is present, as a separate process (if, for example, you need different JVM settings)
3. Launcher is controlled via command-line keys, and by start.ini file from current dir. There is a special command-line key –ini allowing to either specify a different ini file, or to specify a configuration content which is expected from such file
4. Server is customizable. You can specify which modules will be loaded. These are called OPTIONS, and are read from OPTIONS section of start.ini (or specified in –ini command line arg). Example:

java -jar start.jar --ini OPTIONS=Server,websocket etc\jetty.xml

5. To get a list of available modules use –list-options command-line key. It will show which jar files are used by each module.
6. Information produced by –list-options is calculated based on start.config script file which is packed inside start.jar. You can take a look by using –list-config command line key to understand module dependencies. You can override this config file by using –config command line key to completelly change server’s behaviour, but this is rarely needed.
7. There are some modules which just aggregate several other modules. Very good aggregate module is Server, which includes HTTP server, servlet container and deployer
8. Modules may be explicitly configured by XML config scripts which are usually stored in etc/ dir. Application of such configs specified in start.ini
9. To configure Jetty logging, customize existing configuration script etc/jetty-logging.xml, and don’t forget to add it to start.ini
10. Web applications are deployed to webapps dir
11. If you’ve included “resources” module, then directory resources/ is added to classpath (by default start.config script). You can put config files for your application there
12. One of usages of resouces/ dir is for applications which use log4j. If you put file log4j.properties in resources/ dir, then log4j will discover that file and will configure itself based on that file

Jetty 9

1. start.jar is a launcher. Use –help command-line key to list available command-line options
2. Launcher can start server in the same process (by default), or, if –exec command-line arg is present, as a separate process (if, for example, you need different JVM settings)
2. Launcher is controlled via command-line keys, and by start.ini file from current dir. File start.ini is just a set of same keys as for command-line
3. Server is customizable. You can specify which modules will be loaded via –module key. Example:

java -jar start.jar --module=http,deploy

4. To get a list of available modules use –list-modules command-line key. It will show which jar files are used by each module, and will show module dependencies.
5. Information provided by –list-modules is calculated based on module descriptors stored in modules/ directory as .mod files.
6. Each module has a corresponding config in etc/ directory. To check which config corresponds to which module you can use output of –list-modules (property XML) or by looking into .mod file. Configs are implicitly executed if corresponding module is included.
7. Minimal set of modules for working servlet container with auto-deployment support is server,http,deploy.
8. Web applications are deployed to webapps dir (will work in deploy module is enabled)
9. Recommended way to start Jetty 9 is to create a separate base directory for each running instance, which will contain configs, webapps and logs for this instance. This is called jetty.base
10. When running Jetty 9 this instance base dir should be current dir, and path to start.jar should be fully specified:

java -jar path/to/jetty/installation/start.jar

11. You can manually place start.ini into instance base dir, and create sub-dirs log, webapps. But there is a better way: from empty instance base dir run start.jar via full path and use command-line key –add-to-start. Example:

java -jar path/to/jetty/installation/start.jar --add-to-start=http,server,deploy

This will create config files from templates, and all necessary dirs.
12. If you’ve included “resources” module, then directory resources/ is added to classpath. You can put config files for your application there

Difference between Jetty 9 and Jetty 8

1. In Jetty 9, a start.ini file is just a set of command-line keys, so you can live without it. In Jetty 8 value of OPTIONS could be specified only in ini file, so you could live without it if you have specified –ini command-line key had to override this
2. Instead of defining OPTIONS in single config script, each module has it’s own descriptor.

Servlets basics

1. Web applications are packed into .war files (similar to .jar files). Applications contain static files and dynamic logic.
2. Jetty deployer maps content of web app to URL path corresponding to name of .war file. For example, application example.war to URL http://host:80/example/ . It’s called servet URL context path.
2. All dynamic logic should be placed in WEB-INF/ dir inside a .war file. In this dir there should be a classes/ sub-dir for .class files, lib/ sub-dir for .jar files, and web.xml descriptor of web app
3. Descriptor file web.xml describes servlets (they have name and main class) and mappings from url patterns to servlets.
4. Mappings from URL patterns to servlets is done relative from web application URL context path. For example, if in web.xml servlet mainServlet is mapped to URL pattern /main for web application superapp.war,
then such servlet will handle HTTP requests for URL http://host:80/superapp/main
5. Jetty comes with several built-in servlets: DefaultServlet which serves static files, and JSP servlet which handles .jsp files. You can use them in your application.
6. By default, these servlets are already used in your application. This happens because for module “deploy” it’s config script “etc/jetty-deploy.xml” defines property “jetty.deploy.defaultsDescriptorPath” with value “etc/webdefault.xml”.
This webdefault.xml is pre-pended to your web.xml. To switch it off, override property in your config files, for example in start.ini
7. in etc/webdefault.xml servlet DefaultServlet is mapped to “/”
7. Use Maven with war packaging, maven-war-plugin at build stage, maven profiles to specify which content should be included in .war file

Spring MVC

1. Spring comes with servlet called org.springframework.web.servlet.DispatcherServlet. You can have different servlets of this class under several names
2. This servlet will create Spring application content based on XML file. It will search for Spring context descriptor XML file, depending on servlet name, with pattern -context.xml
3. Like with any Spring XML context, you can either specify beans, including controller beans, directly via tags, or via  and
3. To enable MVC annotations like @Controller etc use
4. If you have mapped DispatcherServlet to “/”, but still want static files to be served, use in your context XML following tag: . It will make static requests to be forwarded
from DispatcherServlet to container’s DefaultServlet (if it is included in your web.xml either explicitly or implicitly by Jetty, see above)
5. If your Spring context supports several profiles, then you can specify which Spring profiles are active using web.xml to set context param “spring.profiles.active”
6. Log examples to see how your RequestHandlerMappings work:

INFO RequestMappingHandlerMapping:534 - Mapped "{[/rest/query],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.lang.String> com.ProxyController.search
INFO RequestMappingHandlerMapping:534 - Mapped "{[/proxy/**]}" onto public org.springframework.http.ResponseEntity<java.lang.String> com.GenericPurposeProxyController.processRequest(jav

For default servlet handler:

INFO SimpleUrlHandlerMapping:341 - Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0'

 

Categories: Java, Web Tags: , ,