本文共 1911 字,大约阅读时间需要 6 分钟。
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans" 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来创建对象,在spring这些都称为bean --> <bean id="hello" class="com.lixv.entity.Hello"> <constructor-arg index="0" value="xxxxxxxxxxxx"/> </bean> <bean id="helloSpring" class="com.lixv.entity.HelloSpring" name="helloSpringNew1,helloSpringNew2"> <property name="springStr" value="springstr"/> <property name="hello" ref="hello"/> </bean> <alias name="helloSpring" alias="helloSpringNew"/></beans>
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans" 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"> <import resource="beans2.xml"/> <import resource="beans.xml"/></beans>
getBean("helloSpringNew1")
获取这个id会得到最后import的beans中的bean(当id相同时:后import的会覆盖先import的)package com.lixv.dao;import com.lixv.entity.Hello;import com.lixv.entity.HelloSpring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloSpring hellospring1 = (HelloSpring) context.getBean("helloSpringNew1"); System.out.println(hellospring1); }}
转载地址:http://voeq.baihongyu.com/