`
JavaCrazyer
  • 浏览: 2992458 次
  • 性别: Icon_minigender_1
  • 来自: 河南
社区版块
存档分类

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>
 


 

分享到:
评论
1 楼 吕檀溪 2013-10-18  

相关推荐

    spring-boot-starter-kafka示例程序

    spring-boot-starter-kafka示例程序\n 支持springcloud1.5.4,kafka0.8.2.x\n 项目地址:https://github.com/zhyea/spring-boot-starter-kafka

    spring-cloud-examples

    Spring Cloud使用的各种示例,以最简单、最实用为标准 spring-cloud-eureka:eureka server单机、双机、集群示例 eureka-producer-consumer:利用eureka实现服务提供与调用示例 spring-cloud-hystrix:Hystrix熔断...

    spring-boot-websocket-client代码示例

    spring-boot-websocket-client 代码示例

    Spring微服务示例-spring-cloud-example.zip

    Spring微服务示例-spring-cloud-example

    spring-boot-web-restfulcrud代码示例

    spring-boot-web代码示例,是IDEA的项目工程,使用restful风格实现增删查改,静态数据

    Spring boot 示例 官方 Demo

    spring-boot-jpa-thymeleaf-curd:spring boot + jpa + thymeleaf 增删改查示例 spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-web...

    spring-framework-4.1.6.RELEASE.rar

    此压缩文件中包含spring的如下组件的doc,source,jar: spring-aop-4.1.6.RELEASE.jar spring-aspects-4.1.6.RELEASE.jar spring-beans-4.1.6.RELEASE.jar spring-context-4.1.6.RELEASE.jar spring-context-...

    Spring3+Spring-data-mongodb1.5.6示例

    Spring3+Spring-data-mongodb1.5.6示例

    spring-cloud-contract-samples, spring 云合同项目示例.zip

    spring-cloud-contract-samples, spring 云合同项目示例 spring-云合同示例这个存储库包含用于 spring 云契约项目的消费者和生产者应用程序。它包含 2个重要分支。 master,我们测试最新版本的spring 云协定,以及 ...

    spring 3.2.4.RELEASE jar包

    spring 3.2.4 Realease 的所有jar包: spring-context-3.2.4.RELEASE.jar spring-core-3.2.4.RELEASE.jar spring-beans-3.2.4.RELEASE.jar spring-test-3.2.4.RELEASE.jar spring-web-3.2.4.RELEASE.jar spring-aop-...

    spring-boot-examples-master.zip

    spring-boot-examples-master示例程序,与各种框架集成,包括: dockercompose-springboot-mysql-nginx spring-boot-actuator spring-boot-banner spring-boot-docker spring-boot-elasticsearch spring-boot-...

    spring-cloud使用的各种示例

    Spring Cloud 使用的各种示例,以最简单、最实用为标准 [Spring Cloud 中文索引](https://github.com/ityouknow/awesome-spring-cloud) &nbsp;| &nbsp; [Spring Boot学习示例代码]...

    spring-data-commons-1.8.0.RELEASE

    spring data jpa的包。spring-data-commons-1.8.0.RELEASE.jar

    Android代码-spring-boot-examples

    Spring Boot 使用的各种示例,以最简单、最实用为标准 Spring Boot 中文索引 | Spring Cloud学习示例代码 | Spring Boot 精品课程 English | Github地址 | 码云地址 | Spring Boot 1.0 Spring Boot 2.0 Spring ...

    spring-cloud-stream-samples, spring 云流示例.zip

    spring-cloud-stream-samples, spring 云流示例 spring Cloud示例应用程序这个库包含使用 spring 云流编写的应用程序的集合。 所有的应用程序都是自包含的。 它们可以针对 Kafka 或者RabbitMQ中间件技术运行。 你...

    spring-boot示例项目

    本项目示例基于spring boot 最新版本(2.1.9)实现,Spring Boot、Spring Cloud 学习示例,将持续更新…… 在基于Spring Boot、Spring Cloud 分布微服务开发过程中,根据实际项目环境,需要选择、集成符合项目...

    spring-data-commons-1.7.2.RELEASEspring-data-jpa-1.5.2.RELEASE-java datajpa

    spring-data-commons-1.7.2.RELEASEspring-data-jpa-1.5.2.RELEASE-java datajpa

    spring-framework-4.2.4.RELEASE-dist

    spring-framework-4.2.4.RELEASE-dist;官网不好下载了。这里分享下资源给大家,完整的spring开发jar包源码包,说明文档,全都有,最新版本不断更新中。

    spring-framework-4.2.3.RELEASE-dist

    spring-framework-4.2.3.RELEASE-dist;官网不好下载了。这里分享下资源给大家,完整的spring开发jar包源码包,说明文档,全都有,最新版本不断更新中。

    spring-beans-3.0.xsd

    spring-beans-3.1.xsd

Global site tag (gtag.js) - Google Analytics