After the completion of build environment. Use spring config editor to open the spring xml configuration file.
To achieve their own, and some basic applications on the java spring. Saw a total of four videos. Here is his realization of code:
The most basic is to create a spring ioc container object. Obtained from the bean instance in ioc
For spring xml file is written as follows:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <!-- 配置bean文件 --> <bean id="helloword" class="lib.Helloword"> <property name="name" value="spring"></property> </bean> <bean id="car22" class="lib.Car"> <constructor-arg value="wen" index="0"></constructor-arg> <constructor-arg index="1"> <value><![CDATA[<ncjsnjkad>]]></value> </constructor-arg> </bean> <bean id="person" class="lib.Person"> <property name="name" value="Tom"></property> <property name="age" value="24" <-! establish a relationship between the bean ref wording ->> </ Property> <!-- <property name="car" ref="car2"></property> <property name="car"> <ref bean="car2"/> </property>--> <!-- 内部的bean的使用方法,不能为外部的引用 --> <property name="car"> <bean class="lib.Car"> <constructor-arg value="wen" index="0"></constructor-arg> <constructor Arg-index = " . 1 " > <value> <! [CDATA [<ncjsnjkad>]]> </ value> </ Arg-constructor> <-! This is the null-specific tags <constructor-arg> < null /> </ Arg-constructor> -> <-! assignment need to initialize the property can be assigned to the cascade cascade attribute times <property name = " PERSON.NAME " value = " 399 " > </ Property> -> </ the bean> </ Property> </ the bean> <the bean ID = "car1" class="lib2.Car"> <constructor-arg value="wen" index="0"></constructor-arg> <constructor-arg index="1"> <value><![CDATA[<ncjsnjkad>]]></value> </constructor-arg> </bean> <bean id="car2" class="lib2.Car"> <constructor-arg value="wen" index="0"></constructor-arg> <constructor-arg index="1"> <value><![CDATA[<ncjsnjkad>]]></value> </constructor-arg> </bean> <bean id="car3" class="lib2.Car"> <constructor-arg value="wen" index="0"></constructor-arg> <constructor-arg index="1"> <value><![CDATA[<ncjsnjkad>]]></value> </constructor-arg>person3" <the bean ID = <-! configuration set Properties -> </ the bean>" class="lib2.Person"> <property name="name" value="mike"></property> <property name="age" value="24"></property> <property name="cars"> <list> <ref bean="car1"/> <ref bean="car2"/> <ref bean="car3"/> </list> </property> </bean> <!-- 配置map属性值 --> <bean id="person33" class="lib2.Person3"> <property name="name" value="mao"></property> <property name="age" value="34"></property> <property name="cars"> <map> <!-- 使用map的节点和map的子节点entry进行配置 --> <entry key="AA" value-ref="car1"></entry> <entry key="BB" value-ref="car2"></entry> </map> </property> </bean> <bean id="datasourse" class="lib2.Datasourse"> <property name="properties"> <! - the use of props and prop node to the configuration information for the properties -> <props> <prop Key = " US " > djkchsk </ prop> <prop Key = " EE " > hkjcsd </ prop> <prop = Key " WWW " > chjdss </ prop> </ the props> </ Property> </ bean> <- configuration example of a single set of bean, bean performed for a plurality of reference ->! <util: ID List = " cars " > < REF the bean = "car1"/> <ref bean=" CAR2 " /> < ref bean = " Car3 " /> </ util: List> ! <- for the property assignment bean, you need to import the namespace p through the rankings next space, to the traditional more concise - > <the bean ID = " person5 " class = " lib2.Person " P: Age = " 30 " P: name = " quens " P: cars- REF = " cars " > </ the bean> </ Beans>
Wherein the main function simply list:
package lib2; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ceshi.newww; public class spring { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext se3t=new ClassPathXmlApplicationContext("applicationcontext.xml"); Person person3=(Person) se3t.getBean("person3"); System.out.println("1 "+person3); Person3 person33=(Person3) se3t.getBean("person33"); System.out.println("2 "+person33); Datasourse datasourse=se3t.getBean(Datasourse.class); System.out.println("3 "+datasourse); } }