首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring开发_spring环境搭建

spring开发_spring环境搭建

作者头像
Hongten
发布2018-09-13 16:53:53
4040
发布2018-09-13 16:53:53
举报
文章被收录于专栏:HongtenHongten

项目结构

http://www.cnblogs.com/hongten/gallery/image/112322.html

这里需要设置环境:

添加如下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 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4     xsi:schemaLocation="http://www.springframework.org/schema/beans
5            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
6     <bean id="personServiceImpl" class="com.b510.service.impl.PersonServiceBean"></bean>
7 </beans>

/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()方法
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-03-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档