<?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"> <bean id="piano" class="com.springinaction.springidol.Piano" scope="prototype" init-method="init" destroy-method="destory"> </bean> <bean id="people" class="com.springinaction.springidol.People"> <property name="name" value="nihao" /> <!-- 第一种注入 方式<property name="piano" ref="piano" /> --> <!-- 内部bean 的注入--> <property name="piano"> <bean class="com.springinaction.springidol.Piano" /> </property> </bean> </beans>
public class People { private String name; private String age; private String area; private Piano piano; public Piano getPiano() { return piano; } public void setPiano(Piano piano) { this.piano = piano; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } public static void main(String args[]){ ApplicationContext ac=new ClassPathXmlApplicationContext("com/springinaction/springidol/springidol-context-2.xml"); People people=(People)ac.getBean("people"); System.out.println(people.getName()); people.getPiano().play(); } }
一些更加简便的方式 来源 http://blog.csdn.net/joker_zhou/article/details/8551692
p:命名空间:
xmlns:p="http://www.springframework.org/schema/p"
作用:简化在xml配置bean的属性 在<bean>中使用p:属性名来配置
AOP:命名空间:
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation:
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
作用:简化AspectJ的AOP在xml中配置
util:命名空间:
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation:
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
context:命名空间:
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation:
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
作用:1.配置自动扫描bean注解类
context:命名空间:
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation:
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
具体配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="piano" class="com.springinaction.springidol.Piano" scope="prototype" init-method="init" destroy-method="destory"> </bean> <bean id="people" class="com.springinaction.springidol.People" p:name="haha" p:piano-ref="piano" /> </beans>
java 代码同上。