`

跟我学Spring3 学习笔记一

 
阅读更多

 

public interface HelloApi {
	public void sayHello();  

}

 

 

public class HelloImpl implements HelloApi{

	public void sayHello() {
		System.out.println("Hello World ! ");
	}

}

 

 

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

	<!-- id 表示你这个组件的名字,class表示组件类 -->
	<bean id="hello"
		class="com.HelloImpl"> 
		
	</bean>

</beans>

 

public class HelloImplTest {

	@Test
	public void testSayHello() {
		//ClassPathXmlApplicationContext: ApplicationContext实现, 
		// 以classes为根目录算起
		//1、读取配置文件实例化一个Ioc容器
		ApplicationContext context = new ClassPathXmlApplicationContext("helloworld.xml");
		//2、从容器获取Bean,注意此处完成 “面向接口编程,而不是面向实现”
		HelloApi helloApi = context.getBean("hello",HelloApi.class);
		//3、执行业务逻辑
		helloApi.sayHello();
		
		//XmlBeanFactory 从classpath 或 文件系统等获取资源(项目根目录)
		File file = new File("helloworld.xml");
		Resource resource = new FileSystemResource(file);
		                  // = new ClassPathResource("helloworld.xml");
		BeanFactory beanFactory = new XmlBeanFactory(resource);
		                  //  = new XmlBeanFactory(resource);
		HelloApi helloApi2 = (HelloApi)beanFactory.getBean("hello");
		helloApi2.sayHello();
		
		// FileSystemXmlApplicationContext:ApplicationContext实现,
		// 从文件系统获取配置文件(项目根目录)
		BeanFactory beanFactory2 = new FileSystemXmlApplicationContext("helloworld.xml");
		HelloApi helloApi3 = (HelloApi)beanFactory2.getBean("hello");
		helloApi3.sayHello();
	}

}
 

 

 

分享到:
评论
1 楼 sblig 2013-01-04  
配置列子如下
<?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"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation=" 
           http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/mvc 
           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" default-autowire="byName">
										<!-- default-autowire="byName",约定优于配置,约定优于配置 -->
	
	<!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd -->
	<mvc:resources mapping="/img/**" location="/img/"/>
	<mvc:resources mapping="/js/**" location="/js/"/>
	<mvc:resources mapping="/css/**" location="/css/"/>
	<mvc:resources mapping="/html/**" location="/html/"/>

	<!-- 
	①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 
	-->
	<context:component-scan base-package="com.fsj.spring.web" />

	<!-- 
	②:启动Spring MVC的注解功能,完成请求和注解POJO的映射,添加拦截器,类级别的处理器映射 
	-->
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <list>
                <bean class="com.fsj.spring.util.MyHandlerInterceptor"/>
            </list>
        </property>
	</bean>
	
	<!-- 
	②:启动Spring MVC的注解功能,完成请求和注解POJO的映射,
	配置一个基于注解的定制的WebBindingInitializer,解决日期转换问题,方法级别的处理器映射
	-->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
	    <property name="cacheSeconds" value="0" />
	    <property name="webBindingInitializer">
	        <bean class="com.fsj.spring.util.MyWebBinding" />
	    </property>
	    <!-- 配置一下对json数据的转换 -->
	    <property name="messageConverters">
	    	<list>
	    		<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
	    	</list>
	    </property>
	</bean>
	
	<!-- 
	③:对模型视图名称的解析,即在模型视图名称添加前后缀
	InternalResourceViewResolver默认的就是JstlView所以这里就不用配置viewClass了 
	-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
</beans> 

相关推荐

    跟我学javaweb全套ppt

    《跟我学Java Web》内容包括搭建Web开发环境、HTML相关技术基础知识、JavaScript相关技术基础知识、JSP技术基础知识、Servlet技术基础知识、搭建MySQL数据库开发环境、JDBC技术、JavaBean技术基础知识、Ajax技术基础...

    《跟我学Java26日通》随堂笔记 java教学课件

    《跟我学Java26日通》是一门旨在帮助初学者快速掌握Java核心技术的视频教程,配合随堂笔记,使得学习过程更加系统且有效。 在Java教学中,我们通常会涵盖以下几个重要的知识点: 1. **基础语法**:Java的基础语法...

    Blaze+Spring整合资料

    7. "跟我StepByStep学FLEX教程" 系列文章是逐步指导读者如何配置Flex开发环境,特别是与Spring的整合,包括配置开发工具、创建Flex项目、设置数据源等步骤。 通过这些资源,开发者可以学习到如何有效地利用BlazeDS...

    跟我学java26日通 第16天

    在“跟我学java26日通 第16天”的学习中,我们将深入理解这一核心概念。 反射机制的核心在于Class类,它代表了Java程序中的类。通过Class对象,我们可以动态地获取类的信息,包括类名、包名、方法、构造器、字段等...

    下面是我对于JAVA学习的一些心得体会

    学习JAVA是一种深度探索的过程,它不仅要求我们掌握基础语法,还需要理解其背后的原理和技术。JAVA以其面向对象的特性,强大的类库支持,以及在多领域中的广泛应用,成为了开发者的重要工具。学习JAVA首先需要掌握...

    ssm的整合SSM.zip

    知道这个地方需要什么,以后知道在哪儿可以改,即使忘记了具体代码,也可以在网上查到,学习方法很重要,我还记得我表哥天天跟我说他是不是不适合这个行业,其实我也最开始觉得自己不适合,但慢慢发现博客记录很多...

    译林版-7B-Unit4笔记整理.doc

    2. **follow** - 动词"follow"表示“跟随”或“遵循”,例如"follow sb."(跟着某人),“follow me to the office”(跟我去办公室)。另外,它还可以表示时间或事件的顺序,如"Spring follows winter"(冬去春来)...

Global site tag (gtag.js) - Google Analytics