What is the problem using Spring

I am trying to understand Spring. I read about it and did some lessons. I kind of get the IoC paradigm and the fact that DI is its implementation in Spring.

My question is: what do you lose without using Spring? I understand that this is a big question and somewhat subjective. But if you could write a few point dots or give an example of a problem that might arise if Spring was not used, I think that would help me and many other people.

+3
source share
7 answers

Spring is much more than just another IoC tool (think of DAO related stuff, convenient MVC support, testing tools ...). In my opinion, it gradually turned into a kind of "flavor" of Java. But this is not the main thing :) Speaking about IoC / DI, what you lose does not use it, this is the easiest way to get free communication in your application, which is connected with the possibility of reuse. Obviously, most people tend to think about the possibility of reusing a component in another project, which, in my experience, does not happen so often. The greatest benefit of reuse comes when you need to disable your code. Interface programming and the use of DI / IoC make unittests so easy that even those who are reluctant to unit test will love it.

UT - , .

+3

. , , Spring. , Spring, , ( ), . () , Spring .

, , Spring. ...

+2
+2

, . , , (), ( , " " ). , DI, Spring Guice, . . , , , Struts, Hibernate ..

+2

glib , . Spring (), , Spring :

:

  <sectionGroup name="spring">
    <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
    <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
  </sectionGroup>

  <spring>
    <context>
      <resource uri="config://spring/objects"/>
    </context>
    <objects xmlns="http://www.springframework.net">
      <object name="mediaLibrary" type="AlbumLibraryWPF.AlbumLibrary, AlbumLibrary"/>
    </objects>
  </spring>

:

  IApplicationContext ctx = ContextRegistry.GetContext();
  library = (Library)ctx.GetObject("mediaLibrary");

: DI ?

+1

Spring , DI. , :

  • JDBC, Hibernate, JMS- ( )
  • (Spring )
  • Spring MVC
  • Spring -

- . - , , , , .

Spring - , , . DI , . Inversion of Control. , (, Guice), , Spring .

+1

IoC - , , Java, , , / , - .

Spring IoC/DI . , , , /, , ( ).

DAO, :

interface IPersonDao {
  List<Person> getPersonsByTeam(String teamName);
}

src, Spring "". , :

class MyService {
  @Autowired
  IPersonDao svc;
}

:

class TestPersonDao {
  @Autowired
  IPersonDao svc;

   @Test
  void testGetPersons(){
    List<Person> persons = svc.getPersonsByTeam("billing");
    Assert.assertDataSet(persons, "billing.xml");
  }
}

, Dao , Dao. , , :

class JpaPersonDao implements IPersonDao {

@PersistenceContext
EntityManager em;

  List<Person> getPersonsByTeam(String tm) {
    em.createQuery("...");
  }
}

, beans. , DI, . , Spring , , , mvc, , hibernate, jpa / db, , , Spring IoC.

0

Source: https://habr.com/ru/post/1706873/


All Articles