博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring开发_spring环境搭建
阅读量:7142 次
发布时间:2019-06-28

本文共 2857 字,大约阅读时间需要 9 分钟。

项目结构

这里需要设置环境:

添加如下jar包

commons-logging.jar

spring.jar

/spring_0001_搭建spring环境/src/com/b510/service/PersonService.java

1 package com.b510.service;  2 /**  3  *  4  * @author Hongten  5  * 这是一个Service层的接口  6  *  7  */  8 public interface PersonService {
9 10 public abstract void save(); 11 12 }

/spring_0001_搭建spring环境/src/com/b510/service/impl/PersonServiceBean.java

1 package com.b510.service.impl;  2  3 import com.b510.service.PersonService;  4 /**  5  *  6  * @author Hongten  7  *    实现PersonService接口  8  */  9 public class PersonServiceBean implements PersonService {
10 /* (non-Javadoc) 11 * @see com.b510.service.impl.PersonService#save() 12 */ 13 public void save() {
14 System.out.println("i'm hongten,这是save()方法"); 15 } 16 }

beans.xml

1 
2
6
7

 

/spring_0001_搭建spring环境/src/junit/test/SpringTest.java

package junit.test; import org.junit.BeforeClass; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.b510.service.PersonService; /**  *  * @author Hongten  *    测试类 */ public class SpringTest {
@BeforeClass public static void setUpBeforeClass() throws Exception {
} /** * spring容器实例化 */ public void instanceSpring(){
ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"}); //取出bean PersonService personService=(PersonService) ctx.getBean("personServiceImpl"); //调用bean的方法save() personService.save(); } public static void main(String[] args) {
new SpringTest().instanceSpring(); } }

ApplicationContext实例是Spring的核心,它既是一个巨大的工厂,也是一个功能强大的工厂,spring框架的绝大部分功能

都是通过该容器实现的。

运行结果

2012-3-5 20:19:05 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@15eb0a9: display name [org.springframework.context.support.ClassPathXmlApplicationContext@15eb0a9]; startup date [Mon Mar 05 20:19:05 CST 2012]; root of context hierarchy 2012-3-5 20:19:05 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [beans.xml] 2012-3-5 20:19:05 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@15eb0a9]: org.springframework.beans.factory.support.DefaultListableBeanFactory@16a786 2012-3-5 20:19:05 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@16a786: defining beans [personServiceImpl]; root of factory hierarchy i'm hongten,这是save()方法

 

 

 

 

 

转载地址:http://ubmrl.baihongyu.com/

你可能感兴趣的文章
Python导入不同文件夹下模块
查看>>
浏览器缓存
查看>>
docker 下 alpine 镜像设置时区的有效办法
查看>>
IO重定向
查看>>
bootstrap的carousel图片轮播
查看>>
[转]jQuery Validate使用说明
查看>>
ORACLE表空间的备份与恢复策略
查看>>
asm 兼容性、asm 主要参数管理
查看>>
查函数功能
查看>>
linux命令详解之useradd命令使用方法[linux下 添加用户、删除用户、修改用户密码、用户组管理]...
查看>>
QTcpSocket使用过程中的一些问题记录
查看>>
Discuz常见小问题-如何关闭验证码
查看>>
keystone nova v2 python
查看>>
oracle 11g创建数据库教程
查看>>
Logstash之三:命令行中常用的命令
查看>>
实现自动构建编译javaweb项目并发布到N台服务器
查看>>
Java通过Fork/Join来优化并行计算
查看>>
Spring第四弹—–Spring的三种实例化bean的方式
查看>>
[React Router v4] Render Catch-All Routes with the Switch Component
查看>>
BZOJ 2916: [Poi1997]Monochromatic Triangles [计数]
查看>>