Dependency Injection (DI) in Java EE

In a J2EE/JavaEE server, container has always been the backbone for creating and managing the lifecycle of resident components. It's not a surprise to see some forms of DI is already present at the beginning of J2EE. For example, javax.ejb.SessionBean, the legacy interface implemented by old-style EJB bean class, contains a setSessionContext(SessionContext ctx) method, which is called by ejb container to inject a SessionContext to the bean class. Similarly, javax.ejb.EntityBean has setEntityContext(EntityContext ctx), and javax.ejb.MessageDrivenBean has setMessageDrivenContext(MessageDrivenContext ctx) callbacks.

Another less obvious example is Servlet init methods, which are invoked by servlet container to initialize ServletContext and ServletConfig for servlet class:
//Declared in javax.servlet.Servlet interface and implemented in GenericServlet:
public void init(ServletConfig config)

//Convenience method implemented in GenericServlet
public void init();
This early forms of DI is typically defined as part of the lifecycle callbacks and strongly typed to a specific interface or abstract class.

DI capability was substantially expanded in Java EE 5 with the introduction of annotation- and xml-based DI. Both field- and setter-injections are supported, and xml deployment descriptors can be used to override or augment annotation meta data. Most common ones are:
@EJB private Calc calc;

@EJB(name="ejb/calc", beanName="DefaultCalc", beanInterface=com.my.ejb.Calc.class)

@Resource SessionContext ctx;

@Resource private String widgetName;

@Resource(mappedName="custom/widgetName")
private String widgetName;

@PersistenceContext private EntityManager em;

@PersistenceContext(unitName="advanced-pu", type=TRANSACTION)
private EntityManager em;
As you can see, the annotation type is specific to each resource type. The advantage of this approach is, you can easily customize how a specific type of dependency is constructed and initialized. This design makes sense as some resources need more initialization params than others. But the downside is lack of internal consistency across the board. For each new type of resources, we need to decide whether to reuse the existing annotations, add new attributes to existing annotations, or create a new one. This task is tackled in Java EE 6, with JSR 330 (AtInject spec) and JSR 299 (CDI spec), both are required component specs in Java EE 6 Platform.

Now in Java EE 6, all the above is doable with a single @java.inject.Inject. For injections with no attributes (or default attributes), we can easily replace @EJB/@Resource/@PersistenceContext with @Inject. Otherwise, a @javax.inject.Qualifier is needed to provider more information. Basically we are promoting annotation attributes to auxiliary annotations, replacing string-based qualifiers with typed qualifiers, and chaining them together to resolve the DI.

@EJB and @Resource are also enhanced in Java EE 6 to take an additional portable attribute lookup, mainly as a replacement for the nonportable mappedName attribute:
@EJB(lookup="java:global/myApp/myEJBModule/CalcBean")
private Calc calc;

@Resource(lookup="java:module/env/sleepSeconds")
private long sleepSeconds;

JUnit Notes, Do's and Don'ts

A skeleton JUnit 4 test case:
import java.util.*;
import org.junit.*;
import static org.junit.Assert.*;

public class MyTest {
/** 4 optional lifecycle callback methods */
@Before public void before() {} //must be public void no-arg

@After public void after() {} //must be public void no-arg

@BeforeClass public static void beforeClass() {} //must be public static void no-arg

@AfterClass public static void afterClass() {} //must be public static void no-arg

@Test public void testAdd() {
int a = 1, b =2, expected = 3;
int actual = a + b;
assertEquals(expected, actual);
}
}
Do not name your test main class Test.java, which has the same short name as @Test annotation. Unless you explicitly use @org.junit.Test, @Test will resolve to your test class, instead of org.junit.Test annotation type.

Avoid using Java assert; use static assertXXX methods (statically imported) in org.junit.Assert class. Java assert is turned off by default, and can be enabled only at command line with "java -ea", or "java -enableassertions" options. So some tests using Java assert will always pass when running with the default configuration, regardless of the actual test conditions. For example, the following method passes unless -ea option is passed to java:
@Test public void testNumber() {
assert(1 < 0);
}

java -cp $tmp/junit-4.8.1.jar:. org.junit.runner.JUnitCore MyTest
JUnit version 4.8.1
Time: 0.009
OK (4 tests)
A secondary reason to favor org.junit.Assert.assertXXX over Java assert is, the former is a richer API with more descriptive output, whereas Java assert, in its common form, only fails with java.lang.AssertionError and stack trace with no description. Java assert can also provide details with a second param: assert false : reason

maven surefire plugin automatically enables Java assertions when running JUnit tests on JDK 1.4 and above. So when running with surefire, there is no problem of false positive test results. But still I would like my JUnit tests to work the same way across all testing environment.

