This article describes the basic management of Spring Bean with examples. Share it with everyone for your reference, the details are as follows:
1. Use setter method to complete dependency injection
Below are the Bean and beans-config.xml files.
public class HelloBean { private String helloWord; //...omit getter and setter methods}
<?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" > <property name="helloWord"> <value>Hello!Justin!</value> </property> </bean> </beans>
public class SpringDemo { public static void main(String[] args) { Resource rs = new FileSystemResource("beans-config.xml"); BeanFactory factory = new XmlBeanFactory(rs); HelloBean hello = (HelloBean) factory.getBean(" helloBean"); System.out.println(hello.getHelloWord()); } }
2. Use the constructor method to complete the injection
public class HelloBean { private String name; private String helloWord; // It is recommended to have a parameterless construction method public HelloBean() { } public HelloBean(String name, String helloWord) { this.name = name; this.helloWord = helloWord; } //...Omit getter and setter methods}
<?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" > <constructor-arg index="0"> <value>Justin</value> </constructor-arg> <constructor-arg index="1"> <value>Hello</value> </constructor-arg> </bean> </beans>
public class SpringDemo { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext("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()); } }
3. Attribute reference
public class HelloBean { private String helloWord; private Date date; //...omit getter and setter methods}
<beans> <bean id="dateBean"/> <bean id="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 FileSystemXmlApplicationContext("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("."); } }
4. "byType" automatic binding
Change the configuration file in "Three" to the following to complete the automatic binding of bean attributes by type.
<beans> <bean id="dateBean"/> <bean id="helloBean" autowire="byType"> <property name="helloWord"> <value>Hello!</value> </property> </bean> </beans>
5. "byName" automatic binding
Change the configuration file in "Three" to the following to complete the automatic binding of bean attributes by name.
<beans> <bean id="dateBean"/> <bean id="helloBean" autowire="byName"> <property name="helloWord"> <value>Hello!</value> </property> </bean> </beans>
6. "constructor" automatic binding
Change the configuration file in "Three" to the following to complete the automatic binding of bean attributes according to the construction method. When establishing a dependency relationship, the Spring container will try to compare the Bean instance type in the container and the parameter type on the related constructor to see if the type is consistent. If so, the constructor will be used to create the Bean. Example. If it cannot be bound, an org.springframework.beans.factory.UnsatisfiedDependencyException exception is thrown.
<beans> <bean id="dateBean"/> <bean id="helloBean" autowire="constructor"> <property name="helloWord"> <value>Hello!</value> </property> </bean> </beans>
6. “autodetect” automatic binding
Change the configuration file in "Three" to the following to complete the automatic binding of bean properties. This automatic binding means that Spring will try to use the constructor to handle the establishment of dependencies. If it does not work, try to use the byType class to establish it. Dependencies.
<beans> <bean id="dateBean"/> <bean id="helloBean" autowire="autodetect"> <property name="helloWord"> <value>Hello!</value> </property> </bean> </beans>
7. Dependency checking method
In automatic binding, since there is no way to clearly see whether each attribute has been set from the definition file, in order to ensure that certain dependencies are indeed established, you can set the dependency check when using the <bean> tag. Defining "dependency-check", there are four dependency checking methods: simple, objects, all, and none.
simple: Only checks whether the properties of simple types (like primitive data types or string objects) complete dependencies.
objects: Check whether the properties of the object type complete the dependency.
all: Check whether all attributes complete dependencies.
none: The setting is the default value, indicating that dependencies are not checked.
<beans> <bean id="dateBean"/> <bean id="helloBean" autowire="autodetect" dependeny-check="all"> <property name="helloWord"> <value>Hello!</value> < /property> </bean> </beans>
8. Collection object injection
For collection objects such as arrays, Lists, Sets, and Maps, some objects must be filled into the collection before injection. Then when the collection objects are injected into the required beans, they can also be automatically maintained or generated by Spring's IoC container. Collection objects and complete 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; } public Some[] getSomeObjArray() { return someObjArray; } public void setSomeObjArray(Some[] someObjArray) { this.someObjArray = someObjArray; } public List getSomeList() { return someList; } public void setSomeList(List someList) { this.someList = someList; } public Map getSomeMap() { return someMap; } 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"> <property name="name"> <value>Justin</value> </property> </bean> <bean id="some2"> <property name="name"> <value>momor</value> </property> </bean> <bean id="someBean"> <property name="someStrArray"> <list> < value>Hello</value> <value>Welcome</value> </list> </property> <property name="someObjArray"> <list> <ref bean="some1"/> <ref bean="some2"/> </list> </property> <property name="someList"> <list> <value>ListTest</value> <ref bean="some1"/> <ref bean="some2" /> </list> </property> <property name="someMap"> <map> <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) { ApplicationContext context = new FileSystemXmlApplicationContext( "beans-config.xml"); SomeBean someBean = (SomeBean) context.getBean("someBean"); // Get array type Stateful 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 the 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 be helpful to everyone in Java programming.