在applicationContext.xml中配置如下代码
<!-- 在spring主配置文件中,告诉spring IOC容器,user在这里, spring一旦运行起来就会首先加载该配置文件作为spring的上下文 创建user对象(实例) --> <bean id="user" class="com.spring.di.User"> <property name="id" value="2"></property> <property name="username"> <value>marry</value> </property> </bean> <bean id="person" class="com.spring.di.Person"> <property name="name"> <value>zhangsan</value> </property> <property name="age" value="32"></property> <property name="sal" value="3200"></property> <property name="user"> <ref bean="user"></ref> </property> <property name="user2"> <!-- 内部bean 作用范围只在当前person bean 的内部 外部是看不见 不能被外部bean使用 --> <bean class="com.spring.di.User"> <property name="id" value="22"></property> <property name="username"> <value>marry2</value> </property> </bean> </property> </bean> <bean id="dog" class="com.spring.di.Dog"> <!-- 根据构造器参数的索引来注入 --> <!-- <constructor-arg value="小花花" index="0"></constructor-arg> <constructor-arg value="白色" index="1"></constructor-arg> --> <!-- 根据构造器参数类型注入 --> <constructor-arg value="小花花" index="0" type="java.lang.String"></constructor-arg> <constructor-arg value="白色" index="1" type="java.lang.String"></constructor-arg> <constructor-arg value="1" index="2" type="java.lang.Integer"></constructor-arg> <constructor-arg type="com.spring.di.User" ref="user"></constructor-arg> </bean> <bean id="cat" class="com.spring.di.Cat"> <property name="name" value="詹尼"></property> <!-- 注入null值 也可以不注入 --> <property name="color"> <null /> </property> </bean> <!-- 集合类型注入 --> <bean id="pet" class="com.spring.di.Pet"> <!-- List类型注入 --> <property name="dogs"> <list> <value>小花花</value> <value>珍妮</value> <value>旺财</value> <value>小强</value> </list> </property> <!-- Set类型注入 --> <property name="cats"> <set> <value>小花花</value> <value>珍妮</value> <value>旺财</value> <value>小强</value> </set> </property> <!-- Map类型注入 --> <property name="pigs"> <map> <entry> <key> <value>1</value> </key> <value>大白</value> </entry> <entry key="2" value="小白"></entry> </map> </property> <!-- 数组类型注入 --> <property name="birds"> <list> <value>新天问</value> <value>喜鹊</value> </list> </property> <!-- Properties类型属性注入 --> <property name="pp"> <props> <prop key="p1">p1_value</prop> <prop key="p2">p2_value</prop> </props> </property> </bean> <!-- 自动装配 byName byTime --> <bean id="a" class="com.spring.auto.A"> <property name="aname" value="AAA"></property> </bean> <bean id="b" class="com.spring.auto.B"> <property name="bname" value="BBB"></property> </bean> <!-- 这是正常的对象属性注入 --> <bean id="ab" class="com.spring.auto.AB"> <property name="a"> <ref bean="a"></ref> </property> <property name="b"> <ref bean="b"></ref> </property> </bean> <!-- byName spring的IOC容器会根据AB类的属性名称会去在容器中自动查找id等于其属性名称的bean进行自动装配(实现注入) --> <bean id="ab_byName" class="com.spring.auto.AB" autowire="byName"> </bean> <!-- byType spring的IOC容器会根据AB类的属性类型去容器中自动查找类型等同于其属性类型的bean进行自动装配(实现注入) 如果出现了多个类型相同的bean 使用byType自动装配就会出现异常 org.springframework.beans.factory.NoUniqueBeanDefinitionException --> <!-- <bean id="b1" class="com.spring.auto.B"> <property name="bname" value="BBB1"></property> </bean> --> <bean id="ab_byType" class="com.spring.auto.AB" autowire="byType"> </bean> <!-- 分散装配 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer --> <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>test.properties</value> </property> </bean> test.propertie文件
key1=value1
key2=value2
key3=value3
<bean id="testProperties" class="com.spring.auto.TestProperties"> <property name="key1" value="${key1}"></property> <property name="key2" value="${key2}"></property> <property name="key3" value="${key3}"></property> </bean>
测试代码如下
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); /*AB ab= (AB) ac.getBean("ab"); System.out.println(ab);*/ AB ab_byName= (AB) ac.getBean("ab_byName"); System.out.println(ab_byName); /* AB ab_byType= (AB) ac.getBean("ab_byType"); System.out.println(ab_byType);*/ /*TestProperties testProperties= (TestProperties) ac.getBean("testProperties"); System.out.println(testProperties);*/
/* User user = new User(); //代码的耦合在一起,不利于后期代码的维护 user.setUsername("jack"); System.out.println(user.toString()); System.out.println("------------------------"); User user2 = new User(1,"tom","123"); System.out.println(user2.toString());*/ //启动spring的IOC容器 加载主配置文件 /* @SuppressWarnings("resource") ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml"); User user =(User) ac.getBean("user"); System.out.println(user.toString());*/ ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person) ac.getBean("person"); System.out.println(person.toString()); /*Cat cat=(Cat) ac.getBean("cat"); System.out.println(cat);*/ /*Pet pet= (Pet) ac.getBean("pet"); System.out.println(pet);*/
时间: 2024-10-14 03:54:33