Spring温习(1)--最基础的示例

从现在开始,我将从Spring为起点,逐步复习几大框架各方面的知识,以便今后查看使用

第一各Spring示例

必须包:spring-framework-2.5.6\dist\spring.jar

         spring-framework-2.5.6\lib\jakarta-commons\common-logging.jar

为了方便测试还需要:spring-framework-2.5.6\lib\junit\junit4.4.jar

第一步,先在spring资源包找到:spring-framework-2.5.6\samples\jpetstore\attributes\WEB-INF\applictionContext.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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> </beans>

 UserDAO.java

package com.test.domain; public interface UserDAO { void say(); }

 UserDAOImpl.java

package com.test.domain; public class UserDAOImpl implements UserDAO { @Override public void say() { System.out.println("i can speak"); } }

applictionContext.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="userDAO" class="com.test.domain.UserDAOImpl"/> </beans>

 测试类

 

package com.test.domain; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; public class MyTest { @Test public void testUser(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); UserDAO dao=(UserDAO)context.getBean("userDAO"); dao.say(); } }

 测试结果:i can speak

 

Spring加载XML配置文件的方式

 spring 中加载xml配置文件的方式,好像有3种, xml是最常见的spring 应用系统配置源。Spring中的几种容器都支持使用xml装配bean,包括:
    XmlBeanFactory ,
    ClassPathXmlApplicationContext ,
    FileSystemXmlApplicationContext ,
    XmlWebApplicationContext

一、XmlBeanFactory 引用资源
    Resource resource = new ClassPathResource("appcontext.xml");
    BeanFactory factory = new XmlBeanFactory(resource);
二、ClassPathXmlApplicationContext  编译路径
    1)ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:appcontext.xml");
    2)ApplicationContext factory=new ClassPathXmlApplicationContext("appcontext.xml");   // src目录下的
    3)ApplicationContext factory=new ClassPathXmlApplicationContext("conf/appcontext.xml");   // src/conf 目录下的
    4)ApplicationContext factory=new ClassPathXmlApplicationContext("file:G:/Test/src/appcontext.xml");

     5)String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
      ApplicationContext ctx = new ClassPathXmlApplication(locations);

三 、 用文件系统的路径
   1) ApplicationContext factory=new FileSystemXmlApplicationContext("src/appcontext.xml");
    //使用了  classpath:  前缀,作为标志,  这样,FileSystemXmlApplicationContext 也能够读入classpath下的相对路径
    2)ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:appcontext.xml");
    3)ApplicationContext factory=new FileSystemXmlApplicationContext("file:G:/Test/src/appcontext.xml");
    4)ApplicationContext factory=new FileSystemXmlApplicationContext("G:/Test/src/appcontext.xml");

    5)String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
        ApplicationContext ctx = new FileSystemXmlApplicationContext(locations );

四、XmlWebApplicationContext   是专为Web工程定制的。
    ServletContext servletContext = request.getSession().getServletContext();
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext );

注:其中FileSystemXmlApplicationContext和ClassPathXmlApplicationContext与BeanFactory的xml文件定位方式一样是基于路径的

 

Spring的实例化Bean有三种方式:

 使用类构造器直接实例化

 使用静态工厂的方法实例化

 使用实例工厂方法实例化

具体对应配置如

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <!--Spring的实例化Bean有三种方式:--> <!-- 使用类构造器直接实例化 --> <bean id="userDAO" class="com.test.domain.UserDAOImpl"/> <!-- 使用静态工厂的方法实例化 --> <bean id="userDAO1" class="com.test.domain.BeanFactory" factory-method="UserDAOService" /> <!-- 使用实例工厂方法实例化 --> <bean id="factory" class="com.test.domain.BeanFactory" /> <bean id="userDAO2" factory-bean="factory" factory-method="getUserDAOService" /> </beans> 

 

 BeanFactory.java

package com.test.domain; public class BeanFactory { //使用静态工厂的方法实例化使用 public static UserDAO UserDAOService() { return new UserDAOImpl(); } public UserDAO getUserDAOService() { return new UserDAOImpl(); } } 

 测试类

package com.test.domain; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; public class MyTest { @Test public void testUser(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); UserDAO dao=(UserDAO)context.getBean("userDAO"); dao.say(); UserDAO dao2=(UserDAO)context.getBean("userDAO2"); dao2.say(); UserDAO dao3=(UserDAO)context.getBean("userDAO3"); dao3.say(); } } 

 

测试结果

i can speak

i can speak

i can speak



PS:Spring的配置文件引入方式

1)传统配置多个文件,applicationContext-xx.xml,applicationContext-yy.xml,applicatonContext-zz.xml

   那么在web.xml中引入这么多文件可以是这样写

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:/META-INF/spring/applicationContext-*.xml</param-value>
 </context-param>

 2)第二种方式,也是上面那么三个配置文件,那么我们可以将-yy.xml和-zz.xml都配置在-xx.xml中去,然后再在web.xml中单独配置-xx.xml就可以

     applicationContext-xx.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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<import resource="classpath:/META-INF/spring/applicationContext-yy.xml" />
	
	<import resource="classpath:/META-INF/spring/applicationContext-zz.xml" />

</beans>

 那么在web.xml中应该是

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:/META-INF/spring/applicationContext-xx.xml</param-value>
 </context-param>
 


 

你可能感兴趣的:(DAO,spring,xml,bean,Web)