实例:
1。环境搭建
导入spring需要的包,spring核心jar包、commons-logging.jar(暂时先用这两个玩玩),因为要写单元测试,导入juint4相关jar包。
2。eclipse新建springTest工程。导入相关jar包,在src目录下新建beans.xml.
新建接口UserDao及实现类UserDaoImpl.代码如下:
接口:
package com.neusoft.main.dao;
/**
* @author <a href="mailto:hongchq@neusoft.com">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午10:25:39
*/
public interface UserDao {
void save();
}
实现类:
package com.neusoft.main.dao.impl;
import com.neusoft.main.dao.UserDao;
/**
* @author <a href="mailto:hongchq@neusoft.com">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午10:26:25
*/
public class UserDaoImpl implements UserDao {
/* (non-Javadoc)
* @see com.neusoft.main.dao.UserDao#save()
*/
public void save() {
System.out.println("execute --save()-- method.");
}
}
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" 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="userDaoImpl" class="com.neusoft.main.dao.impl.UserDaoImpl"/>
</beans>
编写单元测试类:代码如下:
package com.neusoft.java.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.neusoft.main.dao.UserDao;
/**
* @author <a href="mailto:hongchq@neusoft.com">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午10:19:30
*/
public class SpringEnvTest{
@Test
public void testEnv() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void base() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserDao userDao = (UserDao) ctx.getBean("userDaoImpl");
userDao.save();
}
}
运行单元测试:结果如下:
2010-6-1 10:31:41 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1855af5: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1855af5]; startup date [Tue Jun 01 10:31:41 CST 2010]; root of context hierarchy
2010-6-1 10:31:41 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-1 10:31:41 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1855af5]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8e059
2010-6-1 10:31:41 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8e059: defining beans [userDaoImpl]; root of factory hierarchy
2010-6-1 10:31:41 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@d08633: display name [org.springframework.context.support.ClassPathXmlApplicationContext@d08633]; startup date [Tue Jun 01 10:31:41 CST 2010]; root of context hierarchy
2010-6-1 10:31:41 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-1 10:31:41 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@d08633]: org.springframework.beans.factory.support.DefaultListableBeanFactory@704baa
2010-6-1 10:31:41 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@704baa: defining beans [userDaoImpl]; root of factory hierarchy
execute --save()-- method.
上面使用的是类的直接生成方式,其次还有bean静态工厂和bean实例工厂两种方式。
bean静态工厂beans.xml的配置如下:
<bean id="userDaoImpl2" class="com.neusoft.main.dao.impl.UserDaoImplFactory" factory-method="getUserDaoImpl" />
新建UserDaoImplFactory类:
package com.neusoft.main.dao.impl;
/**
* @author <a href="mailto:hongchq@neusoft.com">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午10:43:37
*/
public class UserDaoImplFactory {
public static UserDaoImpl getUserDaoImpl() {
System.out.println("factory born bean.");
return new UserDaoImpl();
}
}
在单元测试类中加入如下测试方法
@Test
public void baseFactory() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserDao userDao = (UserDao) ctx.getBean("userDaoImpl2");
userDao.save();
}
运行结果:
2010-6-1 10:48:09 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@169e11: display name [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]; startup date [Tue Jun 01 10:48:09 CST 2010]; root of context hierarchy
2010-6-1 10:48:10 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-1 10:48:10 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]: org.springframework.beans.factory.support.DefaultListableBeanFactory@910040
2010-6-1 10:48:10 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@910040: defining beans [userDaoImpl,userDaoImpl2]; root of factory hierarchy
factory born bean.
execute --save()-- method.
bean实例工厂配置:
beans.xml配置:
(为了观察效果直观,将userDaoImpl2的bean注释掉,以免测试结果出现混乱)。
<?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="userDaoImpl" class="com.neusoft.main.dao.impl.UserDaoImpl"/>
<!-- <bean id="userDaoImpl2" class="com.neusoft.main.dao.impl.UserDaoImplFactory" factory-method="getUserDaoImpl" /> -->
<bean id="factory" class="com.neusoft.main.dao.impl.UserDaoImplFactory2"/>
<bean id="userDaoImpl3" factory-bean="factory" factory-method="getUserDaoImpl" />
</beans>
java类代码如下:
package com.neusoft.main.dao.impl;
/**
* @author <a href="mailto:hongchq@neusoft.com">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午10:50:57
*/
public class UserDaoImplFactory2 {
public UserDaoImpl getUserDaoImpl () {
System.out.println("factory2 born bean.");
return new UserDaoImpl();
}
}
单元测试代码如下:
package com.neusoft.java.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.neusoft.main.dao.UserDao;
/**
* @author <a href="mailto:hongchq@neusoft.com">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午10:19:30
*/
public class SpringEnvTest{
@Test
public void testEnv() {
//ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void base() {
// ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
// UserDao userDao = (UserDao) ctx.getBean("userDaoImpl");
// userDao.save();
}
@Test
public void baseFactory() {
// ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
// UserDao userDao = (UserDao) ctx.getBean("userDaoImpl2");
// userDao.save();
}
@Test
public void baseFactory2() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserDao userDao = (UserDao) ctx.getBean("userDaoImpl3");
userDao.save();
}
}
执行结果:
2010-6-1 10:53:54 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@169e11: display name [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]; startup date [Tue Jun 01 10:53:54 CST 2010]; root of context hierarchy
2010-6-1 10:53:54 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-1 10:53:54 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]: org.springframework.beans.factory.support.DefaultListableBeanFactory@18088c0
2010-6-1 10:53:54 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18088c0: defining beans [userDaoImpl,factory,userDaoImpl3]; root of factory hierarchy
factory2 born bean.
execute --save()-- method.
1。环境搭建
导入spring需要的包,spring核心jar包、commons-logging.jar(暂时先用这两个玩玩),因为要写单元测试,导入juint4相关jar包。
2。eclipse新建springTest工程。导入相关jar包,在src目录下新建beans.xml.
新建接口UserDao及实现类UserDaoImpl.代码如下:
接口:
package com.neusoft.main.dao;
/**
* @author <a href="mailto:hongchq@neusoft.com">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午10:25:39
*/
public interface UserDao {
void save();
}
实现类:
package com.neusoft.main.dao.impl;
import com.neusoft.main.dao.UserDao;
/**
* @author <a href="mailto:hongchq@neusoft.com">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午10:26:25
*/
public class UserDaoImpl implements UserDao {
/* (non-Javadoc)
* @see com.neusoft.main.dao.UserDao#save()
*/
public void save() {
System.out.println("execute --save()-- method.");
}
}
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" 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="userDaoImpl" class="com.neusoft.main.dao.impl.UserDaoImpl"/>
</beans>
编写单元测试类:代码如下:
package com.neusoft.java.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.neusoft.main.dao.UserDao;
/**
* @author <a href="mailto:hongchq@neusoft.com">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午10:19:30
*/
public class SpringEnvTest{
@Test
public void testEnv() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void base() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserDao userDao = (UserDao) ctx.getBean("userDaoImpl");
userDao.save();
}
}
运行单元测试:结果如下:
2010-6-1 10:31:41 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1855af5: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1855af5]; startup date [Tue Jun 01 10:31:41 CST 2010]; root of context hierarchy
2010-6-1 10:31:41 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-1 10:31:41 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1855af5]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8e059
2010-6-1 10:31:41 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8e059: defining beans [userDaoImpl]; root of factory hierarchy
2010-6-1 10:31:41 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@d08633: display name [org.springframework.context.support.ClassPathXmlApplicationContext@d08633]; startup date [Tue Jun 01 10:31:41 CST 2010]; root of context hierarchy
2010-6-1 10:31:41 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-1 10:31:41 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@d08633]: org.springframework.beans.factory.support.DefaultListableBeanFactory@704baa
2010-6-1 10:31:41 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@704baa: defining beans [userDaoImpl]; root of factory hierarchy
execute --save()-- method.
上面使用的是类的直接生成方式,其次还有bean静态工厂和bean实例工厂两种方式。
bean静态工厂beans.xml的配置如下:
<bean id="userDaoImpl2" class="com.neusoft.main.dao.impl.UserDaoImplFactory" factory-method="getUserDaoImpl" />
新建UserDaoImplFactory类:
package com.neusoft.main.dao.impl;
/**
* @author <a href="mailto:hongchq@neusoft.com">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午10:43:37
*/
public class UserDaoImplFactory {
public static UserDaoImpl getUserDaoImpl() {
System.out.println("factory born bean.");
return new UserDaoImpl();
}
}
在单元测试类中加入如下测试方法
@Test
public void baseFactory() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserDao userDao = (UserDao) ctx.getBean("userDaoImpl2");
userDao.save();
}
运行结果:
2010-6-1 10:48:09 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@169e11: display name [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]; startup date [Tue Jun 01 10:48:09 CST 2010]; root of context hierarchy
2010-6-1 10:48:10 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-1 10:48:10 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]: org.springframework.beans.factory.support.DefaultListableBeanFactory@910040
2010-6-1 10:48:10 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@910040: defining beans [userDaoImpl,userDaoImpl2]; root of factory hierarchy
factory born bean.
execute --save()-- method.
bean实例工厂配置:
beans.xml配置:
(为了观察效果直观,将userDaoImpl2的bean注释掉,以免测试结果出现混乱)。
<?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="userDaoImpl" class="com.neusoft.main.dao.impl.UserDaoImpl"/>
<!-- <bean id="userDaoImpl2" class="com.neusoft.main.dao.impl.UserDaoImplFactory" factory-method="getUserDaoImpl" /> -->
<bean id="factory" class="com.neusoft.main.dao.impl.UserDaoImplFactory2"/>
<bean id="userDaoImpl3" factory-bean="factory" factory-method="getUserDaoImpl" />
</beans>
java类代码如下:
package com.neusoft.main.dao.impl;
/**
* @author <a href="mailto:hongchq@neusoft.com">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午10:50:57
*/
public class UserDaoImplFactory2 {
public UserDaoImpl getUserDaoImpl () {
System.out.println("factory2 born bean.");
return new UserDaoImpl();
}
}
单元测试代码如下:
package com.neusoft.java.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.neusoft.main.dao.UserDao;
/**
* @author <a href="mailto:hongchq@neusoft.com">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午10:19:30
*/
public class SpringEnvTest{
@Test
public void testEnv() {
//ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void base() {
// ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
// UserDao userDao = (UserDao) ctx.getBean("userDaoImpl");
// userDao.save();
}
@Test
public void baseFactory() {
// ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
// UserDao userDao = (UserDao) ctx.getBean("userDaoImpl2");
// userDao.save();
}
@Test
public void baseFactory2() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserDao userDao = (UserDao) ctx.getBean("userDaoImpl3");
userDao.save();
}
}
执行结果:
2010-6-1 10:53:54 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@169e11: display name [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]; startup date [Tue Jun 01 10:53:54 CST 2010]; root of context hierarchy
2010-6-1 10:53:54 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-1 10:53:54 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]: org.springframework.beans.factory.support.DefaultListableBeanFactory@18088c0
2010-6-1 10:53:54 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18088c0: defining beans [userDaoImpl,factory,userDaoImpl3]; root of factory hierarchy
factory2 born bean.
execute --save()-- method.