建立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目录下)
随机推荐
- git 清除本地无效的分支
远程服务器的分支已经删掉了,但是本地分支还存在 $ git fetch -p 如果不行,使用下面的指令 $ git remote prune origin
- atoi函数的实现——面试
主要考虑,字符串中是否有非法字符,字符串是否有溢出控制 #include<stdio.h> int myatoi(const char *str){ ,ret=,i=; if(str[i] ...
- css loading 效果
.loading{ width:160px; height:56px; position: absolute; top:50%; left:50%; line-height:56px; color:# ...
- JVM内存占用情况深入分析
内存分布 首先,列举一下一个JVM进程主要占用内存的一些地方: Young Old metaspace java thread count * Xss other thread count * sta ...
- Web应用层协议---HTTP
处于协议栈顶层的应用层协议定义了运行在不同端系统的应用程序进程如何相互传递报文.定义内容如下: 1.交换的报文类型.请求报文和响应报文. 2.各种报文类型的语法,如报文中的各个字段及这这些字段是如何描 ...
- Html5 input日期时间输入类型标签赋值 正确的格式应该是value="2014-03-15T08:00"
自己也亲测了!T在php的date函数中格式化后是CST 所以不能写成date('Y-m-dTH:i:s',time()) 得把把年月日和时间用date函数分别格式化 再用大写T分割开 放在v ...
- ThinkPHP Http工具类(用于远程采集 远程下载) phpSimpleHtmlDom采集类库_Jquery筛选方式 使用phpQuery轻松采集网页内容http://www.thinkphp.cn/extend/541.html
[php]代码库 view sourceprint? <?php // +------------------------------------------------------------ ...
- 配置IVR实现语音
1.新建系统录音档 登入FreePBX,在Admin面板下选择System Recordings,如下图: 在这个页面可以上传制作好的一些录音,例如像“欢迎致电XXX,按1中文,按2英文... ...
- Linux系统性能检测
转自:http://www.cnblogs.com/itech/archive/2011/06/08/2075145.html 一 .uptime uptime命令用于查看服务器运行了多长时间以及有多 ...
- select函数源码阅读
fd_set结构体 #undef __NFDBITS /* It's easier to assume 8-bit bytes than to get CHAR_BIT. */ #define __N ...