建立maven项目

打开pop.xml文件

添加springframework所依赖的包

  1. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-context</artifactId>
  5. <version>4.3.10.RELEASE</version>
  6. </dependency>

1、pom.xml文件内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6.  
  7. <groupId>com.fz</groupId>
  8. <artifactId>spring01</artifactId>
  9. <version>1.0</version>
  10. <packaging>jar</packaging>
  11. <properties/>
  12. <dependencies>
  13. <dependency>
  14. <groupId>junit</groupId>
  15. <artifactId>junit</artifactId>
  16. <version>4.12</version>
  17. <scope>test</scope>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring-context</artifactId>
  22. <version>4.3.10.RELEASE</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.projectlombok</groupId>
  26. <artifactId>lombok</artifactId>
  27. <version>1.16.18</version>
  28. </dependency>
  29. </dependencies>
  30. <build>
  31. <finalName>${project.artifactId}</finalName>
  32. <sourceDirectory>src/main/java</sourceDirectory>
  33. <testSourceDirectory>src/test/java</testSourceDirectory>
  34. <resources>
  35. <resource>
  36. <directory>src/main/java</directory>
  37. <includes>
  38. <include>**/*.xml</include>
  39. </includes>
  40. </resource>
  41. <resource>
  42. <directory>src/main/resources</directory>
  43. <includes>
  44. <include>**/*.xml</include>
  45. <include>**/*.properties</include>
  46. </includes>
  47. </resource>
  48. </resources>
  49. </build>
  50. </project>

2、在src/main/resources 建立

  1. applicationContext.xml
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
  1.         <!-- spring框架默认bean的单例模式 -->
  2. <bean name="book" class="com.fz.entity.Book"/>
  1. </beans>

3、编写程序要使用的类

  1. src/main/java/com/fz/entity/Book.java
  2. package com.fz.entity;
  3. import lombok.Data;
  4. /**
  5. * Created by webrx on 2017-09-07.
  6. */
  7.  
  8. @Data
  9. public class Book {
  10. private int id;
  11. private String name;
  12. }

4、测试框架代码

  1. src/test/com/Demo.java
  2.  
  3. package com;
  4. import com.fz.entity.Book;
  5. import org.junit.Test;
  6. import org.springframework.beans.factory.BeanFactory;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. import java.util.Date;
  9.  
  10. /**
  11. * Created by webrx on 2017-09-07.
  12. */
  13. public class Demo {
  14. @Test
  15. public void tt(){
  16. BeanFactory f = new ClassPathXmlApplicationContext("applicationContext.xml"); //建立bean工厂
  17. Book b = f.getBean(Book.class);
  18. Book book = f.getBean("book",Book.class);
  19. System.out.println(book);
  20. b.setId(100);
  21. b.setName("java书籍");
  22. System.out.println(b);
  23. System.out.println(b==book);
  24. }
  25. }

建立spring项目入门实例的更多相关文章

  1. Spring oxm入门实例

    O/XMapper是什么? Spring3.0的一个新特性是O/XMapper.O/X映射器这个概念并不新鲜,O代表Object,X代表XML.它的目的是在Java对象(几乎总是一个plainoldJ ...

  2. Vue项目入门实例

    前言 本文记录Vue2.x + Element-UI + TypeScript语法入门实例 为什么要用TypeScript? 1.TypeScript是JavaScript的超集,利用es6语法,实现 ...

  3. Eclipse安装springsource-tool-suite插件及spring helloworld入门实例

    转载至: https://www.cnblogs.com/aaron-shu/p/5156007.html 一.查看eclipse版本 Help-->About Eclipse,我的版本是4.4 ...

  4. Spring Boot下Spring Batch入门实例

    一.About Spring Batch是什么能干什么,网上一搜就有,但是就是没有入门实例,能找到的例子也都是2.0的,看文档都是英文无从下手~~~,使用当前最新的版本整合网络上找到的例子. 关于基础 ...

  5. Lucene建立索引搜索入门实例

                                第一部分:Lucene建立索引 Lucene建立索引主要有以下两步:第一步:建立索引器第二步:添加索引文件准备在f盘建立lucene文件夹,然后 ...

  6. Spring AOP 入门实例详解

    目录 AOP概念 AOP核心概念 Spring对AOP的支持 基于Spring的AOP简单实现 基于Spring的AOP使用其他细节 AOP概念 AOP(Aspect Oriented Program ...

  7. Spring Boot入门实例

    简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置 ...

  8. Spring 简单入门实例

    首先新建一个Web 项目 导入相应Jar 包 <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  9. 建立Spring项目的基础

    1.新建web项目 2.在lib下添加这五个包 3.新建applicationContext.xml(一定在src目录下)

随机推荐

  1. opencv Mat中某点的值

    Mat mat = imread("baby.jpg"); Mat p = mat.col().row(); uchar* ptr = (uchar*) p.data; ]; ]; ...

  2. ubuntu 安装Eigen

    Eigen官网 Eigen是一个高层次的C ++库,有效支持线性代数,矩阵和矢量运算,数值分析及其相关的算法. ubuntu下安装: sudo apt-get install libeigen3-de ...

  3. dubbox实现REST服务

    一.dubbox的由来 dubbox是当当网基于dubbo的基础上开发的扩展版,也可以认为是dubbo的升级版,根据当前互联网的应用需求,增加了很多扩展的功能. dubbox并没有发布到maven中央 ...

  4. struts2学习(8)struts标签1(数据标签、控制标签)

    一.struts2标签简介: struts标签很多,功能强大,这是优点: 但是缺点的话,性能方面可能会,各方面速度啊啥的会降低:有人比较测试,struts性能比jstl低很多:   二.struts2 ...

  5. 【转】如何使用JMeter测试Java项目

    一. Apache JMeter工具 1)简介 JMeter——一个100%的纯Java桌面应用,它是 Apache组织的开放源代码项目,它是功能和性能测试的工具.JMeter可以用于测试静态或者动态 ...

  6. Memory leak by misusing Autofac

    Recently I’ve found out that we can easily cause a memory leaks in our .net application by improper ...

  7. linux 利用nethogs查看某进程的网卡流量

    一.nethogs介绍 分享一个linux 下检测系统进程占用带宽情况的检查.来自github上的开源工具. 它不依赖内核中的模块.当我们的服务器网络异常时,可以通过运行nethogs程序来检测是那个 ...

  8. GridControl 添加全选列

    这里通过List对象绑定GridControl,且不用在GirdControl界面中添加任何列,实现CheckBox列的方法 1.列表中出现CheckBox列 非常简单,在绑定的List实体中,增加一 ...

  9. linux中find工具

    find 由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下.即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只要你具有相应的权限. ...

  10. [Z]LaTeX入门教程

    LaTeX入门教程 Contents TEX/LATEX是什么? 为什么要用TEX/LATEX? 安装 开始使用 数学符号使用中文文章的各个部分表格 行内公式与行间公式 上标与下标 常见的数学公式 行 ...