Developers only need to do two things under the Spring framework: Develop beans, configure beans. So we have to do: in the eyes of the XML, the play is to think of Java class code, because the srping essence is to drive Java code through XML, each XML corresponds to a piece of Java code.
Scope of the Bean
The Scope property allows you to set the following scopes for the bean.
Singleton: There is always only one instance. The default is singleton. The schema is continuously tracked for maintenance after creation.
Prototype: Each get is a new instance. After new, it doesn't matter.
Request: It is always the same as the one requested, the end of which the instance disappears. You need to add code to the Web. XML to make it effective.
<listener> <listener-class> org.springframework.web.context.request.requestcontextlistener</ Listener-class></listener>
Session: Always the same in a conversation.
Global session: Each global HTTP session corresponds to the same instance.
In the Web, if you use JavaScript scripts to get containers, you should write this:
Webapplicationcontext ctx=webapplicationcontextutils.getwebapplicationcontext (application); Person p= (person) ctx.getbean ("person");
Configuration dependencies
The dependency is either a deterministic value or a reference to another bean. Beanfactory The bean is required to create the bean after the container is created. ApplicationContext is the first to create a singleton bean. Remember before we used ref to specify a reference to a bean that was already in the container. If we set the Default-autowire property to beans, the schema can automatically load the objects it depends on as needed by the bean. Attribute values are no, byname, Bytype. Bytype If there is exactly one bean type that matches the type required by the set method, that's it! If more than one eligible Bean, the exception! ByName, if the name of the set method is the same as the name of a bean, it is him! If not found, nothing is injected. There are also constructor and AutoDetect decisions.
Inject nested beans
A nested bean is a bean element that comes under a property or Constructor-args, which is simply a parameter to a set or constructor, and it cannot be fetched by a container.
Only if we are sure that we will never have a chance to access this bean can we set it as an embedded bean.
Inject Collection value
The injection set is no different from the normal type, just that the property is no longer using the value attribute, but instead the seed element is injected into the list (list and array are used this), map, props, set. The child element underneath the list is value. There are entry sub-elements under map, set key and value (or VALUE-REF, which is the map for the object). Set what can be put inside, value specifies the base type, ref specifies other instances in the container, the bean formulates a nested bean,list,set,map,props can be nested another set. Props is the meaning of the property, which is specified in the form of <prop key= "Length" >18</prop>. The types of elements in the collection can be determined by generics.
Combining attributes
The so-called composite attribute is the ability to assign values to the attributes of a bean in the current bean, using the form of a similar OGNL expression. For example, the name assignment for the person attribute in the bean example is as follows:
<bean id= "Example" class= "Com.cm.example" > <property name= "person.name" value= "Alvin"/></bean>
Since this is written, then the person must not be empty, or where to find the name? This configuration is equivalent to the following write in the example class:
Example.getPerson.setName ("Alvin");
This article from "Fingertip Light Fly" blog, declined reprint!
Spring note--3. Beans in a container