建立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目录下)
随机推荐
- 嵌入式视频采集编程思路(Video 4 Linux)-转
转自:http://zyg0227.blog.51cto.com/1043164/271954 1. linux 内核有video for linux简称V4L.V4L是Linux影像系统与嵌入式影 ...
- jdk1.8新特性之lambda表达式
lambda表达式其实就是指一个匿名函数,应用最广泛的就是匿名内部类的简化.在jdk1.8之前,我们定义一个匿名内部类可能需要写一大坨代码,现在有了lambda之后,可以写的很简洁了.但不是说lamb ...
- jdk1.8新特性之函数式接口
函数式接口就是只有一个抽象方法的接口.如果这个接口里没有或者包含了两个以上的抽象方法,对不起,你不叫函数式接口,只能叫你接口.那这个函数式有啥用呢?如果配合Lambda表达式的话,可以大大的简化代码. ...
- Linux 制作补丁 打补丁 撤销补丁
1.制作补丁 diff - 逐行比较文件 格式 diff 参数 旧文件/旧文件夹 新文件/新文件夹 -N 将不存在的文件看作是空的 -a 将所有文件都视为文本文件 -u 以合并 ...
- 高速AD中的LVDS和FPGA
通常情况下,模拟输入信号通过高速ADC的量化输出的数字信号需要交给FPGA进行处理.如果高速ADC采用LVDS输出,那么经量化处理过的数字信号将会有非常多的LVDS数据差分对.而LVDS数据接收端,接 ...
- 用php命令执行php脚本报错,在浏览器里执行却正常。
写了一个Php脚本,里面用到了PDO连接数据库,但是所有的库都已经安装,在浏览器里执行完全正常,但是写到批处理文件里用php命令去执行的时候却报错找不到驱动,很奇怪. 经查找得知原来php命令与浏览器 ...
- SpringMVC使用Hibernate-validator验证出现的错误
缺少jar包 SpringMVC可以使用Hibernate-validator作为效验的实现,需要的jar包: hibernate-validator.jar validation-api.jar j ...
- tomcat:A docBase * inside the host appBase has been specifi, and will be ignored
警告: A docBase D:\apache-tomcat-8.5.12\webapps\webapps\projectname inside the host appBase has been ...
- DOM库及常用方法封装
节点 nodeType nodeName nodeValue 元素节点 1 大写的标签名 null 文本节点 3 #text 文本内容 注释节点 8 #comment 注释内容 document 9 ...
- TypeError: 'ExcelData' object is not iterable
今天写了个测试的代码,结果在执行test_register.py文件在调用readexcle.py的时候一直报错TypeError: 'ExcelData' object is not iterabl ...