Spring was introduced in 2005 and has evolved from there. Nowadays, you need Spring and Hibernate for most of the jobs. Spring began with a dislike of Enterprise Java Beans (EJB).
What Is Spring?
Spring is described as a lightweight framework for building Java applications, but that statement brings up two interesting points. First, you can use Spring to build any application in Java (e.g., stand-alone, Web, JEE applications, etc.), unlike many other frameworks such as Apache Struts, which is limited to web applications. Second, the lightweight part of the description doesn’t really refer to the number of classes or the size of the distribution, but rather, it defines the principle of the Spring philosophy as a whole—that is, minimal impact. Spring is lightweight in the sense that you have to make few, if any, changes to your application code to gain the benefits of the Spring core.
Inversion of Control
The core of the Spring Framework is based on the principle of Inversion of Control (IoC).. IoC is a technique that externalizes the creation and management of component dependencies. Inversion of Control (IOC) is a design pattern.
Dependency injection
Spring’s DI implementation is based around two core Java concepts: JavaBeans and interfaces. When you use Spring as the DI provider, you gain the flexibility of defining dependency configuration within your applications in different ways (e.g., externally in XML files, Spring Java configuration classes, or Java annotations within your code). JavaBeans (also known as POJOs, for Plain Old Java Objects) provide a standard mechanism for creating Java resources that are configurable in a number of ways.
Interfaces and DI are technologies that are mutually beneficial. By using DI, you reduce the amount of code you need to utilize an interface-based design in your application to almost zero. Likewise, by using interfaces, you can get the most out of DI because your beans can utilize any interface implementation to satisfy their dependency.
Spring Framework
JAR Files are under:
spring-framework-3.1.3.RELEASEdist
Can either copy all of the JAR files to your library or you can pick and choose the JAR you need. JARs are divided into four Spring Modules:
1) Data Access Integration
JDBC, ORM (Hibernate, TopLink), OXM (Object XML Mapping), JMS, Transactions
2) Web (MVC / Remoting)
Web, Servlet, Portlet, Struts
3) Core Container
Beans, Core, Context, Expression Language (New in Spring 3)
4) Test
There is also AOP, Aspects, and Instrumentation
In Data Access with traditional Java, you have to rewrite lots of the same code over and over again.
Don’t repeat yourself.
In Spring, there is an easier way to do this by moving those repetitious parts up into the superclass
The framework delivers the most commonly used JARs.
For additional functionality, you can add additional JARs like Spring Security, Spring Mobile, etc.
public class Calculate {
private Operation operation;
private ResultWriter writer;
public Calculate (Operation operation, ResultWriter writer) {
this.operation = operation;
this.writer = writer;
}
* Notice the keyword new is not anywhere. CalculateApp does all the work.
Everything done in Spring will depend on an XML config file.
public class CalculateApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(“context.xml”);
Calculate calc = (Calculate)context.getBean(“calculatorBean”);
Calc.execute(args);
}
}
POJOs are called Beans in Spring.
To initialize a Spring container, you need to provide it with the XML file. Here, we ask Spring to pull up the Calculate bean.
CalculateBean has two dependencies – one on operation and one on writer
Dependency lookup – using an XML file to match the bean name with the location of the class.
In the Context.xml file, we match up the bean names with their classes and properties.
<bean id=”add” class=”com.springclass.OpAdd” />
<bean id=”multiply” class=”com.springclass.OpMultiply” />
<bean id=”screen” class=”com.springclass.ScreenWriter” />
<bean id=”file” class=”com.springclass.DataFileWriter” />
<bean id=”calculatorBean” class=”com.springclass.Calculate”>
<property name=”operation” ref=”add” />
<property name=”writer” ref=”” />
</bean>
If you want to inject a primitive, use value. If you want to inject a class, use ref.
<bean id=”helloWorld” class=”com.springclass.HelloWorld” />
<property name=”message” value”Hello, World!” />
</bean>
These inject values into constructors
Property means I want to set a property in my bean. This calls the set method and passes in the object.
Here, we replace the message with a message service.
<bean id=”helloWorld” class=”com.springclass.HelloWorld” />
<property name=”messageService ” ref=”messageService” />
</bean>
<bean id=”messageService” class=”com.springclass.MessageService” />
<property name=”message ”value=”Hello, World. It’s me.” />
</bean>
You simply need to wire up your business logic using an ApplicationContext and use a WebApplicationContext to integrate your web layer.
What this does:
• Creates the object
• Notices the two dependencies
• Creates beans for those two dependencies
• Injects values into those
Spring has one dependency : commons-logging.jar
Build Path -> Configure Build Path
- Commons-logging.jar
- Log4j.1.2.15.jar
- Spring.jar
- JRE System Library
Spring 3 does not include the all-encompassing spring.jar. Now, you get 20 JAR files, so you have to figure out which ones you need.