Spring 之bean的注入

Posted 轻语

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 之bean的注入相关的知识,希望对你有一定的参考价值。

<?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:context="http://www.springframework.org/schema/context"
 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd    
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
  <bean id="egg" class="com.auto.entity.SmallEgg"></bean>
  <bean id="a" class="com.auto.entity.BigEgg"></bean>
  <bean id="rice" class="com.auto.entity.Rice"></bean>
  <!-- 构造器参数注入 -->
  <!-- <bean id="eggAndRice" class="com.auto.entity.EggAndRice">
   <constructor-arg index="0" ref="egg"></constructor-arg>
   <constructor-arg index="1" ref="rice"></constructor-arg>
  </bean> -->
  <!-- set参数注入 -->
  <!-- <bean id="eggAndRice" class="com.auto.entity.EggAndRice">
   <property name="eg" ref="egg"></property>
   <property name="rice" ref="r"></property>
  </bean> -->
  <!-- 自动装配 -->
  <bean id="eggAndRice" class="com.auto.entity.EggAndRice"
   autowire="byName" ></bean>
   
   <bean id="dataSource" class="com.auto.common.DataSource">
    <property name="username" value="#{jdbc.user}"></property>
    <!--SpringEl  -->
    <property name="password" value="#{jdbc.password}"></property>
    <property name="url" value="#{jdbc.url}"></property>
    <property name="driver" value="#{jdbc.driver}"></property>
   </bean>
   <bean id="userDao" class="com.auto.dao.UserDaoImpl" autowire="byName"></bean>
<!--   <bean id="userDao" class="com.auto.dao.mysqlUserdaoImpl" autowire="byName"></bean> -->
   <bean id="userService" class="com.auto.service.UserService" autowire="byName"></bean>
 <!-- 基本参数注入 -->
  <bean id="exam" class="com.auto.entity.ExampleBean">
   <property name="id" value="1"></property>
   <property name="sal" value="222.3"></property>
   <property name="job" value="Information Technology"></property>
   <property name="isMarry" value="true"></property>
  </bean>
 <!-- 集合、键值对属性注入 --> 
  <bean id="exam2" class="com.auto.entity.ExampleBean">
   <property name="objs">
    <list>
     <value>zs</value>
     <bean class="java.util.Date"></bean>
     <bean class="com.auto.entity.Rice"></bean>
    </list>
   </property>
   <property name="sets">
    <set>
     <value>zs</value>
     <value>ls</value>
     <value>zs</value>
    </set> 
   </property>
   
   <property name="maps">
    <map>
     <entry key="a" value="zs"></entry>
     <entry key="b" value-ref="eggAndRice"></entry>
     <entry key="c">
      <bean class="com.auto.entity.Egg"></bean>
     </entry>
    </map>
   </property>
   
   <property name="props">
    <props>
     <prop key="url" >jdbc:oracle:thin:@localhost:1521:xe</prop>
     <prop key="driver">oracle.jdbc.driver.OracleDriver</prop>
     <prop key="a">zs</prop>
    </props>
   </property>
  </bean>
  <!--引用方式注入  -->
  <bean id="exam3" class="com.auto.entity.ExampleBean">
   <property name="objs" ref="list"></property>
   <property name="sets" ref="set"></property>
   <property name="maps" ref="map"></property>
   <property name="props" ref="prop"></property>
  </bean>
  
  
  <util:list id="list" >
   <value>zs</value>
   <bean class="java.util.Date"></bean>
   <bean class="com.auto.entity.Rice"></bean>
  </util:list>
  
  <util:set id="set">
    <value>zs</value>
    <value>ls</value>
    <value>zs</value>
  </util:set>
  
  <util:map id="map">
   <entry key="a" value="zs"></entry>
   <entry key="b" value-ref="eggAndRice"></entry>
   <entry key="c">
    <bean class="com.auto.entity.Egg"></bean>
   </entry>
  </util:map>
  
  <util:properties id="prop">
   <prop key="url" >jdbc:oracle:thin:@localhost:1521:xe</prop>
   <prop key="driver">oracle.jdbc.driver.OracleDriver</prop>
   <prop key="a">zs</prop>
  </util:properties>
  
  <util:properties id="jdbc" location="classpath:com/auto/test/db.properties"></util:properties>
</beans>

 





















































































































以上是关于Spring 之bean的注入的主要内容,如果未能解决你的问题,请参考以下文章

Spring的三种注入方式扩展之spring对Bean的管理110402

4.3---Spring框架之Spring中bean的注入方式---(深入版本)

Spring 之bean的注入

spring ioc容器之Bean实例化和依赖注入

Spring必学手册之注入内部Bean

spring总结之二(Bean作用域依赖注入)