..
In this section we analyze the configuration through the IOC container xml metadata.
For a bean instazionare nell'IoC contanier enough for us just two infomarzioni:
<beans id="myBean" class="com.MyClass"/> <beans class="com.MyClass1"/>The second example is deliberately omitted the id attribute, because if you do not specify it generates a random Spring. However, it is recommended that you always specify an id, so you can reference the bean if necessary. A bean can have multiple identifiers that can be added through the name attribute or by the alias tag:
<beans id="myBean" class="com.MyClass" name="alias1 alias2" />or
<alias name="myBean" alias="alias1" />
To instantiate the Spring beans using three methods:
{public class FactoryMethodBean
private static instance = new FactoryMethodBean FactoryMethodBean ();
Private FactoryMethodBean () {}
public static FactoryMethodBean getInstance () {
return instance;
}
}
and nell'applicationContext. xml
<bean id="factoryMethodExample" class="it.mrwebmaster.singleton.FactoryMethodBean" factory-method="getInstance"/>
The last method of instantiation uses, in addition to the factory method, a bean factory, which is typical of the Service Locator .
FactoryBean {public class
Private ExampleBean former;
public FactoryBean () {
super ();
Former ExampleBeanImpl = new ();
}
public ExampleBean getExampleBean () {
return ex;
}
}
and nell'applicationContext. xml
<bean id="factoryBean" class="it.mrwebmaster.factory.FactoryBean" /> <bean id="exampleBean" class="it.mrwebmaster.factory.ExampleBean" factory-bean="factoryBean" factory-method="getExampleBean"/>
An important property of a bean is its scope, which defines the life cycle. Spring supports five types of scope:
The singleton and prototype scopes are available for all ApplicationContext as request, session, global session and are valid only for the WebApplicationContext.
If the scope attribute is not specified, the bean becomes singleton scope by default.
In addition to these scopes there exists a sixth, but the scope thread is not enabled by default. In Spring you can create custom scopes, but is left to the reader the depth of this topic.
| |
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 €. |