Question

I am new to Spring framework.While reading dependency injection i found out two ways to inject beans anotationbased and xml based.

In xmlBased it is quite simple that you define one bean inside your application context xml file.

eg.

<bean id="wild" class="com.javapapers.spring.ioc.Wolf" />
<bean id="zoo" class="com.javapapers.spring.ioc.Zoo">
     <property name="wild" ref="wild" />
</bean> 

but in Annotation based configuration we just have to write

<context:component-scan base-package="com.javapapers.spring.ioc" />

I want to know how it will load "wild" and "zoo" .

Does it means it will load all beans or only specific which is written under @Service annotation..???

I also want to know how it is loaded ..??? is all beans gets initialized when application is loaded..??

Thanks ...!!

Was it helpful?

Solution 2

Beans get initialized through an ApplicationContext, which is also a BeanFactory. With an XML configuration, you would need an implementation of that interface, ClassPathXmlApplicationContext. Your application needs to create such a class, register your XML file(s), and refresh the context. When that is done, Spring will start creating your beans by reading the configuration.

When it hits the <component-scan> element, Spring will scan your declared packages for any classes annotated with @Component or its specializations. From the docs:

In Spring 2.0 and later, the @Repository annotation is a marker for any class that fulfills the role or stereotype (also known as Data Access Object or DAO) of a repository. Among the uses of this marker is the automatic translation of exceptions.

Spring 2.5 introduces further stereotype annotations: @Component, @Service, and @Controller. @Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Controller are specializations of @Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.

Therefore, you can annotate your component classes with @Component, but by annotating them with @Repository, @Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts.

Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated above, @Repository is already supported as a marker for automatic exception translation in your persistence layer.

When it finds those classes, it will create an instance of each of them.

As for how it does this, it's a little more complicated. The overall strategy is with reflection. However, because of your configuration, Spring will sometimes generate (java or cglib) proxies instead of clear instances so that it can add behavior.

All the steps are described in detail in the official documentation.

OTHER TIPS

When you use <context:component-scan base-package="com.javapapers.spring.ioc" />, spring will instanciate all classes that are in the "com.javapapers.spring.ioc" package and have one of this annotation :

  • @Service
  • @Controller
  • @Repository
  • ...

And yes, all beans gets initialized when you launch your application.

You can have more info in this page : here

Will be loaded all beans annotated with

@Controller
@Component
@Service
@Repository

which are inside a packages com.javapapers.spring.ioc, and its subpackages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top