(1)搭建与测试 Spring 的开发环境
2012-06-11 19:52
489 查看
1,使用Spring所使用到的jar包
到http://www.springsource.org/download下载spring,然后进行解压缩,在解压目录中找到下面jar文件,拷贝到类路径下
dist\spring.jar
lib\jakarta-commons\commons-logging.jar
如果使用了切面编程(AOP),还需要下列jar文件
lib/aspectj/aspectjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件
lib\j2ee\common-annotations.jar。
2,Spring的配置文件模版
[html]
view plaincopyprint?
<?xml
version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
.....
</beans>
5,开始使用Spring,编写业务逻辑方法,这里就写一个简单的方法。
[java]
view plaincopyprint?
package cn.itm.service.impl;
import cn.itm.service.PersonService;
public class PersonServiceBean
implements PersonService{
public void save(){
System.out.println("我是 save() 方法");
}
}
6:
在Spring容器中 做相应的配置:要用谁就要配置谁,也就是让Spring容器来维护和管理,去找要用的业务方法:
<!-- 业务bean 交给 Spring容器 管理。 -->
<bean id="personService" class="cn.itm.service.impl.PersonServiceBean"></bean>
<!-- 配置好后,这个bean就会由Spring容器 帮我们创建和维护。。。当我们用到bean的时候 就直接从Spring中获取就可以了 -->
[html]
view plaincopyprint?
<?xml
version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 业务bean 交给 Spring容器 管理。 -->
<bean
id="personService"
class="cn.itm.service.impl.PersonServiceBean"></bean>
<!-- 配置好后,这个bean就会由Spring容器 帮我们创建和维护。。。当我们用到bean的时候 就直接从Spring中获取就可以了 -->
</beans>
[java]
view plaincopyprint?
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itm.service.PersonService;
public class SpringTest {
@BeforeClass
public static
void setUpBeforeClass() throws Exception {
}
// 专门用来实例化 Spring 容器的。
@Test public
void instanceSpring(){
// 在类路径下,寻找配置文件来实例化容器。
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
// 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。
PersonService personService = (PersonService) ctx.getBean("personService");
//调用 业务方法:
personService.save();
}
}
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itm.service.PersonService;
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
// 专门用来实例化 Spring 容器的。
@Test public void instanceSpring(){
// 在类路径下,寻找配置文件来实例化容器。
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
// 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。
PersonService personService = (PersonService) ctx.getBean("personService");
//调用 业务方法:
personService.save();
}
}
到http://www.springsource.org/download下载spring,然后进行解压缩,在解压目录中找到下面jar文件,拷贝到类路径下
dist\spring.jar
lib\jakarta-commons\commons-logging.jar
如果使用了切面编程(AOP),还需要下列jar文件
lib/aspectj/aspectjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件
lib\j2ee\common-annotations.jar。
2,Spring的配置文件模版
[html]
view plaincopyprint?
<?xml
version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
.....
</beans>
[java] view plaincopyprint? package junit.test; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } // 专门用来实例化 Spring 容器的。 @Test public void instanceSpring(){ // 在类路径下,寻找配置文件来实例化容器。 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); } } package junit.test; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } // 专门用来实例化 Spring 容器的。 @Test public void instanceSpring(){ // 在类路径下,寻找配置文件来实例化容器。 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); } }
5,开始使用Spring,编写业务逻辑方法,这里就写一个简单的方法。
[java]
view plaincopyprint?
package cn.itm.service.impl;
import cn.itm.service.PersonService;
public class PersonServiceBean
implements PersonService{
public void save(){
System.out.println("我是 save() 方法");
}
}
[java] view plaincopyprint? package cn.itm.service; public interface PersonService { public abstract void save(); } package cn.itm.service; public interface PersonService { public abstract void save(); }
6:
在Spring容器中 做相应的配置:要用谁就要配置谁,也就是让Spring容器来维护和管理,去找要用的业务方法:
<!-- 业务bean 交给 Spring容器 管理。 -->
<bean id="personService" class="cn.itm.service.impl.PersonServiceBean"></bean>
<!-- 配置好后,这个bean就会由Spring容器 帮我们创建和维护。。。当我们用到bean的时候 就直接从Spring中获取就可以了 -->
[html]
view plaincopyprint?
<?xml
version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 业务bean 交给 Spring容器 管理。 -->
<bean
id="personService"
class="cn.itm.service.impl.PersonServiceBean"></bean>
<!-- 配置好后,这个bean就会由Spring容器 帮我们创建和维护。。。当我们用到bean的时候 就直接从Spring中获取就可以了 -->
</beans>
[java] view plaincopyprint? // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。 PersonService personService = (PersonService) ctx.getBean("personService"); //调用 业务方法: personService.save(); // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。 PersonService personService = (PersonService) ctx.getBean("personService"); //调用 业务方法: personService.save();
[java]
view plaincopyprint?
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itm.service.PersonService;
public class SpringTest {
@BeforeClass
public static
void setUpBeforeClass() throws Exception {
}
// 专门用来实例化 Spring 容器的。
@Test public
void instanceSpring(){
// 在类路径下,寻找配置文件来实例化容器。
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
// 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。
PersonService personService = (PersonService) ctx.getBean("personService");
//调用 业务方法:
personService.save();
}
}
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itm.service.PersonService;
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
// 专门用来实例化 Spring 容器的。
@Test public void instanceSpring(){
// 在类路径下,寻找配置文件来实例化容器。
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
// 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。
PersonService personService = (PersonService) ctx.getBean("personService");
//调用 业务方法:
personService.save();
}
}
相关文章推荐
- Spring学习笔记(一)环境搭建和测试开发环境
- ITCAST视频-Spring学习笔记(搭建与测试Spring的开发环境)
- spring学习笔记整理--02(搭建与测试Spring的开发环境)
- 搭建与测试Spring的开发环境
- Spring之旅--搭建与测试Spring开发环境
- (2)搭建与测试 Spring 的开发环境
- 搭建与测试Spring的开发环境
- 一步一步深入spring(1)--搭建和测试spring的开发环境
- 搭建与测试Spring的开发环境
- Spring2.5教程:2、搭建与测试Spring的开发环境
- Spring学习笔记(一)环境搭建和测试开发环境
- 搭建与测试Spring的开发环境_容器常用的两种方式
- spring2.5.6学习笔记三:搭建与测试Spring的开发环境
- 搭建与测试Spring的开发环境
- Spring第二弹—–搭建与测试Spring的开发环境
- SpringBoot + MyBatis + MySQL + Maven在Intellij IDEA下开发环境的搭建
- 在WIN下搭建PHP的测试、开发环境
- spring快速开发搭建环境
- SpringMVC+Spring3+hibernate4 开发环境搭建以及一个开发实例教程
- Spring MVC-使用Spring Tool Suite IDE搭建Spring MVC开发环境