首先在spring的配置文件中添加
<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
1>通过构造器注入
<bean id="userBean" class="cn.anzhi.spring.bean.UserBean" scope="singleton"
<!-- 通过构造器注入 index:参数的序号,从0开始,从左到右value:注入的参数值,此值为基本类型,string、int ref:可以引用一个bean实例
name:构造 参数名称,如果使用name指定注入的参数(构造 方法的形参)名称,可以不使用index ,type:参数类型 -->
<!-- <constructor-arg index="0" value="张三" type="java.lang.String"></constructor-arg> -->
<!-- <constructor-arg index="1" value="zhangsan" type="java.lang.String"></constructor-arg> -->
<constructor-arg name="usercode" value="zhangsan" type="java.lang.String"></constructor-arg>
<constructor-arg name="username" value="zhangsan" type="java.lang.String"></constructor-arg>
</bean>
2> 通过构造器和引入注入,name是构造器参数名称 public UserBean(User user)
<bean class="cn.anzhi.spring.bean.UserBean">
<constructor-arg name="user" type="cn.anzhi.spring.bean.User" ref="user"></constructor-arg>
</bean>
3>set 方法注入,会找到方法的参数注入到对应的字段中 setUsercode(String usercode)
<!-- name:spring拼接一个set方法名称,通过反射去调用 value:注入一些基本类型、简单,string,int ref:引用一个bean实例 -->
<bean id="userBean3" class="cn.anzhi.spring.bean.UserBean">
<property name="user" ref="user"></property>
<property name="username" value="李四"></property>
<property name="usercode" value="lisi"></property>
</bean>
4>集合数据注入
<!-- 集合数据注入 name:spring拼接一个set方法名称,通过反射去调用 value-->
<!-- 通过p命名空间注入 使用方法: p:<属性名>="xxx" 引入常量值 p:<属性名>-ref="xxx" 引用其它Bean对象 -->
<bean id="userBean6" class="cn.anzhi.spring.bean.UserBean" p:user-ref="user" p:username="李四" p:usercode="lisi">
<property name="listString">
<list>
<value>张三</value>
<value>李四</value>
</list>
</property>
<!-- List<User> userList -->
<property name="userList">
<list>
<!-- 向list<user>中注入user对象 -->
<ref bean="user"/>
<ref bean="user"/>
</list>
</property>
<property name="setString">
<set>
<value>张三</value>
<value>李四</value>
<value>张三</value>
<value>李四</value>
</set>
</property>
<property name="map">
<map>
<entry key="username" value="张三" ></entry>
<entry key="user" value-ref="user"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="username">张三</prop>
<prop key="usercode">李四</prop>
</props>
</property>
</bean>
5>util标签的使用
<bean id="userBean1" class="cn.anzhi.spring.bean.UserBean1"><property name="listString" ref="listString"></property>
<property name="userList" ref="userList"></property>
<property name="map" ref="map"></property>
<property name="setString" ref="setString"></property>
<property name="properties" ref="properties"></property>
<property name="properties1" ref="properties1"></property>
<property name="constantValue" ref="constantValue"></property>
</bean>
<util:list id="listString">
<value>张三</value>
<value>李四</value>
</util:list>
<util:list id="userList" list-class="java.util.ArrayList" value-type="cn.anzhi.spring.bean.User">
<ref bean="user"></ref>
<ref bean="user"></ref>
</util:list>
<util:map id="map" map-class="java.util.HashMap" key-type="java.lang.String" value-type="java.lang.String">
<entry key="username" value="张三" ></entry>
<entry key="usercode" value="zhangsan"></entry>
</util:map>
<util:set id="setString" set-class="java.util.HashSet" value-type="java.lang.String">
<value>set1</value>
<value>set2</value>
</util:set>
<util:properties id="properties">
<prop key="properties1">properties1</prop>
<prop key="properties2">properties2</prop>
</util:properties>
<!--<util:properties/>元素 "classpath:"表明,将从类路径上查找并装载xxx属性文件.-->
<util:properties id="properties1" location="classpath:properties.properties"></util:properties>
<!-- 定义常量 -->
<util:constant id="constantValue" static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
<util:constant id="maxValue" static-field="java.lang.Integer.MAX_VALUE"/>
<util:map id="serviceSendMessageMap" map-class="java.util.HashMap" key-type="java.lang.String" value-type="java.lang.String">
<entry key="service.appKey" value="142605894293bjc9VR9P3Xqv7jFTgh" />
<entry key="service.secret" value="eeUu5p6XElQbYGM26iCIOmo2" />
<entry key="service.url" value="http://dev23.anzhi.com" />
</util:map>
6> bean的初始化方法和销毁方法
<bean id="user" class="cn.itcast.spring.bean.User" init-method="init" destroy-method="teardown"> </bean>
实体类:
public class UserBean {
// 集合属性注入
private List<String> listString;
private List<User> userList;
private Set<String> setString;
private Map<String, Object> map;
private Properties properties;
private String username;
private String usercode;
private User user;
//省略对应的set get方法及构造方法
}
测试:
@Test
public void userBean() {
//注意: getBean是通过id获取bean 不是name
ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "beanDI.xml" );
UserBean userBean = (UserBean) applicationContext.getBean( "userBean" ); //通过id获取bean
System.err.println( userBean.toString() );
}
注解实例化bean 依赖注入:
----------------------------------------------------------------------------------------------------------------------------
@Component("itemService") 实体类上配置的是id
web层:@Controller (表示一个控制器)
业务层:@Service (表示一个业务bean)
持久层:@Respository(表示一个持久层的dao)
这三个注解是为了让标注类本身的用途清晰,这个三注解可以使用@Component代替,不过推荐使用这三个注解标注Action、Service、Dao