• 聊聊、Spring 第一篇


     Spring 大家都不陌生,企业应用中很流行的一个平台。从最开始的 Servlet 控制所有,到 MVC 模式的出现。从 SSH (Struts、Spring、Hibernate) 所谓的三剑客 到 SpringMVC、SpringBoot 等等。技术总是不断在更新,开源框架也越来越多。世界上很多的大公司也慢慢走向了开源的道路,其实这也是必经之路。世界上总有那么多不断挑战权威,不断挑战垄断,不断改变生活的人。好啦,闲话不说了,我们开始搭建最基本,最小型化的 Spring 应用环境。Spring 核心的概念是控制反转,依赖注入。控制反转的意思就是控制权交给 Spring 平台,依赖注入就是程序依赖于 Spring Bean 通过 Java 反射机制将所需对象注入到程序中。

     一、【工具】 

     项目管理 Maven,开发工具 Eclipse Kepler,JDK 1.7。

    二、【Jar 包】

     spring-context-4.3.8.RELEASE.jar

    三、【项目结构】

     

    四、【web.xml】

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
    
        <display-name>mrp</display-name>
        
    </web-app>

      

    五、【application.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" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
             http://www.springframework.org/schema/beans 
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd
             http://www.springframework.org/schema/tx
             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
             http://www.springframework.org/schema/aop 
             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
        
        <bean id="user" class="com.mrp.www.service.User" />
    
    </beans>  

     

    六、【User类】

    package com.mrp.www.service;
    
    public class User {
        
        private String name = "xxxx";
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }   
    
    }

    七、【Test测试类】 

    package com.mrp.www.service;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    
        private static ApplicationContext context;
    
        public static void main(String[] args) {
            
            context = new ClassPathXmlApplicationContext("application.xml");
            
             User u = (User) context.getBean("user");
             System.out.println(u.getName());
            
        } 
        
    }

    八、【 pom.xml】

    <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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>www.mrp.com</groupId>
        <artifactId>mrp</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>mrp Maven Webapp</name>
        <url>http://maven.apache.org</url>
    
        <properties>
            <build_path>C:	emp${project.artifactId}</build_path>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <spring_webmvc_version>3.2.4.RELEASE</spring_webmvc_version>
            <spring_version>4.2.6.RELEASE</spring_version>
            <jstl_version>1.2</jstl_version>
            <standard_version>1.0.6</standard_version>
            <jackson_version>1.9.13</jackson_version>
            <spring_test_version>3.2.4.RELEASE</spring_test_version>
            <hessian_version>4.0.7</hessian_version>
            <servlet_version>2.5</servlet_version>
        </properties>
    
        <dependencies>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.3.8.RELEASE</version>
            </dependency>
            
            
        </dependencies>
    
        <build>
            <finalName>mrp</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <uriEncoding>${project.build.sourceEncoding}</uriEncoding>
                        <port>8017</port>
                        <path>/${project.artifactId}</path>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerVersion>1.6</compilerVersion>
                    <generatedSourcesDirectory>${build_path}</generatedSourcesDirectory>
                    <generatedTestSourcesDirectory>${build_path}</generatedTestSourcesDirectory>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.4.2</version>
                    <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

     Spring 平台可以集成很多的开源框架,而且 Spring 轻量级,配置简单,上面的例子仅仅配置了一个 User Bean,现实应用当中可就不止一个 Bean 这么简单了。会涉及到 AOP 编程,事务,数据源,缓存,分布式等等一系列的东西。后面在慢慢写出来吧,希望对大家有所帮助,也让自己有所收获。谢谢大家观看!

     

      

  • 相关阅读:
    Windows 8 自带定时关机的4种实现方法
    Windows 7/8 自带定时关机命令
    将博客搬至CSDN
    【贪心】【POJ-3637】Shopaholic
    【贪心】【POJ-2437】Muddy roads
    【贪心】【POJ-1328&AOJ-195】Radar Installation
    【贪心】【POJ-1456】Supermarket
    【贪心】【HDOJ-1789】Doing Homework again
    【贪心】【POJ-1992】Ride to School
    【贪心】【POJ-1018】Communication System
  • 原文地址:https://www.cnblogs.com/xums/p/6818887.html
Copyright © 2020-2023  润新知