1.构造注入

1.创建实体类User

2.创建dao层接口及两个实现类user1daoimpl,user2daoimpl

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

3.创建业务层接口及实现类userbizImpl

 Spring 依赖注入 随笔 第1张

 

  1. 为实现类编写构造注入。

 Spring 依赖注入 随笔 第2张

 

  1. 编写文件头

<?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">

  1. 在配置文件中调用。

 Spring 依赖注入 随笔 第3张

6.测试构造注入。

 

p命名空间注入

 

1.加入文件头

 

<?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">

 

2.给实体注入属性。

 

<bean id="user" class="com.bdqn.entity.User"

 

       p:id="2" p:name="随便一个名字" p:password="自定义密码"

 

      >

 

</bean>

 

3.为业务层注入组件

 

<bean id="biz" class="com.bdqn.biz.impl.UserBizImpl" p:dao-ref="dao1">

 

        

 

</bean>

 

注意:注入组件要用p:dao-ref

 

其他注入

<?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-3.2.xsd">
<bean id="en" class="entity.TestEntity">
<!-- 使用<![CDATA[]]>标记处理XML特 殊字符 -->
<property name="specialCharacter1">
<value><![CDATA[P&G]]></value>
</property>
<!-- 把XML特殊字符替换为实体引用 -->
<property name="specialCharacter2">
<value>P&amp;G</value>
</property>
<!-- 定义内部Bean -->
<property name="innerBean">
<bean class="entity.User">
<property name="name">
<value>Mr. Inner</value>
</property>
</bean>
</property>
<!-- 注入List类型 -->
<property name="list">
<list>
<!-- 定义List中的元素 -->
<value>足球</value>
<value>篮球</value>
</list>
</property>
<!-- 注入数组类型 -->
<property name="array">
<list>
<!-- 定义数组中的元素 -->
<value>足球</value>
<value>篮球</value>
</list>
</property>
<!-- 注入Set类型 -->
<property name="set">
<set>
<!-- 定义Set或数组中的元素 -->
<value>足球</value>
<value>篮球</value>
</set>
</property>
<!-- 注入Map类型 -->
<property name="map">
<map>
<!-- 定义Map中的键值对 -->
<entry>
<key>
<value>football</value>
</key>
<value>足球</value>
</entry>
<entry>
<key>
<value>basketball</value>
</key>
<value>篮球</value>
</entry>
</map>
</property>
<!-- 注入Properties类型 -->
<property name="props">
<props>
<!-- 定义Properties中的键值对 -->
<prop key="football">足球</prop>
<prop key="basketball">篮球</prop>
</props>
</property>
<!-- 注入空字符串值 -->
<property name="emptyValue">
<value></value>
</property>
<!-- 注入null值 -->
<property name="nullValue">
<null/>
</property>
</bean>
</beans>

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