基本的javabean对象,省略。。。java
注入方式有两种:set方法和构造器注入spa
<!-- 配置bean set方式注入-->
<bean id="helloWorld" class="com.dao.HelloWorld">
<property name="name" value="Spring"></property>
</bean>xml
<!--构造器注入 -->
<bean id="car" class="com.dao.Car">
<constructor-arg value="Audi"></constructor-arg>
<constructor-arg value="Shanghai"></constructor-arg>
<constructor-arg value="300000.0"></constructor-arg>
</bean>对象
<bean id="person" class="com.dao.Person">
<property name="name" value="Jrry"></property>
<property name="sex" value="boy"></property>
<!-- ref 表示对象关系间的引用 -->
<!--
<property name="car" ref="car"></property>
-->
<!-- 内部bean的使用 -->
<property name="car">
<bean class="com.dao.Car">
<constructor-arg value="BMW"></constructor-arg>
<constructor-arg value="Beijinh"></constructor-arg>
<constructor-arg value="500000.0"></constructor-arg>
</bean>
</property>
</bean>get
//还能够加载list map set 集合,进行级联加载io
//1.建立Spring的ioc容器对象
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
//2.从ioc获取bean的实例class
//下面两种方式加载
HelloWorld world = (HelloWorld) context.getBean("helloWorld");容器
HelloWorld world = (HelloWorld) context.getBean(HelloWorld.class);
world.hello();ioc