This example describes the basic management of spring beans. Share to everyone for your reference, specific as follows:
One, using setter method to complete dependency injection
The following are the beans and beans-config.xml files.
public class Hellobean {
private String Helloword;
//... Omit getter, setter method
}
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE beans Public "-//spring/dtd bean/en"
"Http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
<bean id= "Hellobean"
class= "Onlyfun.caterpillar.HelloBean" >
<property name= "Helloword" >
<value>hello! justin!</value>
</property>
</bean>
</beans>
public class Springdemo {public
static void Main (string[] args) {
Resource rs = new Filesystemresource ("Beans-con Fig.xml ");
Beanfactory factory = new Xmlbeanfactory (RS);
Hellobean Hello = (hellobean) factory.getbean ("Hellobean");
System.out.println (Hello.gethelloword ());
}
Second, the use of constructor way to complete the injection
public class Hellobean {
private String name;
Private String Helloword;
It is recommended to have no parameter construction method public
Hellobean () {
} public
Hellobean (string name, String helloword) {
this.name = Name;
This.helloword = Helloword;
}
//... Omit getter, setter method
}
<?xml version= "1.0" encoding= "UTF-8"?>
DOCTYPE beans Public "-//spring/dtd bean/en" "Http://www.springframework.org/dtd/spring-beans.dtd" > <beans> <bean id= "Hellobean" class= "Onlyfun.caterpillar.HelloBean" > <constructor-arg index= "0" > ;value>justin</value> </constructor-arg> <constructor-arg index= "1" > <value>he llo</value> </constructor-arg> </bean> </beans>
public class Springdemo {public
static void Main (string[] args) {
ApplicationContext context =
new Filesyste Mxmlapplicationcontext ("Beans-config.xml");
Hellobean Hello = (hellobean) context.getbean ("Hellobean");
System.out.print ("Name:");
System.out.println (Hello.getname ());
System.out.print ("Word:");
System.out.println (Hello.gethelloword ());
}
Third, attribute reference
public class Hellobean {
private String Helloword;
private date date;
//... Omit getter, setter method
}
<beans>
<bean id= "Datebean" class= "java.util.Date"/> <bean id= "Hellobean"
Onlyfun.caterpillar.HelloBean ">
<property name=" Helloword ">
<value>Hello!</value>
</property>
<property name= "Date" >
<ref bean= "Datebean"/>
</property>
</bean>
</beans>
public class Springdemo {public
static void Main (string[] args) {
ApplicationContext context =
new Filesy Stemxmlapplicationcontext ("Beans-config.xml");
Hellobean Hello = (hellobean) context.getbean ("Hellobean");
System.out.print (Hello.gethelloword ());
System.out.print ("It ' s");
System.out.print (Hello.getdate ());
System.out.println (".");
}
}
Four, "Bytype" automatic binding
Automatically binds the bean properties by type by changing the configuration file in "three" to the following.
<beans>
<bean id= "Datebean" class= "java.util.Date"/> <bean id= "Hellobean"
Onlyfun.caterpillar.HelloBean "autowire=" Bytype ">
<property name=" Helloword ">
<value> hello!</value>
</property>
</bean>
</beans>
Five, "byname" automatic binding
You can do this by automatically binding the Bean property by name by changing the configuration file in "three" to the following.
<beans>
<bean id= "Datebean" class= "java.util.Date"/> <bean id= "Hellobean"
Onlyfun.caterpillar.HelloBean "autowire=" byname ">
<property name=" Helloword ">
<value> hello!</value>
</property>
</bean>
</beans>
Six, "constructor" automatic binding
The configuration file in "Three" is changed to the following to complete the automatic binding of the bean properties by construction method. When a dependency is established, the Srping container attempts to establish a bean instance by selecting the type of the bean instance in the container, as well as the type of the parameter on the associated constructor, and, if so, using the construction method. Throws a Org.springframework.beans.factory.UnsatisfiedDependencyException exception if it cannot be bound.
<beans>
<bean id= "Datebean" class= "java.util.Date"/> <bean id= "Hellobean"
Onlyfun.caterpillar.HelloBean "autowire=" constructor ">
<property name=" Helloword ">
<value >Hello!</value>
</property>
</bean>
</beans>
Six, "AutoDetect" automatic binding
The automatic binding of the bean properties can be completed by changing the configuration file in "three" to the following, which spring will try to use in constructor to handle the establishment of the dependencies, and if not, then try to establish a dependency with the Bytype class.
<beans>
<bean id= "Datebean" class= "java.util.Date"/> <bean id= "Hellobean"
Onlyfun.caterpillar.HelloBean "autowire=" AutoDetect ">
<property name=" Helloword ">
<value> hello!</value>
</property>
</bean>
</beans>
Vii. Dependent inspection Methods
In automatic binding, because there is no way from the definition file to clearly see whether each property is set, in order to determine that certain dependencies are actually established, you can, if you rely on checks, set "Dependency-check" when the <bean> tag is used. There are four ways to rely on checking: simple, objects, all, none.
Simple: Only check whether the properties of a primitive type (such as native data type or string object) complete a dependency relationship.
Objects: Checks whether the property of the object type completes the dependency relationship.
All: Checks whether all properties complete dependencies.
None: setting is the default value, which means that dependencies are not checked.
<beans>
<bean id= "Datebean" class= "java.util.Date"/> <bean id= "Hellobean"
Onlyfun.caterpillar.HelloBean "autowire=" AutoDetect "dependeny-check=" All "> <property name=" Helloword "
>
<value>Hello!</value>
</property>
</bean>
</beans>
Viii. Collection Object Injection
For collection objects such as arrays, lists, sets, and maps, you must populate some objects into the collection before you inject them, and then inject the collection objects into the desired bean, or you can submit the spring IOC container to automatically maintain or generate the collection objects and complete the dependency injection.
public class Somebean {private string[] somestrarray;
Private some[] Someobjarray;
Private List somelist;
Private Map Somemap;
Public string[] Getsomestrarray () {return somestrarray;
} public void Setsomestrarray (string[] somestrarray) {this.somestrarray = Somestrarray;
Some[] Getsomeobjarray () {return someobjarray;
} public void Setsomeobjarray (some[] someobjarray) {this.someobjarray = Someobjarray;
Public List getsomelist () {return somelist;
The public void Setsomelist (List somelist) {this.somelist = somelist;
Public Map Getsomemap () {return somemap;
The public void Setsomemap (Map somemap) {this.somemap = Somemap;
} public class Some {private String name;
Public String GetName () {return name;
public void SetName (String name) {this.name = name;
Public String toString () {return name; }
}
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE beans Public "-//spring/dtd bean/en" "Http://www.springframework.org/dtd/spring-beans.dtd" > <beans> <bean id= "some1" class= "Onlyfun.caterpillar.Some" > <property name= "name" > <value>Justin< /value> </property> </bean> <bean id= "some2" class= "Onlyfun.caterpillar.Some" > <PR Operty name= "name" > <value>momor</value> </property> </bean> <bean id= "som Ebean "class=" Onlyfun.caterpillar.SomeBean "> <property name=" Somestrarray "> <list>
;value>hello</value> <value>Welcome</value> </list> </property> <property name= "Someobjarray" > <list> <ref bean= "some1"/> <ref bean= "some"
2 "/> </list> </property> <property name=" Somelist "> <list> <value>ListTest</value> <ref bean= "some1"/> <ref bean= "s Ome2 "/> </list> </property> <property name=" Somemap "> <map> & Lt;entry key= "Maptest" > <value>hello! justin!</value> </entry> <entry key= "SomeKey1" > <ref bean= "some1"/>
;
</entry> </map> </property> </bean> </beans>
public class Springdemo {public static void main (string[] args) {Applicationco
ntext context = new Filesystemxmlapplicationcontext ("Beans-config.xml");
Somebean Somebean = (Somebean) context.getbean ("Somebean");
Get array type Dependency Injection object string[] STRs = (string[)) Somebean.getsomestrarray ();
Some[] Somes = (some[]) Somebean.getsomeobjarray ();
for (int i = 0; i < strs.length i++) {System.out.println (Strs[i] + "," + somes[i].getname ());
//Get list Type Dependency Injection Object System.out.println ();
List somelist = (list) somebean.getsomelist ();
for (int i = 0; i < somelist.size (); i++) {System.out.println (Somelist.get (i));
//Get Map Type Dependency Injection Object System.out.println ();
Map Somemap = (map) somebean.getsomemap ();
System.out.println (Somemap.get ("maptest"));
System.out.println (Somemap.get ("SomeKey1")); }
}
I hope this article will help you with your Java programming.