您的位置:首页 > 编程语言 > Java开发

Spring基本知识1

2015-10-14 19:21 441 查看
<?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:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 这是在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>邵磊是傻逼</value>
</property>
<property name="pasword">
<value>aass</value>
</property>
</bean>
<bean id="person" class="com.spring.di.Person">

<property name="name">
<value>是是是</value>
</property>
<property name="age">
<value>22</value>
</property>
<property name="salary">
<value>122222</value>
</property>
<property name="user">
<ref bean="user"></ref>
</property>
<property name="user2">
<!-- 内部bean对象作用范围只在当前personbean的内部,外部 是看不见的不能被外部bean使用 -->
<bean class="com.spring.di.User">
<property name="id" value="4"></property>
<property name="username">
<value>邵</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="白色" type="java.lang.String"></constructor-arg>
<constructor-arg value="小花花" type="java.lang.String"></constructor-arg>
<constructor-arg value="1" type="java.lang.Integer"></constructor-arg>
<constructor-arg ref="user" type="com.spring.di.User"></constructor-arg>

</bean>
<!-- 注入空值 也可以注入 -->
<bean id="cat" class="com.spring.di.Cat">
<property name="name" value="珍妮"></property>
<property name="color">

<null />
</property>
</bean>
<!-- 范型注入 -->
<bean id="pet" class="com.spring.di.Pet">
<property name="dogs">
<list>
<value>小花花</value>
<value>珍妮</value>
<value>旺财</value>
<value>小强</value>
</list>
</property>
<property name="cats">
<set>
<value>小mai</value>
<value>珍妮d</value>
<value>旺财ddd</value>
<value>小强dd</value>
</set>
</property>
<property name="pigs">
<map>
<entry>
<key>
<value>1</value>
</key>
<value>dddddddd</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>
<!-- 自动注入 -->

     <bean id="a" class="com.spring.auto.A">

     <property name="name" 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="byname" class="com.spring.auto.AB"  autowire="byName">

        </bean>

        <!-- bytypeSpring 的IOC容器会根据AB类的属性类型去在容器中自动查找类型等同于属性类型的bean 进行自动装配,实现注入 -->

          <bean id="bytype" class="com.spring.auto.AB"  autowire="byType">

        </bean>

        <!--注意!!多个类型相同的bean使用bytype自动装配就会报出异常 org.springframework.beans.factory.NoUniqueBeanDefinitionException: -->

         

     

     <!-- <bean id="b1" class="com.spring.auto.B">

     <property name="bname" value="BBB1"></property> </bean> -->

     

     <!-- 分散装配PropertyPlaceHolderConfigurer -->

     <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

     <property name="location">

     <value>test.properties</value>

     </property>

     

     </bean>

     <bean id="testpro" class="com.spring.auto.Location">

     <proper
953f
ty name="key1" value="${key1}"></property>

     <property name="key2" value="${key2}"></property>

     <property name="key3" value="${key3}"></property>

     

     

     </bean>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: