`
chinrui
  • 浏览: 93906 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Spring IOC XML Configuration

阅读更多
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 {

	@Test
	public 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所提供的官方文档。
分享到:
评论

相关推荐

    spring-javaconfig-reference

    Spring (i.e.: have never used Spring's XML configuration language, it is suggested that you first read Chapter 3. IoC of the Core Spring reference manual documentation. You will also notice that this ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 ...

    Spring.net框架

    在Step3到Step5的例子中,我们将利用Spring.net提供的Ioc框架,轻松完 成解耦以及系统改造等工作。 一、类之间的依赖 我们的第一个例子主要用于说明程序的基本构造,并且作为一个反面典型,引出为什么要解耦,以及...

    Spring 2.0 开发参考手册

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点...

    Spring.html

    IOC 控制反转:把创建对象的权利交给Spring 创建对象 1.无参构造 2.静态工厂 3.实例工厂 管理对象 对象关系DI 构造器注入 set注入 生命周期 scope:prototype/singleton init-method ...

    spring.net中文手册在线版

    20.7. XML Schema for configuration 20.8.参考资源 第二十一章. .NET企业服务 21.1.简介 21.2.服务组件 21.3.服务端 21.4.客户端 第二十二章. Web服务 22.1.服务端 22.1.1.消除对.asmx文件的依赖 22.1.2.向web服务...

    Spring 基于java的容器配置.docx

    @Bean注解用于指示方法实例化、配置和初始化由Spring IoC容器管理的新对象。对于那些熟悉Spring的 XML配置的人来说,@Bean注解的作用与元素相同。你可以对任何Spring @Component使用@Bean注解的方法,但是,它们最...

    springmybatis

    2. Configuration.xml 里面 的&lt;mapper resource="com/yihaomen/mybatis/model/User.xml"/&gt;是包含要映射的类的xml配置文件。 3. 在User.xml 文件里面 主要是定义各种SQL 语句,以及这些语句的参数,以及要返回的类型...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (1)

    12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1 系统概述 13.2 需求分析 13.2.1 系统用例图 ...

    Spring中文帮助文档

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    spring chm文档

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点...

    Spring API

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (3)

    12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1 系统概述 13.2 需求分析 13.2.1 系统用例图 ...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (2)

    12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1 系统概述 13.2 需求分析 13.2.1 系统用例图 ...

    第24次课-1 Spring与Hibernate的整合

    Spring提供了很多IoC特性的支持,方便处理大部分典型的Hibernate整合问题。 如:SessionFactory的注入、HibernateTemplate的简化操作、DAO的支持等。 为了更好地与持久层框架整合,Spring还提供了统一的异常处理体系...

    spring-framework-reference-4.1.2

    4.1. Introduction to the Spring IoC container and beans .............................................. 22 4.2. Container overview .........................................................................

    Jetty中文手册

    Jetty XML语法(Syntax)–Jetty IOC Configuration Jetty XML用法–Using and Combining Jetty Configurations 配置文件 jetty.xml–Server configuration jetty-web.xml–Web App configuration jetty-env.xml–...

    ActionScript Parsley 3 帮助文档

    The core of the framework (the IOC Container and the Messaging Subsystem) does not depend on the Flex SDK at all, but there are several additional modules that are specifically designed for Flex, ...

    spring3.1中文参考文档

    2.5.3 控制反转(IoC)容器 ................................................................................................................ 25 2.5.3.1 基于Java的bean元数据.................................

    spring-framework-reference4.1.4

    4.1. Introduction to the Spring IoC container and beans .............................................. 22 4.2. Container overview .........................................................................

Global site tag (gtag.js) - Google Analytics