spring4学习笔记--配置Spring-container
来源:互联网 发布:吴天心知乎 编辑:程序博客网 时间:2024/06/03 14:47
在spring4.0中提供了两种方式用于配置spring-container,分别是传统的XMLconfig与新特性JavaConfig,两者是可以混用的。
XMLconfig
使用传统的XMLconfig配置时,只需要新建一个XML文件,在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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"></beans>
JavaConfig
spring在4.0提供了新特性,即可以使用java文件对spring进行配置,使得spring的配置变得更为简单,只需要给任意的类加上注解@Configuration即可,这个类即作为spring用于生成spring-container的配置文件
@Configurationpublic class ConfigurationClass{}
编写测试
- 首先写一个被自动扫描的对象
package com.spring4.testConfig.beans;import org.springframework.stereotype.Component;@Componentpublic class CompactDisc { public void embark(){ System.out.println("wang wang"); }}
1.使用XML配置时
- 首先应该在XML中添加
<context:component-scan base-package="com.spring4.testConfig" />
- 然后编写测试用例
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(value={"classpath:spring-context.xml"})public class TestXMLBeans extends BaseTest4XML { @Autowired private CompactDisc compactDisc ; @Test public void testDoTest(){ System.out.println("123"); Assert.assertNotNull(compactDisc); }}
运行Junit执行成功则表示Spring-container配置成功并成功的取出对象
2.使用java配置时
- 首先应该在java文件类上添加注解
@ComponentScan(value="com.spring4.testConfig")
- 然后编写测试用例
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes=ConfigurationClass.class)public class BaseTest { @Autowired private CompactDisc compactDisc; @Test public void doTestComp(){ Assert.assertNotNull(compactDisc); compactDisc.embark(); }}
运行Junit执行成功则表示Spring-container配置成功并成功的取出对象。
以上就是配置简单的spring-container的全部内容啦,更多精彩内容请持续关注,欢迎在评论区交流。
0 0
- spring4学习笔记--配置Spring-container
- Spring4 学习笔记(3)-Spring 基于 XML 的方式配置 Bean(供自己学习)
- Spring4学习笔记-通过注解配置bean
- Spring4 学习笔记(3)-Spring 基于 XML 的方式配置 Bean
- Spring4 学习笔记(4)-Spring 属性配置的一些细节
- Spring4.0学习笔记--web下配置spring并整合Struts2
- Spring4学习:Spring Cache
- Spring4 学习笔记(5)-Spring 的自动装配
- Spring4.0学习笔记002——Spring应用初识
- Spring4学习笔记----Spring的依赖注入详解
- J2EE系列之Spring4学习笔记(一)--Spring介绍
- Spring4第五讲学习笔记,整合Spring,Struts,Mybatis框架
- Spring4学习笔记-AOP
- Spring4学习笔记
- Spring4学习笔记-JDBC
- Spring4实战学习笔记
- Spring4实战学习笔记
- Spring4 学习笔记
- 内部类可以引用它的包含类的成员吗?有没有什么限制?
- 第10章 shell脚本编写技巧
- 45个实用的JavaScript技巧、窍门和最佳实践
- RHCE练习题 10-11. NFS服务 共享与挂载
- canvas 学习笔记01
- spring4学习笔记--配置Spring-container
- 手把手教你Photoshop中的图层混合模式(一)
- 通过上次写的自动获取IP代理池,访问whatismyip,来测试ip代理访问
- LTE 信令流程Log 之 OAI eNB
- 第11章 正则表达式
- 【强烈推荐】开源C#工具类MSCL系列(四)缓存Cache帮助类
- Java的BigInteger的应用
- super.getClass()方法调用
- RNN with Adaptive Computation Time