Avoid using assertTrue(list1.equals(list2)); use assertEquals(list1, list2) instead. assertTrue will only print out the useless message like "expecting true, but actual false", whereas assertEquals gives more helpful message like "expected:[null, 1, 2], but was:[1, 2]"

Do not add assertTrue(true) at the end of a @Test to signal the success result. It's just not needed. The test simply passes when nothing goes wrong.

@Test method can declare throws clause, e.g., throws NamingException. When an exception is thrown during test run, the test is failed. So use exception as a means to communicate test failures. Do not try-catch an exception, just to print it and manually fail() it. For example, do this:
@Test public void testNumber() {
int i = 10 / 0;
}
Do NOT do this:
@Test public void testNumber() {
try {
int i = 10 / 0;
} catch (ArithmeticException e) {
e.printStackTrace();
fail(); // or even worse, assert(false);
}
}
When running the try-catch version, ArithmeticException stack trace is printed, followed by another stack trace of java.lang.AssertionError from calling fail(), whereas in the shorter version, only the ArithmeticException is logged and hence much easier to trace.

How to run a single test? org.junit.runner.JUnitCore, the default command-line runner, takes a list of test classes as input, but doesn't support running a single test method. Other tools and IDE may support it by providing their own runner. With maven surefire 2.7.3+ and JUnit 4 tests, it is supported with a -D sysprop (link):
mvn test -Dtest=com.my.test.TestMain#test1
mvn test -Dtest=com.my.test.TestMain#*egative*
mvn org.apache.maven.plugins:maven-surefire-plugin:2.8:test -Dtest=com.my.test.TestMain#test1
I prefer to put the test name at the very end of the line so it's easier to go through the history and edit command.

To check the version of your JUnit:
$ java -cp $tmp/junit.jar junit.runner.Version
4.8.1
To skip or exclude a test, use @Ignore (org.junit.Ignore).

DIY Canvas Wall Art |Easy Diy Canvas Wall Painting

Murals - if you are a child's room decor, nursery or playroom, you can decorate the walls with murals. This may require a lot of work and artistic ability. Consider your child's tastes and favorite patterns. You can paint murals in the jungle scene, a fantasy land, African safari, or fish and marine animals in the water landscape.

Tapestry - You can come from Africa, the Middle East, Asia and other parts of the world where the exotic tapestries to decorate your living room or den. This will be more meaningful, if you buy the tapestry of your travel at home and abroad.

DIY Canvas Wall Art

DIY Canvas Wall Art

DIY Canvas Art Materials:
    * Either: blank canvas, primed canvas, unprimed canvas, stretched canvas, or a painters canvas
    * Cool wallpaper
    * Masking or painter’s tape
    * Staple gun or glue
    * Paint (various colors)
    * Paint brushes, small roller
    * Pattern (optional)
    * Ruler
    * Pencil
    * Large spoon
DIY Canvas Wall Art

DIY Canvas Wall Art


Use an image editor to enhance your photos, enhance your photos printed on canvas using professional software such as Adobe Photoshop and Corel Draw. You can also choose to provide a variety of free online image editing. No matter which program you choose, have a different effect, try color combinations and layout of the experiment. This will open your door, look at your digital images of numerous development possibilities.

Garden Wall Decors |Garden Wall Decoration Idea |Garden Wall Murals


Garden Wall Decoration Idea-Murals
Wall stone is sometimes a very good idea for your family to create national and modern home design impression. We give you the best gallery pictures beautiful large garden and outdoor decoration ideas new wallstone. This product is most suitable for wall decoration contemporary home design and fashion samples. neowall is the best natural decorative stone wall for your garden and outdoor decorating ideas reshape the luxurious interior design.

Garden Wall Decoration Idea-Murals

Garden Wall Decoration Idea-Murals
This delightful patio garden design and exotic furniture, a beautiful arbor, Tuscan wall, rooftoop patio, Tuscan wall, so to find one or more of the design of your backyard.

Garden Wall Decoration Idea-Murals
Fences and walls is often neglected by the owners, but they offer the possibility to create a garden of the main design features. There is a tendency to try to fence and screen planting, but they are more creative ways to treat the vertical: in the fresh air art gallery.

Garden Wall Decoration Idea-Murals

Garden Wall Decoration Idea-Murals

Garden wall art is a cheap and effective way to increase the front porch area of ​​the touch-type and interest. In this example, the outdoor wall art mirrors spend a day lilys in the garden bed, and connected to the garden of the house.

Followers

Pageviews Last 7 Days