Posts

Showing posts with the label Spring

J2EE Vs Spring

Java EE is an standard, official, specification for a full featured Enterprise Application Framework stack. Includes stuff like Object-Relational Mapping, Security, Web Applications, database connectivity, transactions... On top of Java EE specifications there are JavaEE  implementations/application servers  like: JBoss, Glassfish, WebSphere, Weblogic. Spring on the other hand, is a framework doing lots of the stuff on the Java EE specifications, but in its own form. They don't follow Java EE specifications and APIs for that. But they do include a Web Framework, transaction management, security and several other solutions Java EE offers. ------------------------------------------------------ Java EE: Java EE industry approved standard API based framework IT is predominantly based on annotation and CDI JFC MVC framework for web development JPA implementation to process DB operation JTA API with implementation EJB container and POJO based implementation Ora...

Setter injection vs Constructor Injection with example

use constructor injection for all mandatory collaborators and setter injection for all other properties. Again, constructor injection ensures all mandatory properties have been satisfied, and it is simply not possible to instantiate an object in an invalid state (not having passed its collaborators). In other words, when using constructor injection you do not have to use a dedicated mechanism to ensure required properties are set (other than normal Java mechanisms). One of the other arguments for not using constructor injection is the lack of argument names in constructors and the fact that these do not appear in the XML. I would argue that in  most applications, this does not matter that much. First consider the variant that uses setter injection: <bean id = "authenticator" class = "com.mycompany.service.AuthenticatorImpl" /> <bean id = "accountService" class = "com.mycompany.service.AccountService" > <property nam...

Cyclic dependency in Spring

The  Spring reference manual  explains how circular dependencies are resolved. The beans are instantiated first, then injected into each other. Consider this class: package mypackage ; public class A { public A () { System . out . println ( "Creating instance of A" ); } private B b ; public void setB ( B b ) { System . out . println ( "Setting property b of A instance" ); this . b = b ; } } And a similar class  B : package mypackage ; public class B { public B () { System . out . println ( "Creating instance of B" ); } private A a ; public void setA ( A a ) { System . out . println ( "Setting property a of B instance" ); this . a = a ; } } If you then had this configuration file: < bean id = "a" class = "mypackage.A" > < property name = "b" ref = "...

Call controller from another controller -Java

one controller to call a method on the 2nd controller. There are 2 implementations I have seen so far. 1st Implementation return new Controller (). method ( request , response ); 2nd Implementation @Autowired private Controller controller . return this . controller . method ( request , response ); Which is the right implementation, what are the problems if any with either of them. Ans: What is the advantage of this? Doing so breaks a lot of convention, and you should be considering revising, i.e. breaking up the controller code and maybe moving some of the logic into the business layer. -------------------------------------- Ans:  Sounds like you need to refactor the code. Extract what is common between the two controllers into a separate class, then call that from either controller. ------------------------------------------ Ans: if you do a call in between controller either there is a flaw or you want to make a redirection which is totall...

Spring annotations - Java

Image
SECTION 1 Spring Annotations From its beginning, Spring's most common means of configuration has been XML-based. But as developers grow weary of navigating through a seemingly endless maze of angle-brackets, some have started looking for other ways to wire the beans in their Spring-enabled applications. Spring has responded with several annotation-driven configuration options. In this reference card, you'll find a guide to all of the annotations supported in Spring 2.5. SECTION 2 Core Spring Annotations Context Configuration Annotations These annotations are used by Spring to guide creation and injection of beans. ANNOTATION USE DESCRIPTION @Autowired Constructor, Field, Method Declares a constructor, field, setter method, or configuration method to be autowired by type. Items annotated with @Autowired do not have to be public. @Configurable Type Used with <context:springconfigured> to declare types whose properties should be injected, ...