Spring-QuickStart

group 'com.yuan'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    // https://mvnrepository.com/artifact/org.springframework/spring-context
    compile group: 'org.springframework', name: 'spring-context', version: '4.3.13.RELEASE'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

src/main/java/Hello/MessageService
package hello;

public class MessageService {
    public String getMessage(){
        return "Hello World";
    }
}

src/main/java/Hello/MessagePrinter
package hello;

public class MessagePrinter {
    private MessageService messageService;

    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }

    public void printMessage(){
        System.out.println(this.messageService.getMessage());

    }
}

src/main/java/Hello/Application
package hello;

public class Application {
    public static void main(String[] args)
    {
        System.out.println("application");
        //创建消息打印机对象
        MessagePrinter printer = new MessagePrinter();
        //创建消息服务对象
        MessageService service = new MessageService();
        //设置打印机的service属性
        printer.setMessageService(service);
        //打印消息
        printer.printMessage();

    }
}


初始化spring容器

spring容器可以帮助我们自动的创建对象,加 @Component 和 @ComponentScan注解
package hello;

import org.springframework.stereotype.Component;

@Component
public class MessageService {

    // 无参构造方法
    public MessageService() {
        super();
        System.out.println("Message Service...");
    }

    public String getMessage(){
        return "Hello World";
    }
}
package hello;

import org.springframework.stereotype.Component;

@Component
public class MessagePrinter {

    // 无参构造方法
    public MessagePrinter() {
        super();
        System.out.println("MessagePrinter...");
    }

    private MessageService messageService;

    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }

    public void printMessage(){
        System.out.println(this.messageService.getMessage());

    }
}
package hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

//主键扫描
@ComponentScan
public class ApplicationSpring {
    public static void main(String[] args)
    {
        System.out.println("applicationSpring");

        //初始化Spring容器
        ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationSpring.class);

    }
}


获取bean对象

通过context.getBean获取
package hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

//主键扫描
@ComponentScan
public class ApplicationSpring {
    public static void main(String[] args)
    {
        System.out.println("applicationSpring");

        //初始化Spring容器
        ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationSpring.class);
        //从容器中获取MessagePrinter
        MessagePrinter printer = context.getBean(MessagePrinter.class);
        //从容器中获取MessageService
        MessageService service = context.getBean(MessageService.class);

        System.out.println(printer);
        System.out.println(service);
        printer.setMessageService(service);
        printer.printMessage();
    }
}



Spring管理对象之间的关联关系

@Autowired:自动调用方法,把service和MessagePrinter的关系创建出来

package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessagePrinter {

    // 无参构造方法
    public MessagePrinter() {
        super();
        System.out.println("MessagePrinter...");
    }

    private MessageService service;

    @Autowired
    public void setService(MessageService service) {
        this.service = service;
    }

    public void printMessage(){
        System.out.println(this.service.getMessage());

    }
}
package hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

//主键扫描
@ComponentScan
public class ApplicationSpring {
    public static void main(String[] args)
    {
        System.out.println("applicationSpring");

        //初始化Spring容器
        ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationSpring.class);
        //从容器中获取MessagePrinter
        MessagePrinter printer = context.getBean(MessagePrinter.class);

        printer.printMessage();
    }
}


spring简介

活跃的开源框架,帮助分离组件之间的依赖关系,可以简化企业开发
IOC:控制反转; DI:依赖注入; AOP:面向切面编程


core:实现IOC,DI
beans:bean的创建
context:上下文,IOC容器
spel:spring表达式语言

spring包含spring mvc

使用xml实现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">
    <!--bean描述当前的对象需要由spring容器管理;
       id属性用于标识对象
       class:被管理的对象的类的全名
       property 第一个service对应MessagePrinter 里面的 private MessageService service;
                第二个service是id=“service”的bean对象
    -->
    <bean id="service" class="hello.MessageService"></bean>
    <bean id="printer" class="hello.MessagePrinter">
        <property name="service" ref="service"></property>
    </bean>
</beans>
package hello;


public class MessagePrinter {

    // 无参构造方法
    public MessagePrinter() {
        super();
        System.out.println("MessagePrinter...");
    }

    private MessageService service;

    public void setService(MessageService service) {
        this.service = service;
    }

    public void printMessage(){
        System.out.println(this.service.getMessage());

    }
}
package hello;


public class MessageService {

    // 无参构造方法
    public MessageService() {
        super();
        System.out.println("Message Service...");
    }

    public String getMessage(){
        return "Hello World";
    }
}
package hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//主键扫描
public class ApplicationSpring {
    public static void main(String[] args)
    {
        System.out.println("applicationSpring");

        //初始化Spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器中获取MessagePrinter
        MessagePrinter printer = context.getBean(MessagePrinter.class);

        printer.printMessage();
    }
}

加入log4j日志系统

加入log4j的jar包
dependencies {
    // https://mvnrepository.com/artifact/org.springframework/spring-context
    compile group: 'org.springframework', name: 'spring-context', version: '4.3.13.RELEASE'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    // https://mvnrepository.com/artifact/log4j/log4j
    compile group: 'log4j', name: 'log4j', version: '1.2.17'
}
创建log4j.properties  (https://docs.spring.io/spring/docs/4.3.13.RELEASE/spring-framework-reference/htmlsingle/)
log4j.rootCategory=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n

log4j.category.org.springframework.beans.factory=DEBUG
再次运行测试