Spring2.0(一)第一个Spring程序、IoC的应用

 

Spring的IoC是采用DI来实现的。

1. 新建工程,需要的jar包:

    * spring-framework-2.0\dist\spring.jar (Spring2.0的核心包);

    * spring-framework-2.0\lib\jakarta-commons\commons-logging (日志记录);

    * spring-framework-2.0\lib\log4j\log4j-1.2.14 (log4j);

2. 从spring-framework-2.0\samples\jpetstore\war\WEB-INF下拷贝applicationContext.xml到工

    程src/目录下,删除注释等无关的内容,只保留beans根节点:

<?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:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


</beans>

 3.加入log4j文件,可以用Hibernian的,也可以从Spring的示例程序spring-framework-2.0

    \samples\jpetstore\war\WEB-INF\下找到log4j.properties(两种log4j文件并不一样)。

 4.在applicationContext.xml中描述依赖关系,默认情况下载里头敲任何节点都不给提示的,可以载eclipse中加入对该

    xml的提示帮助文件spring-beans-2.0.xsd(不要用 .dtd,现在xsd的用的多),该文件可以从spring-framework-

    2.0\dist\resources\下找到,最好放在eclipse的安装目录下,防止文件移动还要重新配置。

    具体的做法:windows > perference > MyEclipse Enterprise Workbench > XML >XML Catalog > Add..

    key Type选择URI,Location 选择spring-beans-2.0.xsd的路径,这时Key会自动生成路径,再key路径的末尾

    追加/spring-beans-2.0.xsd ,最后再把key Type 选择为 Schema Location 点击OK即完成了该xsd约束文件

    的添加,重开一下applicationContext.xml,约束提示功能已经生效了。

5.做个示例:

   工程的目录结构如下:

   目录

 

package com.wyx.sring.dao;

public interface UserDao {
	public void save(String name, String passwd);
}

 

package com.wyx.sring.dao;

public class UserDao4MySqlImpl implements UserDao{
	public void save(String name, String passwd) {
		System.out.println("-----------UserDao4MySqlImpl.save()--------------");	
	}
}

 

 

package com.wyx.sring.dao;

public class UserDao4OracleImpl implements UserDao{
	public void save(String name, String passwd) {
		System.out.println("-----------UserDao4OracleImpl.save()--------------");	
	}
}

 

 

package com.wyx.sring.manager;

public interface UserMangager {
	public void save(String name, String passwd);
}

 

    必须在 UserManagerImpl中提供构造函数或setter方法(setter在后面介绍),Spring才能将实例化好的UserDao实

    现注入给我们

package com.wyx.sring.manager;

import com.wyx.sring.dao.UserDao;

public class UserManagerImpl implements UserMangager {
	private UserDao userDao;
	public UserManagerImpl(UserDao userDao) {
		this.userDao = userDao;
	}

	public void save(String name, String passwd) {
		userDao.save(name, passwd);
	}

}

 

<?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:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	
	<bean id="userDao4MySqlImpl" class="com.wyx.sring.dao.UserDao4MySqlImpl"/>
	<bean id="userDao4OracleImpl" class="com.wyx.sring.dao.UserDao4OracleImpl"/>
	
	<bean id="userManagerImpl" class="com.wyx.sring.manager.UserManagerImpl">
		<!--  使用构造子注入时,则使用constructor-arg子标签,来指定构造函数的参数 -->
		<constructor-arg ref="userDao4OracleImpl"/>
	</bean>
</beans>

 

   这样再Client中调用不同的UserManager实现只需要修改applicationContext.xml下constructor-arg 标签的ref 

   值即可,Client如下所示:

package com.wyx.sring.client;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wyx.sring.dao.UserDao;
import com.wyx.sring.dao.UserDao4MySqlImpl;
import com.wyx.sring.manager.UserManagerImpl;

public class CLient {


	public static void main(String[] args) {
//		UserManagerImpl userManagerImpl = new UserManagerImpl(new UserDaoImpl());
//		userManagerImpl.save("张三", "123");
		
		//给客户端拿到的就是manager层,manager不能再用上面注释掉的部分一样new出来,我们现在
		//要从IoC容器中拿。
		//首先要装载配置文件,Spring提供了BeanFactory抽象工厂
		//找到classpath下的applicationContext.xml文件
		 BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
		//根据Id告诉配置文件,需要哪个类,Spring给你这个类的时候,
		//已经把这个类所有依赖的所有对象实例化好,并且注入给你
		 UserManagerImpl userMageImpl = (UserManagerImpl)factory.getBean("userManagerImpl");
		 userMageImpl.save("张三", "123");
	}
}

 

 

   applicationContext.xml写法定义:

         配置文件的根元素是beans ,每个组件使用bean 元素来定义,bean元素可以有许多属性,其中有两个是必须的:id  class 。id表示组件的默认名称class表示组件包名下的完整路径。

  注意:

  (1) 并不是所有的对象都要配置到IoC容器中,我们只需要配置变化的业务层对象。

  (2) UserManagerImpl中提供构造函数,spring将实例化好的UserDao实现注入给我们。

  (3) 让Spring管理对象创建和依赖关系,必须在Spring配置文件applicationContext.xml中定义。

 

使用setter方法实现依赖注入:

1.修改UserManagerImpl:

package com.wyx.sring.manager;

import com.wyx.sring.dao.UserDao;

public class UserManagerImpl implements UserMangager {
	private UserDao userDao;
	/*public UserManagerImpl(UserDao userDao) {
		this.userDao = userDao;
	}*/

	public void save(String name, String passwd) {
		userDao.save(name, passwd);
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
}

 2.修改applicationContext.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"
	     xmlns:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	
	<bean id="userDao4MySqlImpl" class="com.wyx.sring.dao.UserDao4MySqlImpl"/>
	<bean id="userDao4OracleImpl" class="com.wyx.sring.dao.UserDao4OracleImpl"/>
	
	<bean id="userManagerImpl" class="com.wyx.sring.manager.UserManagerImpl">
		<property name="userDao"> <ref local="userDao4OracleImpl"/></property>
	</bean>
</beans>

    运行Clinet.java,查看输出结果。注意:property的那么属性应该指定UserManagerImpl的set方法对应的名字

    (详查Java Bean规范)。

 

   有关applicationContext.xml的更详细配置,参考:

 http://hi.baidu.com/wsgongbing/blog/item/0412863333484cfb1a4cffe7.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值