Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 544 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What exactly is Spring Framework for?

#11
The advantage is [Dependency Injection (DI)][1]. It means outsourcing the task of object creation.Let me explain with an example.


public interface Lunch
{
public void eat();
}

public class Buffet implements Lunch
{
public void eat()
{
// Eat as much as you can
}
}

public class Plated implements Lunch
{
public void eat()
{
// Eat a limited portion
}
}

Now in my code I have a class LunchDecide as follows:

public class LunchDecide {
private Lunch todaysLunch;
public LunchDecide(){
this.todaysLunch = new Buffet(); // choose Buffet -> eat as much as you want
//this.todaysLunch = new Plated(); // choose Plated -> eat a limited portion
}
}
In the above class, depending on our mood, we pick Buffet() or Plated(). However this system is tightly coupled. Every time we need a different type of Object, we need to change the code. In this case, commenting out a line ! Imagine there are 50 different classes used by 50 different people. It would be a hell of a mess. In this case, we need to Decouple the system. Let's rewrite the LunchDecide class.

public class LunchDecide {
private Lunch todaysLunch;
public LunchDecide(Lunch todaysLunch){
this.todaysLunch = todaysLunch
}
}
Notice that instead of creating an object using new keyword we passed the reference to an object of Lunch Type as a parameter to our constructor. Here, object creation is outsourced. This code can be wired either using Xml config file (legacy) or Java Annotations (modern). Either way, the decision on which Type of object would be created would be done there during runtime. An object would be injected by Xml into our code - Our Code is dependent on Xml for that job. Hence, Dependency Injection (DI).
DI not only helps in making our system loosely coupled, it simplifies writing of Unit tests since it allows dependencies to be mocked. Last but not the least, DI streamlines Aspect Oriented Programming (AOP) which leads to further decoupling and increase of modularity.
Also note that above DI is Constructor Injection. DI can be done by Setter Injection as well - same plain old setter method from encapsulation.


[1]:

[To see links please register here]

Reply

#12
Spring was dependency injection in the begining, then add king of wrappers for almost everything (wrapper over JPA implementations etc).

Long story ... most parts of Spring preffer XML solutions (XML scripting engine ... brrrr), so for DI I use Guice

Good library, but with growing depnedenciec, for example **Spring** JDBC (maybe one Java jdbc solution with real names parameters) take from maven 4-5 next.

Using Spring MVC (part of "big spring") for web development ... it is "request based" framework, there is holy war "request vs component" ... up to You
Reply

#13
- Spring is a lightweight and flexible framework compare to J2EE.
- Spring container act as a inversion of control.
- Spring uses AOP i.e. proxies and Singleton, Factory and Template Method Design patterns.
- Tiered architectures: Separation of concerns and Reusable layers and Easy maintenance.


[![enter image description here][1]][1]



[1]:
Reply

#14
In the past I thought about Spring framework from purely technical standpoint.

Given some experience of team work and developing enterprise Webapps - I would say that Spring is for **faster development of applications** (web applications) by **decoupling** its individual elements (beans). Faster development makes it so popular. Spring allows shifting responsibility of building (wiring up) the application onto the Spring framework. The Spring framework's **dependency injection** is responsible for connecting/ wiring up individual beans into a working application.

This way developers can be focused more on development of individual components (beans) as soon as interfaces between beans are defined.

Testing of such application is easy - the primary focus is given to individual beans. They can be easily decoupled and mocked, so unit-testing is fast and efficient.

Spring framework defines multiple specialized beans such as **@Controller** (**@Restcontroller**), **@Repository**, **@Component** to serve web purposes. Spring together with Maven provide a structure that is intuitive to developers.
Team work is easy and fast as there is individual elements are kept apart and can be reused.
Reply

#15
Spring framework is definitely good for web development and to be more specific for restful api services.

It is good for the above because of its [dependency injection][1] and integration with other modules like [spring security][2], [spring aop][3], [mvc framework][4], [microservices][5]
<br>

With in any application, security is most probably a requirement.
<br>
If you aim to build a product that needs long maintenance, then you will need the utilize the Aop concept.
<br>
If your application has to much traffic thus increasing the load, you need to use the microservices concept.
<br>

Spring is giving all these features in one platform. Support with [many modules][6].
<br>
Most importantly, spring is [open source][7] and an extensible framework,have a hook everywhere to integrate custom code in life cycle.
<br>

[Spring Data][8] is one project which provides integration with your project.

<br>
So spring can fit into almost every requirement.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

[5]:

[To see links please register here]

[6]:

[To see links please register here]

[7]:

[To see links please register here]

[8]:

[To see links please register here]

Reply

#16
Old days, **Spring** was a dependency injection frame work only like (**Guice**, **PicoContainer**,...), but nowadays it is a total solution for building your **Enterprise Application**.

The spring dependency injection, which is, of course, the heart of spring is still there (and you can review other good answers here), but there are more from spring...

