Spring IOC XML Configuration

来源:互联网 发布:网络教育公共课统考 编辑:程序博客网 时间:2024/06/08 03:26
Spring Note

Spring Introdution
引入相关的包

  • ----spring-context-3.2.1.RELEASE.jar
  • ----spring-beans-3.2.1.RELEASE.jar
  • ----spring-core-3.2.1.RELEASE.jar
  • ----spring-expression-3.2.1.RELEASE.jar
  • ----commons-logging-1.1.2.jar

UserDAOImpl类:
package com.edu.hpu.impl;import com.edu.hpu.dao.UserDAO;import com.edu.hpu.model.User;public class UserDAOImpl implements UserDAO {public void save(User user) {System.out.println("user saved!");}}

UserService类:
package com.edu.hpu.service;import com.edu.hpu.dao.UserDAO;import com.edu.hpu.model.User;public class UserService {private UserDAO userDAO;public void save(User user) {userDAO.save(user);}public UserDAO getUserDAO() {return userDAO;}public void setUserDAO(UserDAO userDAO) {this.userDAO = userDAO;}}

beans.xml配置文件:
<?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="u" class="com.edu.hpu.impl.UserDAOImpl"></bean>  <bean id="userService" class="com.edu.hpu.service.UserService">    <property name="userDAO" ref="u" />  </bean></beans>

测试类:
package com.edu.hpu.sevice;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.edu.hpu.model.User;import com.edu.hpu.service.UserService;public class TestUser {@Testpublic void testSave() {ApplicationContext acx = new ClassPathXmlApplicationContext("beans.xml");UserService service = (UserService)acx.getBean("userService");User u = new User();service.save(u);}}

注入的方式大致有三种,上面是Setter Injection
  • ----Setter Injection(重要)
  • ----Constructor Injection(不重要)
  • ----Interface Injection(可以忘记)

下面以Constructor Injection为例子进行注入:
给UserService类添加Constructor方法:
public UserService(UserDAO userDAO) {super();this.userDAO = userDAO;}

在beans.xml里面配置Constructor Injection:
<bean id="userService" class="com.edu.hpu.service.UserService">  <constructor-arg>  <ref bean="u"/>  </constructor-arg></bean>

配置bean的时候,属性名称id也可以使用name代替,但是name里面不能加入特殊字符
<bean name="u" class="com.edu.hpu.impl.UserDAOImpl"></bean>

简单属性的注入
在UserDAOImpl类里面添加属性daoID , daoStatus,并添加相应的getter,setter 方法
在beans.xml里面进行简单属性的配置:
<bean name="u" class="com.edu.hpu.impl.UserDAOImpl"><property name="daoID" value="1" /><property name="daoStatus" value="run" /></bean>

bean的生存范围
the Spring Framework supports five scopes, three of which are available only if you use a web-aware ApplicationContext.
scope : singleton,prototype,request,session,global session
singleton是默认的,而后面的三种,只有使用web-aware Application时才会起作用
<bean id="userService" class="com.hpu.service.UserService" scope="prototype">

集合注入(Collection Injection)
在UserDAOImpl类里面添加三个集合属性,并设定getter与setter方法:
  • private List<String> lists;
  • private Set<String> sets;
  • private Map<String , String> maps;

在beans.xml里面进行集合注入的配置:
<bean name="u" class="com.edu.hpu.impl.UserDAOImpl"><property name="lists"><list><value>1</value><value>2</value></list></property><property name="sets" ><set><value>1</value><value>2</value><value>3</value></set></property><property name="maps"><map><entry key="1" value="1"></entry><entry key="2" value="2"></entry><entry key="3" value="3"></entry></map></property></bean>

自动装配(autowire)
装配方式有四种:no , byName , byType , construtor
<bean id="userService" class="com.hpu.service.UserService" autowire="byName"></bean> <!-- 找出beans.xml里面与该类里面属性名一样的bean进行装配 --><bean id="userService" class="com.hpu.service.UserService" autowire="byName"></bean> <!-- 找出beans.xml里面与该类里面属性的类型一样的bean进行装配 -->

bean的生命周期
在UserService类里面,加入init(),destroy()方法,在beans.xml里面配置如下:
<bean id="userService" class="com.edu.hpu.service.UserService" autowire="byType" init-method="init" destroy-method="destroy"></bean>

注意:init-method , destroy-method 不要和scope=”prototype”一起使用。
本文所述十分简略,若要知道详细操作可参考spring所提供的官方文档。
原创粉丝点击