..
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.
{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"/>
{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) {
.....
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:
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:
| |
Linux (Course)
Complete guide to open-source system. From 49 €. |
| |
PHP (Course)
Full course for creating dynamic Web sites. From 49 €. |
| |
Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €. |