Hellobean, a simple bean, contains a property "hello.
Hellobean. javapackage javamxj. Spring. Basic. autowiring;
Public class hellobean {
Private string hello;
Public String gethello (){
Return hello;
}
Public void sethello (string Hello ){
This. Hello = Hello;
} · The hellodate class first defines three constructors and then sets three attributes: Hello, date, and date2.
Hellodate. Java
| Package javamxj. Spring. Basic. autowiring; Import java. util. date; Import java. util. gregoriancalendar; Public class hellodate {public hellodate (){ System. Out. println ("defalt constructor called "); } Public hellodate (hellobean Hello ){ System. Out. println ("hellodate (hellobean) called "); } Public hellodate (hellobean hello, date ){ System. Out. println ("hellodate (hellobean, date) called "); } Public void sethello (hellobean Hello ){ System. Out. println ("Property Hello set "); } Public void setdate (date ){ System. Out. println ("Property date set "); } Public void setdate2 (gregoriancalendar date ){ System. Out. println ("Property date2 set "); } } |
· Beans. xml defines seven beans,
Beans. xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="date" name="myDate" class="java.util.Date"/>
<bean id="helloBean" class="javamxj.spring.basic.autowiring.HelloBean"
dependency-check="simple">
<property name="hello" value="javamxj"/>
</bean>
<bean id="HelloByName" class="javamxj.spring.basic.autowiring.HelloDate"
autowire="byName"/>
<bean id="HelloByType" class="javamxj.spring.basic.autowiring.HelloDate"
autowire="byType"/>
<bean id="HelloConstructor" class="javamxj.spring.basic.autowiring.HelloDate"
autowire="constructor"/>
<bean id="HelloAutodetect" class="javamxj.spring.basic.autowiring.HelloDate"
autowire="autodetect"/>
<bean id="helloCheck" class="javamxj.spring.basic.autowiring.HelloDate" autowire="byType"
dependency-check="objects">
<property name="date2" >
<bean class="java.util.GregorianCalendar"/>
</property>
<!-- <property name="date" ref="date"/>-->
<!-- <property name="hello" ref="helloBean"/>-->
</bean>
</beans>
Package javamxj. Spring. Basic. autowiring;
Import org. springframework. Beans. Factory. beanfactory; Import org. springframework. Beans. Factory. xml. xmlbeanfactory; Import org. springframework. Core. Io. classpathresource;
Public class main { Public static void main (string [] ARGs ){ Beanfactory BF = new xmlbeanfactory (New classpathresource ( "Javamxj/spring/basic/autowiring/beans. xml "));
System. Out. println ("use byname :"); Hellodate HB = (hellodate) BF. getbean ("hellobyname ");
System. Out. println ("/n uses bytype :"); HB = (hellodate) BF. getbean ("hellobytype ");
System. Out. println ("/N using constructor :"); HB = (hellodate) BF. getbean ("helloconstructor ");
System. Out. println ("/N use autodetect :"); HB = (hellodate) BF. getbean ("helloautodetect ");
System. Out. println ("/n uses dependency-check :"); HB = (hellodate) BF. getbean ("hellocheck "); } } |
Running result:Use byname:
Defalt constructor called
Property date set uses bytype:
Defalt constructor called
Property date set
Property Hello set uses constructor:
Hellodate (hellobean, date) called uses autodetect:
Defalt constructor called
Property date set
Property Hello set uses dependency-check:
Defalt constructor called
Property date2 set
Property date set
Property Hello set
Refer to the running results to describe the configuration of beans. XML in detail:· If autowire = "byname" is specified for hellobyname, spring automatically binds the bean Based on the bean name and attribute name. For example, hellodate contains the attributes hello, date, and date2. In beans. XML, there are two beans: Date and hellobean. Therefore, only date meets the requirements. (Bean name includes ID name and name) · hellobytype specifies autowire = "bytype". Here hellodate contains attributes: Hello, date, date2, in beans. XML contains the date class and hellobean class, matching the date and hello attributes respectively. · Helloconstructor specifies autowire = "constructor", which preferentially calls constructors with many parameters. · Helloautodetect specifies autowire = "autodetect". If a default constructor is found, bytype is applied. · If dependency-check = "simple" is specified in hellobean, the dependency of the basic type and set is checked. If the attribute hello is not assigned a value, an exception is thrown. · Dependency-check = "objects" is specified in hellocheck, and autowire = "bytype" is also specified. Although the date and hello attributes are automatically bound, however, because the property date2 is not automatically bound to, special settings are required.