`

跟我学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> 

相关推荐

    跟我学spring3(1-7)

    【第一章】 Spring概述 ——跟我学Spring3 【第二章】 IoC 之 2.1 IoC基础 ——跟我学Spring3 【第二章】 IoC 之 2.2 IoC 容器基本原理 ——跟我学Spring3 【第二章】 IoC 之 2.3 IoC的配置使用——跟我学Spring3 ...

    跟我学spring3(8-13)

    【第十章】集成其它Web框架 之 10.2 集成Struts1.x ——跟我学spring3 【第十章】集成其它Web框架 之 10.3 集成Struts2.x ——跟我学spring3 【第十章】集成其它Web框架 之 10.4 集成JSF ——跟我学spring3 【第十一...

    跟我学Spring,Spring3学习资料

    跟我学Spring,Spring3学习资料,讲的还是很详细的,适合新手看

    跟我学spring3

    跟我学spring3 跟我学spring3 跟我学spring3

    Springcloud学习笔记.md

    Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Spring...

    跟我学spring3pdf,高清

    跟我学spring3的pdf,便于学习,供大家分享,一起学习

    跟我学Spring3

    跟我学Spring3

    Spring框架学习笔记

    学习spring总结的笔记 希望对初学者有所帮助

    spring指南学习笔记

    spring指南学习笔记

    跟我学spring3 pdf+源码

    跟我学spring3(1-7).pdf 跟我学spring3(8-13).pdf 跟我学spring3-源码.rar 跟我学spring3-项目源码(pointShop)(基于注解).rar 跟我学spring3-项目源码(pointShop)(基于XML配置文件).rar

    Spring6学习笔记

    Spring6学习笔记,师承老杜

    springsecurity学习笔记

    三更springsecurity学习笔记

    springcloud学习笔记.pdf

    springcloud学习笔记.pdf

    Spring Cloud 学习笔记.pdf

    Spring Cloud 学习入门笔记,全方面实践,包含 spring cloud alibaba 模块

    跟我学spring3(1-13)

    最新 跟我学spring3 电子书下载,详细信息见http://blog.csdn.net/jinnianshilongnian/article/details/7316201

Global site tag (gtag.js) - Google Analytics