<?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/8551692java
p:命名空間:spring
xmlns:p="http://www.springframework.org/schema/p"
spring-mvc
做用:簡化在xml配置bean的屬性 在<bean>中使用p:屬性名來配置mvc
AOP:命名空間:
this
xmlns:aop="http://www.springframework.org/schema/aop"spa
xsi:schemaLocation:
.net
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
prototype
做用:簡化AspectJ的AOP在xml中配置code
util:命名空間:
orm
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 代碼同上。