仅为个人笔记,方便自己日后查看。
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();
}
浙公网安备 33010602011771号