spring框架学习笔记(一)
生活随笔
收集整理的這篇文章主要介紹了
spring框架学习笔记(一)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
僅為個(gè)人筆記,方便自己日后查看。
?
eclipse安裝spring插件的方法:
http://jingyan.baidu.com/article/1612d5005fd087e20f1eee10.html
?
使用maven添加spring需要的jar包。
幾個(gè)必須的jar包:core、bean、context、express。另外依賴一個(gè)日志包c(diǎn)ommons—logging
pom.xml文件中為了統(tǒng)一版本,因此在properties寫了版本號(hào)如下:
<properties><!-- spring版本號(hào) --><spring.version>4.0.2.RELEASE</spring.version><!-- mybatis版本號(hào) --><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版本號(hào) --><spring.version>4.0.2.RELEASE</spring.version><!-- Mybatis版本號(hào) --><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>?
創(chuàng)建實(shí)體類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;}關(guān)鍵點(diǎn):
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>?
?
測(cè)試,使用spring來創(chuàng)建對(duì)象:
ApplicationContext applicationContext;@Beforepublic void init() {applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");}@Testpublic 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();}?
轉(zhuǎn)載于:https://www.cnblogs.com/falcon-fei/p/5398795.html
總結(jié)
以上是生活随笔為你收集整理的spring框架学习笔记(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 芋泥麻薯小丸子做五十份要多少的材料?
- 下一篇: 继续说一下2016里面的json功能(1