spring:一个开源的框架。
1 创建applicationContext.xml 配置文件
1)配置bean
<bean id="helloworld" class = "com.ljs.spring.beans.HelloWorld" scope="prototype">
<property name="name" value="ljs"></property>
</bean>
id: bean的唯一标识
class: 指定全类名
property: 通过set方法给指定的属性赋值
2. 创建ioc 容器,并容器中获取bean
ApplicationContext context =
new ClassPathXmlApplicationContext("aplicationContext.xml");
HelloWorld h1 = (HelloWorld) context.getBean("helloworld");
-------------------
依赖注入:
1 set 方法注入
<bean id="car" class="com.ljs.spring.beans.Car" >
<property name="name" value="baoma"></property>
<property name="price" value="10000"></property>
</bean>
2 构造器注入:提供对应的构造器。
<bean id="car01" class="com.ljs.spring.beans.Car">
<constructor-arg value="baoma"></constructor-arg>
<constructor-arg value="123"></constructor-arg>
</bean>
3 注入含有对象
<bean id="person" class="com.ljs.spring.beans.Person">
<property name="name" value="sb"></property>
<property name="car" ref="car"></property>
</bean>
4 注入对象中含有对象列表
<bean id="personlist" class="com.ljs.spring.beans.PersonList">
<property name="name" value="shbb"></property>
<property name="cars" >
<list>
<ref bean="car" />
<ref bean="car01" />
</list>
</property>
</bean>
5. 注入对象中含有字典map
<bean id="personmap" class="com.ljs.spring.beans.PersonMap">
<property name="name" value="xiaoting"></property>
<property name="map" >
<map>
<entry key="first" value-ref="car"/>
<entry key="second" value-ref="car01" />
</map>
</property>
</bean>