..


Sponsored Links

Core Container - @ Autowired and Resources

In previous sections we saw how to configure the IoC container using metadata in XML format.
The default configuration through the Java Annotation is not enabled, to enable you to enter nell'applicationContext. <context:annotation-config/> Xml tag.
Let's see how to use the @ Autowired to configure the dependencies.

@ Autowired manufacturer of






 {public class AutowiredBean1





  



 Private ServiceAW1 service1;





  



 Private ServiceAW2 Service2;





  



 @ Autowired



  



 public AutowiredBean1 (ServiceAW1 service1, ServiceAW2 Service2) {



    



 super ();



    



 this.service1 = Service1;



    



 this.service2 = Service2;



  



 }





  



 / **



   



 * Business logic ..........



   



 * /







 }



and nell'applicationContext. xml





 <context:annotation-config/>









 <bean id="serviceAW1" class="it.mrwebmaster.di.autowired.ServiceAW1Impl"/>







 <bean id="serviceAW2" class="it.mrwebmaster.di.autowired.ServiceAW2Impl"/>









 <bean id="autowiredBean1" class="it.mrwebmaster.di.autowired.AutowiredBean1"/>



@ Autowired setters or fields of






 {public class AutowiredBean2





  



 @ Autowired



  



 Private ServiceAW1 service1;





  



 Private ServiceAW2 Service2;





  



 public ServiceAW1 getService1 () {



    



 service1 return;



  



 }





  



 public ServiceAW2 getService2 () {



    



 Service2 return;



  



 }





  



 public void setService1 (ServiceAW1 service1) {



    



 this.service1 = Service1;



  



 }





  



 @ Autowired



  



 public void setService2 (ServiceAW2 Service2) {



    



 this.service2 = Service2;



  



 }





  



 / **



   



 * Business logic ..........



   



 * /







 }



and nell'applicationContext. xml
 



 <bean id="autowiredBean2" class="it.mrwebmaster.di.autowired.AutowiredBean2"/>

 
Along with you can use @ @ Autowired Qualifier specfic to the id of the bean you want to inject, for example, if there are two beans that are of the same class, so if there is ambiguity.





 @ Autowired







 @ Qualifier ("serviceAW1")







 Private ServiceAW1 service1;



or





 @ Autowired







 public AutowiredBean4 (@ Qualifier ("serviceAW1") ServiceAW1 service1, ServiceAW2 Service2) {







 .....



Resources

Another very useful feature that Spring provides us with easy access to resources is the (which can be as varied as files or URLs). This feature is implemented through the use of the interface and its implementations Resource.

The interface Resource extends InputStreamSource adding other features. The main implementations of this interface are:

  • UrlResource, allows access to all resources that are accessed through a URL.
  • ClassPathResource, allows access to all resources on the classpath.
  • FileSystemResource, allows access to files.
  • ServletContextResource, allows access to all resources that are normally accessed through the ServletContext.
  • InputStreamResource, allows access to the resource of a given InputStream.
  • ByteArrayResource, allows access to the resource of a given ByteArray.

Through the following example we will see how instaziare Resources and inject into our beans:






 {public class ResourceBean





  



 private Resource resource;





  



 public void setResource (Resource resource) {



    



 this.resource = resource;



  



 }





  



 public Resource getResource () {



    



 return resource;



  



 }







 }



nell'applicationContext. xml





 <bean id="resourceBean" class="it.mrwebmaster.resources.ResourceBean">



  



 <property name="resource" value="classpath:resource"> </ property>







 </ Bean>



In the example you do not define the type of resource that will be instantiated, it instantiates the Spring but it is appropriate. There are three types of string Resource, is based on Queli instaziata Resource:

  • classpath: instantiates a ClassPathResource
  • File: instantiates a FileSystemResource
  • http:instanzia a UrlResource

Spring Java Guide
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links