<file_sep>package com.shawllvey.hspring4.event;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by carpenshaw on 2017/3/19.
 */
public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);

        DemoPublisher demoPublisher = context.getBean(DemoPublisher.class);
        demoPublisher.publishDemoEvent("Hello demo");

        context.close();
    }
}
<file_sep>package com.shawllvey.hspring4.prepost;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 使用JSR250形式的Bean
 *
 * Created by carpenshaw on 2017/3/19.
 */
public class JSR250WayService {

    @PostConstruct  // 在构造函数执行完之后执行
    public void init() {
        System.out.println("JSR250WayService init");
    }

    @PreDestroy // 在Bean销毁之前执行
    public void destroy() {
        System.out.println("JSR250WayService destroy");
    }

    public JSR250WayService() {
        System.out.println("JSR250WayService constructor");
    }
}
<file_sep>package com.shawllvey.hspring4.condition;

/**
 * Created by carpenshaw on 2017/3/20.
 */
public interface ListService {
    String showListCmd();
}
<file_sep>package com.shawllvey.hspring4.profile;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * Profile为在不同环境下使用不同的配置提供了支持
 *
 * Created by carpenshaw on 2017/3/19.
 */
@Configuration
public class ProfileConfig {

    @Bean
    @Profile("dev")
    DemoBean devDemoBean() {
        return new DemoBean("devDemoBean");
    }

    @Bean
    @Profile("prod")
    DemoBean prodDemoBean() {
        return new DemoBean("prodDemoBean");
    }


}
<file_sep>book.name=hspring4
book.author=shawllvey<file_sep>package com.shawllvey.hspring4.el;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

import java.io.IOException;

/**
 * 演示配置类
 * Spring EL-Spring表达式语言，支持在xml和注解中使用表达式
 *
 * Created by carpenshaw on 2017/3/19.
 */
@Configuration
@ComponentScan("com.shawllvey.hspring4.el")
@PropertySource("classpath:com/shawllvey/hspring4/el/test.properties")  // 注入配置properties
public class ElConfig {
    @Value("I Love You")    // 注入普通字符串
    private String normal;

    @Value("#{systemProperties['os.name']}")    // 注入操作系统属性
    private String osName;

    @Value("#{T(java.lang.Math).random()*100.0}")   // 注入表达式结果
    private double randomNumber;

    @Value("#{demoService.another}")    // 注入其它Bean属性
    private String fromAnother;

    @Value("classpath:com/shawllvey/hspring4/el/test.txt")  // 注入文件资源
    private Resource testFile;

    @Value("http://www.baidu.com")  // 注入网址资源
    private Resource testUrl;

    @Value("${book.name}")  // 注入配置项，需要与@PropertySource配合
    private String bookName;

    @Autowired
    private Environment environment;    // 注入的properties还可以从environment中获得

    @Bean   // 当使用@Value注入properties的值时，需要此Bean（实际并不一定需要，Spring会自动生成此Bean？）
    public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    public void outputResource() {
        System.out.println(this.normal);
        System.out.println(this.osName);
        System.out.println(this.randomNumber);
        System.out.println(this.fromAnother);

        try {
            System.out.println(IOUtils.toString(testFile.getInputStream()));
            System.out.println(IOUtils.toString(testUrl.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(bookName);
        System.out.println(environment.getProperty("book.author"));
        System.out.println(environment);
    }

}
<file_sep>package com.shawllvey.hspring4.scope;

import org.springframework.stereotype.Service;

/**
 * 编写Singleton的Bean
 *
 * Created by carpenshaw on 2017/3/16.
 */
@Service
//@Scope("singleton") 默认
public class DemoSingletonService {
}
<file_sep>package com.shawllvey.hspring4.annotation;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.lang.annotation.*;

/**
 * 组合注解
 *
 * Created by carpenshaw on 2017/3/20.
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration  // 组合@Configuration注解
@ComponentScan  // 组合@ComponentScan注解
public @interface WiselyConfiguration {
    String[] value() default {};    // 覆盖value参数
}
<file_sep>package com.shawllvey.hspring4.aop;

import org.springframework.stereotype.Service;

/**
 * 编写使用方法规则的被拦截器类
 *
 * Created by carpenshaw on 2017/3/16.
 */
@Service
public class DemoMethodService {
    public void add() {}
}
<file_sep>package com.shawllvey.hspring4.event;

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

/**
 * 事件发布类
 *
 * Created by carpenshaw on 2017/3/19.
 */
@Component
public class DemoPublisher {
    @Autowired
    ApplicationContext context; // 注入ApplicationContext来发布事件

    public void publishDemoEvent(String msg) {
        // 使用applicationContext的publishEvent方法来发布
        context.publishEvent(new DemoEvent(this, msg));
    }
}
<file_sep>package com.shawllvey.hspring4.di;

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

/**
 * 功能类的Bean
 *
 * Created by carpenshaw on 2017/3/16.
 */
@Service // 声明当前类是Spring管理的一个Bean
public class UseFunctionService {

    // 使用@Autowired注解将FunctionService的实体Bean注入到UserFunctionService中，让UserFunctionService具备FunctionService的功能
    // 体现了一种“组合”的理念
    @Autowired
    FunctionService functionService;

    public String sayHello(String word) {
        return functionService.sayHello(word);
    }
}
<file_sep>package com.shawllvey.hspring4.condition;

/**
 * Created by carpenshaw on 2017/3/20.
 */
public class LinuxListService implements ListService {
    @Override
    public String showListCmd() {
        return "ls";
    }
}
