spring框架学习笔记(一)
2016-04-16 22:06
459 查看
仅为个人笔记,方便自己日后查看。
eclipse安装spring插件的方法:
http://jingyan.baidu.com/article/1612d5005fd087e20f1eee10.html
使用maven添加spring需要的jar包。
几个必须的jar包:core、bean、context、express。另外依赖一个日志包commons—logging
pom.xml文件中为了统一版本,因此在properties写了版本号如下:
完整示例pom文件为:
创建实体类ProductEntity:
关键点:
spring的application配置文件
使用spring来获取对象
applicationContext.xml文件如下:
测试,使用spring来创建对象:
eclipse安装spring插件的方法:
http://jingyan.baidu.com/article/1612d5005fd087e20f1eee10.html
使用maven添加spring需要的jar包。
几个必须的jar包:core、bean、context、express。另外依赖一个日志包commons—logging
pom.xml文件中为了统一版本,因此在properties写了版本号如下:
<properties> <!-- spring版本号 --> <spring.version>4.0.2.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.2.6</mybatis.version> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties>
完整示例pom文件为:
<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.pf.Soft</groupId> <artifactId>MySpringLearn</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <!-- spring版本号 --> <spring.version>4.0.2.RELEASE</spring.version> <!-- Mybatis版本号 --> <mybatis.version>3.2.6</mybatis.version> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <!-- Spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring依赖的日志包--> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency> </dependencies> </project>
创建实体类ProductEntity:
public class ProductEntity { /** * 商品编码 */ private String prodNo; /** * 商品名称 */ private String prodName; /** * * @return the prodNo */ public String getProdNo() { return prodNo; } /** * @param prodNo the prodNo to set */ public void setProdNo(String prodNo) { this.prodNo = prodNo; } /** * * @return the prodName */ public String getProdName() { return prodName; } /** * @param prodName the prodName to set */ public void setProdName(String prodName) { this.prodName = prodName; }
关键点:
spring的application配置文件
使用spring来获取对象
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.xsd"> <!--xml的方式配置bean --> <bean id="productBean" class="com.pfSoft.beans.ProductEntity"> <property name="prodNo" value="牙膏"></property> <property name="prodName" value="筷子"></property> </bean> </beans>
测试,使用spring来创建对象:
ApplicationContext applicationContext; @Before public void init() { applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void test() { try { ProductEntity productEntity= (ProductEntity) applicationContext.getBean("productBean"); System.out.println(productEntity.getProdNo()); System.out.println(productEntity.getProdName()); } catch (Exception e) { // TODO: handle exception } Calendar calendar = Calendar.getInstance(); }
相关文章推荐
- Java语法基础-数组
- spring websocket 初次了解
- download and install the jdk1.8 on ubuntu14.4
- 20145222黄亚奇《Java程序设计》实验二实验报告
- Java学习之集合框架
- windows可执行文件调用jar包执行java程序-toolsRun.bat
- 安卓eclipse与genymotion模拟器关联的插件解决办法
- 修改myeclipse 自动提示只有.abc
- 排序算法Java实现
- java BIO NIO AIO 理论篇
- 使用 Gradle 编译 Java 项目时报错: Could not find Tools.jar
- Java语法基础-函数
- mysql 保存手机输入法的数据报错org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback
- java区别(String)和toString()的区别
- spring 详解AOP
- hadoop平台搭建(2)--jdk的安装及配置
- Java基础复习(四)
- Java SE 基础知识
- struts2中在Action中如何获取servlet的api?
- Java编程思想--格式化说明符