Spring框架--实现使用配置文件控制对象的生成
2016-06-30 18:47
507 查看
Spring框架的学习1--实现使用配置文件控制对象的生成
MyEclipse环境
新建一个Java Project,配置Spring3.1版本,在src目录下默认增加applicationContext.xml文件
运行步骤:
1)在com.etc.service下新建一个类HelloService.java
package com.etc.service;
public class HelloService {
public void sayHello()
{
System.out.println("hello spring");
}
}
2)在applicationContext.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.1.xsd" >
<bean name="hello" class="com.etc.service.HelloService"></bean>
</beans>
3)右键工程名,新建一个test源文件夹,在文件夹下新建一个com.etc.service包,在这个包下新建
HelloServiceTest.java类
package com.etc.service;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloServiceTest {
@Test
public void test1()
{
//1.配置applicationContext.xml文件
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
HelloService hello = (HelloService) context.getBean("hello");
hello.sayHello();
}
}
4)运行结果
hello spring
如果不使用Spring框架,在HelloServiceTest.java中的实现代码如下:
HelloService myService = new HelloService();
myService.sayHello();
代码现在看起来虽然简单,但是两个类之间存在依赖关系。之后随着代码的增加,Spring写的代码会越来越少。
使用了Spring框架之后,不再是在类中使用new关键字来新建一个对象,而是通过在配置文件中进行相应的
配置来实现。
效果:不需要在测试类中new一个对象,从而生成一个对象,增加他们之间的依赖性,只需要在Spring的
配置文件中设置bean标签,就可以实现new的功能,并且没有类之间依赖性的存在。
MyEclipse环境
新建一个Java Project,配置Spring3.1版本,在src目录下默认增加applicationContext.xml文件
运行步骤:
1)在com.etc.service下新建一个类HelloService.java
package com.etc.service;
public class HelloService {
public void sayHello()
{
System.out.println("hello spring");
}
}
2)在applicationContext.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.1.xsd" >
<bean name="hello" class="com.etc.service.HelloService"></bean>
</beans>
3)右键工程名,新建一个test源文件夹,在文件夹下新建一个com.etc.service包,在这个包下新建
HelloServiceTest.java类
package com.etc.service;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloServiceTest {
@Test
public void test1()
{
//1.配置applicationContext.xml文件
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
HelloService hello = (HelloService) context.getBean("hello");
hello.sayHello();
}
}
4)运行结果
hello spring
如果不使用Spring框架,在HelloServiceTest.java中的实现代码如下:
HelloService myService = new HelloService();
myService.sayHello();
代码现在看起来虽然简单,但是两个类之间存在依赖关系。之后随着代码的增加,Spring写的代码会越来越少。
使用了Spring框架之后,不再是在类中使用new关键字来新建一个对象,而是通过在配置文件中进行相应的
配置来实现。
效果:不需要在测试类中new一个对象,从而生成一个对象,增加他们之间的依赖性,只需要在Spring的
配置文件中设置bean标签,就可以实现new的功能,并且没有类之间依赖性的存在。
相关文章推荐
- JavaSE 基础 第50节 Java中的异常链
- JavaSE 基础 第49节 手动抛出异常
- Eclipse使用andmore插件打开ADT插件创建的android工程出错解决办法
- NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config的问题
- 浅谈Java的三大特性
- JavaSE 基础 第48节 Java中的异常声明
- JavaSE 基础 第47节 获取异常信息
- JavaSE 基础 第46节 异常的分类
- java web的初始化加载顺序,以及servlet的运行过程
- Thinking in java开篇
- JavaSE 基础 第45节Java异常快速入门
- Spring:com.sun.proxy.$proxy0 cannot be cast to XXX
- JavaSE 基础 第44节 引用外部类的对象
- JavaSE 基础 第43节 静态内部类
- JavaSE 基础 第42节 局部内部类
- [javaEE] jsp的指令
- Java——数组
- 一个LDA(Latent Dirichlet Allocation)主题模型的Java实现
- java_set
- spring组合注解