zoukankan      html  css  js  c++  java
  • Hello Spring

    开发一个Spring项目

    工具:idea

    新建一个maven项目

    添加相关的maven依赖

     <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>4.2.0.RELEASE</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.2.0.RELEASE</version>
            </dependency>


           spring-beans.4.2.0.jar
       附带了core核心 和commons-logging

           spring-context.4.2.0.jar

             spring-expression.4.2.0.jar

             spring-aoplple.4.2.0.jar

             spring-aop.4.2.0.jar

    这两个依赖会下载和它相关的jar

    引入日志

    <!--日志的jar-->
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    </dependency>
    添加log4j.properties文件
    log4j.rootLogger=info,con
    #日志的输出级别是info,输出源是con
    #定义输出源的输出位置是控制台
    log4j.appender.con=org.apache.log4j.ConsoleAppender
    #定义输出日志的布局采用的类
    log4j.appender.con.layout=org.apache.log4j.PatternLayout
    #定义日志输出的布局
    log4j.appender.con.layout.ConversionPattern=%d{MM-dd HH:mm:ss}[%p]%c%n -%m%n

    日志输出图:

    编写HelloSpring类

    package cn.baby.entity;
    
    /**
     * Created by Administrator on 2017/10/18.
     */
    public class HelloSpring {
        private String who=null;
    
        public String getWho() {
            return who;
        }
    
        public void setWho(String who) {
            this.who = who;
        }
    
        //输出方法
        public void print(){
            System.out.println("Hello,"+this.getWho()+"!");
        }
    }

    编写Spring配置文件

    applicationContext-hello.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:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
    ">
        <!--通过bean元素声明需要Spring创建的实例.该实例属性的类型通过class属性来指定并通过id属性为实例指定一个名称,以便于访问-->
        <bean id="helloSpring" class="cn.baby.entity.HelloSpring">
            <!--通过property元素用来为实例的属性赋值-->
            <property name="who">
                <value>Spring</value>
            </property>
        </bean>
    
    </beans>

    编写测试方法

     @Test
        public void show()
        {
            //实例化Spring
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext-hello.xml");
            //通过ApplicationContext的getBen方法,根据id来获取Bean的实例
            HelloSpring hellospring = (HelloSpring) context.getBean("helloSpring");
            hellospring.print();
        }
  • 相关阅读:
    函数对象中的prototype属性
    undefined和null的区别
    访问修饰符
    继承
    静态成员和实例成员的区别
    js模拟Trim()方法
    连接池的执行原理
    Javascript中的= =(等于)与= = =(全等于)区别
    数据库中创建约束
    KM算法入门
  • 原文地址:https://www.cnblogs.com/liuzhiw/p/7688016.html
Copyright © 2011-2022 走看看