`

spring 初探

阅读更多

Spring核心设计思想为IOC(Inverse of Control)或依赖注入(Dependence Injection)。

IOC即由容器来控制程序之间的关系,而非传统代码直接操控。

先看第一个spring项目,该项目由上文

http://quicker.iteye.com/blog/666673

项目改写而成,使用spring。

1、spring依赖库的配置:
	* SPRING_HOME/dist/spring.jar
	* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
	* SPRING_HOME/lib/log4j/log4j-1.2.14.jar
	
2、将spring的配置文件拷贝到src下

3、在UserManagerImpl类中,我们定义setter方法,让spring将实例化好的UserDao对象实现给我们

4、让spring管理我们对象的创建,让spring管理对象的依赖,即定义spring配置文件

5、在客户端,通过BeanFactory来访问spring管理的对象,如:

		//通过配置文件初始化工厂
		BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//通过bean工厂取得工厂中的对象
		UserManager userManager = (UserManager)factory.getBean("userManager");


spring的关键点:
	* 让spring来管理你的对象,需要将被管理的对象定义在配置文件中
	* 定义setter方法或构造函数
	
以上两步缺一不可,只有这样定义之后,你的对象之间的依赖关系才能被spring管理

		

程序结构图:

 看具体代码:

package com.lwf.bean;

public class User {

	private String name;
	private int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

 

package com.lwf.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.lwf.bean.User;
import com.lwf.manager.UserManager;

public class Client {
	public static void main(String[] args) {
	
		User user = new User();
		user.setAge(11);
		user.setName("zhangshang");
		
		ApplicationContext ctx = new FileSystemXmlApplicationContext("/src/applicationContext.xml");
		//BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserManager userManager = (UserManager)ctx.getBean("userManager");
		userManager.save(user);
		
		
	}
}

 

package com.lwf.dao;

import com.lwf.bean.User;

public interface UserDao {

	void save(User user);
}

 

package com.lwf.dao;

import com.lwf.bean.User;

public class UserDaoImp4MySql implements UserDao {

	@Override
	public void save(User user) {
		// TODO Auto-generated method stub
		System.out.println("UserDaoImp4MySql");
		System.out.println("UserName :" + user.getName() + "User Age:" + user.getAge());
	}

}

 

package com.lwf.dao;

import com.lwf.bean.User;

public class UserDaoImp4Oracle implements UserDao {

	@Override
	public void save(User user) {
		// TODO Auto-generated method stub
		System.out.println("UserDaoImp4Oracle");
		System.out.println("UserName :" + user.getName() + "User Age:" + user.getAge());
	}

}

 

package com.lwf.manager;

import com.lwf.bean.User;

public interface UserManager {
	 void save(User user);
}

 

package com.lwf.manager;

import com.lwf.bean.User;
import com.lwf.dao.UserDao;

public class UserManagerImp implements UserManager {

	private UserDao userDao ;
	public void save(User user) {
		// TODO Auto-generated method stub
		userDao.save(user);
	}

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

 

 

下面是配置文件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.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


	
	<bean id="userDaoImp4MySql" class="com.lwf.dao.UserDaoImp4MySql"/>
	<bean id="userDaoImp4Oracle" class="com.lwf.dao.UserDaoImp4Oracle"/>
	<bean id="userManager" class="com.lwf.manager.UserManagerImp">
		<property name="userDao" ref="userDaoImp4Oracle"/>
	</bean>

</beans>

 

还有一个log4j.properties文件。

# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
log4j.rootLogger=INFO, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${petstore.root}/WEB-INF/petstore.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

 

 

说明:

配置文件中:

	<bean id="userDaoImp4MySql" class="com.lwf.dao.UserDaoImp4MySql"/>
	<bean id="userDaoImp4Oracle" class="com.lwf.dao.UserDaoImp4Oracle"/>
	<bean id="userManager" class="com.lwf.manager.UserManagerImp">
		<property name="userDao" ref="userDaoImp4Oracle"/>
	</bean>

上面代码实际上spring为UserManagerImp类设置属性userDao。我们的测试代码创建User类,然后再通过代码:

	ApplicationContext ctx = new FileSystemXmlApplicationContext("/src/applicationContext.xml");
		//BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserManager userManager = (UserManager)ctx.getBean("userManager");
		userManager.save(user);

 

实际上代码中对于UserDao我们有两个实现,一个是mysql一个是oracle的实现。那么假如客户要在不同的数据库进行迁移,我们只要将 ref="userDaoImp4Oracle"改为 ref="userDaoImp4MySql"即可。

默认输出结果为:

2010-05-17 16:22:05,593 INFO [org.springframework.context.support.FileSystemXmlApplicationContext] - Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@19fcc69: display name [org.springframework.context.support.FileSystemXmlApplicationContext@19fcc69]; startup date [Mon May 17 16:22:05 CST 2010]; root of context hierarchy
2010-05-17 16:22:05,765 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [D:\workdirlocal\spring_start\src\applicationContext.xml]
2010-05-17 16:22:06,203 INFO [org.springframework.context.support.FileSystemXmlApplicationContext] - Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@19fcc69]: org.springframework.beans.factory.support.DefaultListableBeanFactory@5e5a50
2010-05-17 16:22:06,234 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5e5a50: defining beans [userDaoImp4MySql,userDaoImp4Oracle,userManager]; root of factory hierarchy
UserDaoImp4Oracle
UserName :zhangshangUser Age:11

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics