Spring--最基础的示例

Spring温习(1)--最基础的示例
文章分类:Java编程

从现在开始,我将从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

找到后将多余的删除,留下最基本的
Java代码

   1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?> 
   2. <beans xmlns="http://www.springframework.org/schema/beans" 
   3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   4.     xmlns:context="http://www.springframework.org/schema/context" 
   5.     xmlns:tx="http://www.springframework.org/schema/tx" 
   6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
   7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 
   8.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
   9.  
  10. </beans></span> 

<?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
Java代码

   1. <span style="font-size: medium;">package com.test.domain; 
   2.  
   3. public interface UserDAO { 
   4.   void say(); 
   5. }</span><span> 
   6. </span> 

package com.test.domain;

public interface UserDAO {
  void say();
}

UserDAOImpl.java
Java代码

   1. <span style="font-size: medium;">package com.test.domain; 
   2.  
   3. public class UserDAOImpl implements UserDAO { 
   4.  
   5.     @Override 
   6.     public void say() { 
   7.       System.out.println("i can speak"); 
   8.     } 
   9.  
  10. }</span><span> 
  11. </span> 

package com.test.domain;

public class UserDAOImpl implements UserDAO {

@Override
public void say() {
      System.out.println("i can speak");
}

}

applictionContext.xml
Xml代码

   1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?> 
   2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 
   3.  
   4. <beans> 
   5. <bean  id="userDAO" class="com.test.domain.UserDAOImpl"/> 
   6. </beans></span><span> 
   7. </span> 

<?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>

测试类


Java代码

   1. <span style="font-size: medium;">package com.test.domain; 
   2.  
   3. import org.junit.Test; 
   4. import org.springframework.context.ApplicationContext; 
   5. import org.springframework.context.support.ClassPathXmlApplicationContext; 
   6. import org.springframework.web.context.WebApplicationContext; 
   7. import org.springframework.web.context.support.WebApplicationContextUtils; 
   8.  
   9. public class MyTest { 
  10.      
  11.     @Test 
  12.     public void testUser(){ 
  13.              ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); 
  14.              UserDAO dao=(UserDAO)context.getBean("userDAO"); 
  15.              dao.say(); 
  16.     } 
  17.  
  18. }</span><span> 
  19. </span> 

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代码

   1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?> 
   2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 
   3.  
   4. <beans> 
   5. <!--Spring的实例化Bean有三种方式:--> 
   6.         <!-- 使用类构造器直接实例化 -->      
   7.         <bean  id="userDAO" class="com.test.domain.UserDAOImpl"/> 
   8.         <!-- 使用静态工厂的方法实例化 -->   
   9.         <bean id="userDAO1" class="com.test.domain.BeanFactory" factory-method="UserDAOService" />   
  10.         <!-- 使用实例工厂方法实例化 -->   
  11.         <bean id="factory" class="com.test.domain.BeanFactory" />   
  12.         <bean id="userDAO2" factory-bean="factory" factory-method="getUserDAOService" />  
  13. </beans> 
  14. </span> 

<?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
Java代码

   1. <span style="font-size: medium;">package com.test.domain; 
   2.  
   3. public class BeanFactory { 
   4.      
   5.         //使用静态工厂的方法实例化使用    
   6.         public static UserDAO UserDAOService()    
   7.         {    
   8.             return new UserDAOImpl();    
   9.         }    
  10.             
  11.         public UserDAO getUserDAOService()    
  12.         {    
  13.             return new UserDAOImpl();    
  14.         }    
  15.      
  16. }</span><span style="font-size: medium;"> 
  17. </span> 

package com.test.domain;

public class BeanFactory {

    //使用静态工厂的方法实例化使用  
    public static UserDAO UserDAOService()  
    {  
        return new UserDAOImpl();  
    }  
      
    public UserDAO getUserDAOService()  
    {  
        return new UserDAOImpl();  
    }  

}

测试类
Java代码

   1. <span style="font-size: medium;">package com.test.domain; 
   2.  
   3. import org.junit.Test; 
   4. import org.springframework.context.ApplicationContext; 
   5. import org.springframework.context.support.ClassPathXmlApplicationContext; 
   6. import org.springframework.web.context.WebApplicationContext; 
   7. import org.springframework.web.context.support.WebApplicationContextUtils; 
   8.  
   9. public class MyTest { 
  10.      
  11.     @Test 
  12.     public void testUser(){ 
  13.              ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); 
  14.              UserDAO dao=(UserDAO)context.getBean("userDAO"); 
  15.              dao.say(); 
  16.              UserDAO dao2=(UserDAO)context.getBean("userDAO2"); 
  17.              dao2.say(); 
  18.              UserDAO dao3=(UserDAO)context.getBean("userDAO3"); 
  19.              dao3.say(); 
  20.     } 
  21.  
  22. } 
  23. </span> 

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


转自:http://javacrazyer.iteye.com/blog/794916

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