建立spring项目入门实例
建立maven项目
打开pop.xml文件
添加springframework所依赖的包
- <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>4.3.10.RELEASE</version>
- </dependency>
1、pom.xml文件内容如下
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.fz</groupId>
- <artifactId>spring01</artifactId>
- <version>1.0</version>
- <packaging>jar</packaging>
- <properties/>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>4.3.10.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.16.18</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>${project.artifactId}</finalName>
- <sourceDirectory>src/main/java</sourceDirectory>
- <testSourceDirectory>src/test/java</testSourceDirectory>
- <resources>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.xml</include>
- </includes>
- </resource>
- <resource>
- <directory>src/main/resources</directory>
- <includes>
- <include>**/*.xml</include>
- <include>**/*.properties</include>
- </includes>
- </resource>
- </resources>
- </build>
- </project>
2、在src/main/resources 建立
- 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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
- <!-- spring框架默认bean的单例模式 -->
- <bean name="book" class="com.fz.entity.Book"/>
- </beans>
3、编写程序要使用的类
- src/main/java/com/fz/entity/Book.java
- package com.fz.entity;
- import lombok.Data;
- /**
- * Created by webrx on 2017-09-07.
- */
- @Data
- public class Book {
- private int id;
- private String name;
- }
4、测试框架代码
- src/test/com/Demo.java
- package com;
- import com.fz.entity.Book;
- import org.junit.Test;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import java.util.Date;
- /**
- * Created by webrx on 2017-09-07.
- */
- public class Demo {
- @Test
- public void tt(){
- BeanFactory f = new ClassPathXmlApplicationContext("applicationContext.xml"); //建立bean工厂
- Book b = f.getBean(Book.class);
- Book book = f.getBean("book",Book.class);
- System.out.println(book);
- b.setId(100);
- b.setName("java书籍");
- System.out.println(b);
- System.out.println(b==book);
- }
- }
建立spring项目入门实例的更多相关文章
- Spring oxm入门实例
O/XMapper是什么? Spring3.0的一个新特性是O/XMapper.O/X映射器这个概念并不新鲜,O代表Object,X代表XML.它的目的是在Java对象(几乎总是一个plainoldJ ...
- Vue项目入门实例
前言 本文记录Vue2.x + Element-UI + TypeScript语法入门实例 为什么要用TypeScript? 1.TypeScript是JavaScript的超集,利用es6语法,实现 ...
- Eclipse安装springsource-tool-suite插件及spring helloworld入门实例
转载至: https://www.cnblogs.com/aaron-shu/p/5156007.html 一.查看eclipse版本 Help-->About Eclipse,我的版本是4.4 ...
- Spring Boot下Spring Batch入门实例
一.About Spring Batch是什么能干什么,网上一搜就有,但是就是没有入门实例,能找到的例子也都是2.0的,看文档都是英文无从下手~~~,使用当前最新的版本整合网络上找到的例子. 关于基础 ...
- Lucene建立索引搜索入门实例
第一部分:Lucene建立索引 Lucene建立索引主要有以下两步:第一步:建立索引器第二步:添加索引文件准备在f盘建立lucene文件夹,然后 ...
- Spring AOP 入门实例详解
目录 AOP概念 AOP核心概念 Spring对AOP的支持 基于Spring的AOP简单实现 基于Spring的AOP使用其他细节 AOP概念 AOP(Aspect Oriented Program ...
- Spring Boot入门实例
简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置 ...
- Spring 简单入门实例
首先新建一个Web 项目 导入相应Jar 包 <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- 建立Spring项目的基础
1.新建web项目 2.在lib下添加这五个包 3.新建applicationContext.xml(一定在src目录下)
随机推荐
- opencv Mat中某点的值
Mat mat = imread("baby.jpg"); Mat p = mat.col().row(); uchar* ptr = (uchar*) p.data; ]; ]; ...
- ubuntu 安装Eigen
Eigen官网 Eigen是一个高层次的C ++库,有效支持线性代数,矩阵和矢量运算,数值分析及其相关的算法. ubuntu下安装: sudo apt-get install libeigen3-de ...
- dubbox实现REST服务
一.dubbox的由来 dubbox是当当网基于dubbo的基础上开发的扩展版,也可以认为是dubbo的升级版,根据当前互联网的应用需求,增加了很多扩展的功能. dubbox并没有发布到maven中央 ...
- struts2学习(8)struts标签1(数据标签、控制标签)
一.struts2标签简介: struts标签很多,功能强大,这是优点: 但是缺点的话,性能方面可能会,各方面速度啊啥的会降低:有人比较测试,struts性能比jstl低很多: 二.struts2 ...
- 【转】如何使用JMeter测试Java项目
一. Apache JMeter工具 1)简介 JMeter——一个100%的纯Java桌面应用,它是 Apache组织的开放源代码项目,它是功能和性能测试的工具.JMeter可以用于测试静态或者动态 ...
- Memory leak by misusing Autofac
Recently I’ve found out that we can easily cause a memory leaks in our .net application by improper ...
- linux 利用nethogs查看某进程的网卡流量
一.nethogs介绍 分享一个linux 下检测系统进程占用带宽情况的检查.来自github上的开源工具. 它不依赖内核中的模块.当我们的服务器网络异常时,可以通过运行nethogs程序来检测是那个 ...
- GridControl 添加全选列
这里通过List对象绑定GridControl,且不用在GirdControl界面中添加任何列,实现CheckBox列的方法 1.列表中出现CheckBox列 非常简单,在绑定的List实体中,增加一 ...
- linux中find工具
find 由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下.即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只要你具有相应的权限. ...
- [Z]LaTeX入门教程
LaTeX入门教程 Contents TEX/LATEX是什么? 为什么要用TEX/LATEX? 安装 开始使用 数学符号使用中文文章的各个部分表格 行内公式与行间公式 上标与下标 常见的数学公式 行 ...