<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Spring 入门配置Hi --> <bean id="UserDao" class="cn.nyist.spring.demo1.UserServiceIml"> <property name="name" value="李东"/> </bean> <!-- Spring的sBean的生命周期配置 --> <bean id="customDao" class="cn.nyist.spring.demo2.CustomImpl" init-method="setup" destroy-method="destroy"></bean> <!-- Spring的注入方式 --> <!-- g构造方法的方式 --> <bean id="car" class="cn.nyist.spring.demo3.Car"> <constructor-arg name="name" value="lisi"/> <constructor-arg name="price" value="500000"/> </bean> <!-- Set方法的注入方式 --> <bean id="car1" class="cn.nyist.spring.demo3.Car1"> <property name="price" value="50000000"/> <property name="name" value="BaoMa"/> </bean> <!-- Set方法引入对象 --> <bean id="car10" class="cn.nyist.spring.demo3.User" > <property name="name" value="tom"></property> <property name="getCar" ref="car1"></property> </bean> <!-- P名称空间的方式 --> <bean id="car2" class="cn.nyist.spring.demo3.Car1" p:name="奇瑞" p:price="200000"> </bean> <!-- SpELl的属性注入方式--> <bean id="car3" class="cn.nyist.spring.demo3.Car1"> <property name="name" value="#{'李四'}"></property> <property name="price" value="#{30000}"></property> </bean> <bean id="coo" class="cn.nyist.spring.demo3.Car1"> <property name="name" value="#{'赵红'}"></property> <property name="price" value="#{200000}"></property> </bean> <!-- 引入标签 --> <import resource="applicationContext2.xml"/> </beans>
@Test public void func2(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml"); Car1 car1 = (Car1)applicationContext.getBean("car4"); System.out.println(car1); }