• 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;
    	}
    	
    

    关键点:

    1. spring的application配置文件
    2. 使用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();
    	}
    
  • 相关阅读:
    UE4/Unity绘制地图
    腾讯地图定位打卡功能实现
    腾讯地图GPS轨迹回放-安卓篇
    腾讯地图个性化图层创建及发布
    使用腾讯地图实现汽车沿轨迹行驶功能
    腾讯地图GPS轨迹录制
    微信小程序地图定位开发教程
    基于腾讯地图定位实现物业巡检防作弊场景
    Java 序列化
    Java HashMap
  • 原文地址:https://www.cnblogs.com/falcon-fei/p/5398795.html
Copyright © 2020-2023  润新知