Spring now has lots of projects, each with some sub-projects (

[To see links please register here]

). When someone speaks about spring, you must find out what **spring project** he is talking about, is it only spring core, which is known as **spring framework**, or it is another spring projects.

Some spring projects which is worth too mention are:

- **Spring Security** -

[To see links please register here]

- **Spring Webservices** -

[To see links please register here]

- **Spring Integration** -

[To see links please register here]


If you need some more specify feature for your application, you may find it there too:


- **Spring Batch** batch framework designed to enable the development of
batch application
- **Spring HATEOAS** easy creation of REST API based on HATEOAS principal
- **Spring Mobile** and **Spring Andriod** for mobile application development
- **Spring Shell** builds a full-featured shell ( aka command line) application
- **Spring Cloud** and **Spring Cloud Data Flow** for cloud applications

There are also some tiny projects there for example **spring-social-facebook** (

[To see links please register here]

)

You can use spring for web development as it has the `Spring MVC` module which is part of **Spring Framework** project. Or you can use spring with another web framework, like **struts2**.
Reply

#17
The accepted answer doesn't involve the annotations usage since Spring introduced support for various annotations for configuration.

## Spring annotations approach (Dependency Injection) ##

There the another way to wire the classes up alongside using a XML file: the annotations. Let's use the example from the accepted answer and register the bean directly on the class using one of the annotations `@Component`, `@Service`, `@Repository` or `@Configuration`:

@Component
public class UserListerDB implements UserLister {
public List<User> getUsers() {
// DB access code here
}
}

> This way when the view is created it magically will have a UserLister ready to work.

The above statement is valid with a little bonus of no need of any XML file usage and wiring with another annotation `@Autowired` that finds a relevant implementation and inject it in.

@Autowired
private UserLister userLister;

Use the `@Bean` annotation on a method used to get the bean implementation to inject.
Reply

#18
Basically Spring is a framework for [Tag:Dependency-Injection] which is a pattern that allows building very decoupled systems.

## The problem ##

For example, suppose you need to list the users of the system and thus declare an interface called `UserLister`:

```java
public interface UserLister {
List<User> getUsers();
}
```

And maybe an implementation accessing a database to get all the users:

```java
public class UserListerDB implements UserLister {
public List<User> getUsers() {
// DB access code here
}
}
```

In your view you'll need to access an instance (just an example, remember):

```java
public class SomeView {
private UserLister userLister;

public void render() {
List<User> users = userLister.getUsers();
view.render(users);
}
}
```

Note that the code above hasn't initialized the variable `userLister`. What should we do? If I explicitly instantiate the object like this:

```java
UserLister userLister = new UserListerDB();
```

...I'd couple the view with my implementation of the class that access the DB. What if I want to switch from the DB implementation to another that gets the user list from a comma-separated file (remember, it's an example)? In that case, I would go to my code again and change the above line to:

```java
UserLister userLister = new UserListerCommaSeparatedFile();
```

This has no problem with a small program like this but... What happens in a program that has hundreds of views and a similar number of business classes? The maintenance becomes a nightmare!

## Spring (Dependency Injection) approach ##

What Spring does is to *wire* the classes up by using an XML file or annotations, this way all the objects are instantiated and initialized by Spring and *injected* in the right places (Servlets, Web Frameworks, Business classes, DAOs, etc, etc, etc...).

Going back to the example in Spring we just need to have a setter for the `userLister` field and have either an XML file like this:

```xml
<bean id="userLister" class="UserListerDB" />

<bean class="SomeView">
<property name="userLister" ref="userLister" />
</bean>
```

or more simply annotate the filed in our view class with `@Inject`:

```java
@Inject
private UserLister userLister;
```

This way when the view is created it *magically* will have a `UserLister` ready to work.

```java
List<User> users = userLister.getUsers(); // This will actually work
// without adding any line of code
```

It is great! Isn't it?

- *What if you want to use another implementation of your `UserLister` interface?* Just change the XML.
- *What if don't have a `UserLister` implementation ready?* Program a temporal mock implementation of `UserLister` and ease the development of the view.
- *What if I don't want to use Spring anymore?* Just don't use it! Your application isn't coupled to it. [Inversion of Control][1] states: "The application controls the framework, not the framework controls the application".

There are some other options for Dependency Injection around there, what in my opinion has made Spring so famous besides its simplicity, elegance and stability is that the guys of SpringSource have programmed many many POJOs that help to integrate Spring with many other common frameworks without being intrusive in your application. Also, Spring has several good subprojects like Spring MVC, Spring WebFlow, Spring Security and again a loooong list of etceteras.

Anyway, I encourage you to read [Martin Fowler's article][2] about Dependency Injection and Inversion of Control because he does it better than me. <s>After understanding the basics take a look at [Spring Documentation][3]</s>, in my opinion, it <s>is</s> **used to be** the best Spring book ever.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through