Spring框架本身的四大原则
- 使用POJO进行轻量级和最小入侵式开发
 - 通过依赖注入和基于接口编程实现松耦合
 - 通过AOP和默认习惯进行声明式编程
 - 使用AOP和模板减少模块化代码
 
元注解与组合注解
元注解是可以注释到别的注解上的注解,被注解的注解成为组合注解
元注解有:
- @Retention
 
RetentionPolicy.SOURCE 注解只在源码阶段保留,在编译器进行编译时它将被丢弃忽视。
RetentionPolicy.CLASS 注解只被保留到编译进行的时候,它并不会被加载到 JVM 中。
RetentionPolicy.RUNTIME 注解可以保留到程序运行的时候,它会被加载进入到 JVM 中,所以在程序运行时可以获取到它们。
- @Documented
 
将注解中的元素包含到 Javadoc 中去
- @Target
 
ElementType.ANNOTATION_TYPE 可以给一个注解进行注解
ElementType.CONSTRUCTOR 可以给构造方法进行注解
ElementType.FIELD 可以给属性进行注解
ElementType.LOCAL_VARIABLE 可以给局部变量进行注解
ElementType.METHOD 可以给方法进行注解
ElementType.PACKAGE 可以给一个包进行注解
ElementType.PARAMETER 可以给一个方法内的参数进行注解
ElementType.TYPE 可以给一个类型进行注解,比如类、接口、枚举
- @Inherited
 
Inherited 是继承的意思,但是它并不是说注解本身可以继承,而是说如果一个超类被 @Inherited 注解过的注解进行注解的话,那么如果它的子类没有被任何注解应用的话,那么这个子类就继承了超类的注解。
        @Inherited
        @Retention(RetentionPolicy.RUNTIME)
        @interface Test {}
        
        
        @Test
        public class A {}
        
        
        public class B extends A {}
- @Repeatable
 
可重复注解
        @interface Persons {
            Person[]  value();
        }
        
        
        @Repeatable(Persons.class)
        @interface Person{
            String role default "";
        }
        
        
        @Person(role="artist")
        @Person(role="coder")
        @Person(role="PM")
        public class SuperMan{
            
        }
组合注解
(1)编写Bean
@Service
public class DemoService {
    public void outputResult(){
        System.out.println("组合注解也可以获得bean哦");
    }
}
(2)编写组合配置
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@ComponentScan
public @interface MoConfiguration {
    String[] value() default {};
}
(3)编写配置类
@MoConfiguration
public class DemoConfig {
}
(4)运行
public class Main {
    public static void main(String[] args){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);
        DemoService demoService = context.getBean(DemoService.class);
        demoService.outputResult();
        context.close();
    }
}
依赖注入
spring IoC容器负责创建Bean,并通过容器将功能类注入到你需要的Bean中。Spring提供使用xml、注解、Java配置、groovy配置实现Bean的创建和注入
- 示例
(1)编写功能类的Bean 
@Service
public class FunctionService {
    public String sayHello(String word){
        return "Hello " + word + "!";
    }
}
(2)使用功能类的Bean
@Service //与@Controller、@Component、@Repository等效
public class UseFunctionService {
    @Autowired //将FunctionService的实体Bean注入到UseFunctionService中
    private FunctionService functionService;
    public String SayHello(String word){
        return functionService.sayHello(word);
    }
}
(3)配置类
@Configuration
@ComponentScan //自动扫描包下面所有使用@Service/@Component/@Repository/@Controller的类,并注册为Bean
public class DiConfig {
}
(4)运行
public class Main {
    public static void main(String[] args){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DiConfig.class);
        UseFunctionService useFunctionService = context.getBean(UseFunctionService.class);
        System.out.println(useFunctionService.SayHello("di"));
        context.close();
    }
}
Java配置
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置;Java配置也是Spring Boot推荐的配置方式。通过@Configuration和@Bean来实现
- 示例
(1)编写功能类的Bean 
public class FunctionService {
    public String SayHello(String word){
        return "Hello" + word + "!";
    }
}
(2)使用功能类的Bean
public class UseFunctionService {
    private FunctionService functionService;
    public void setFunctionService(FunctionService functionService) {
        this.functionService = functionService;
    }
    public String SayHello(String word){
        return functionService.SayHello(word);
    }
}
(3)配置类
@Configuration
public class JavaConfig {
    @Bean
    public FunctionService functionService(){
        return new FunctionService();
    }
    @Bean
    public UseFunctionService useFunctionService(){
        UseFunctionService useFunctionService = new UseFunctionService();
        useFunctionService.setFunctionService(functionService());
        return useFunctionService;
    }
}
(4)运行
public class Main {
    public static void main(String[] args){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
        UseFunctionService useFunctionService = context.getBean(UseFunctionService.class);
        System.out.println(useFunctionService.SayHello("java config"));
        context.close();
    }
}
Bean的Scope
Scope描述的是Spring容器如何新建Bean实例的,主要有以下几种
- Singleton:一个Spring容器中只有一个Bean的实例,此为Spring的默认配置,全容器共享一个实例
 - Prototype:每次调用新建一个Bean实例
 - Request:Web项目中,给每个http request新建一个Bean实例
 - Session:Web项目中,给每个http session新建一个Bean实例
 - GlobalSession:这个只在portal应用中有用,给每一个global http session新建一个Bean实例
 
- 实例
 
(1)编写Singleton的Bean
@Service
public class DemoSingletonService {
}
(2)编写Prototype的Bean
@Service
@Scope("prototype")
public class DemoPrototypeService {
}
(3)配置类
@Configuration
@ComponentScan("com.moxuanran.spring.ch2.scope")
public class ScopeConfig {
}
(4)运行
public class Main {
    public static void main(String[] args){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class);
        DemoSingletonService s1 = context.getBean(DemoSingletonService.class);
        DemoSingletonService s2 = context.getBean(DemoSingletonService.class);
        DemoPrototypeService p1 = context.getBean(DemoPrototypeService.class);
        DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class);
        System.out.println("s1与s2是否相等" + s1.equals(s2));
        System.out.println("p1与p2是否相等" + p1.equals(p2));
        context.close();
    }
}
Spring EL和资源调用
Spring开发中经常涉及调用各种资源的情况,包括普通文件,网址,配置文件,系统环境变量等,我们可以使用Spring的表达式语言实现资源的注入
- 实例
 
(1)资源准备
添加依赖
 <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.3</version>
</dependency>
在自己的资源包下新建test.txt,内容随意
在自己的资源包下新建test.properties,内容如下:
book.author=moxuanran
book.name=spring boot
(2)需要注入的Bean
@Service
public class DemoService {
    @Value("其他类的属性")
    private String another;
    public String getAnother() {
        return another;
    }
    public void setAnother(String another) {
        this.another = another;
    }
}
(3)配置类
@Configuration
@ComponentScan("com.moxuanran.spring.ch2.el")
@PropertySource("classpath:com.moxuanran.spring.ch2.el/test.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}")
    private String fromAuthor;
    @Value("classpath:com.moxuanran.spring.ch2.el/test")
    private Resource testFile;
    @Value("http://www.baidu.com")
    private Resource testUrl;
    @Value("${book.name}")
    private String bookName;
    @Autowired
    private Environment environment;
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
        return new PropertySourcesPlaceholderConfigurer();
    }
    public void outputResource(){
        try {
            System.out.println(normal);
            System.out.println(osName);
            System.out.println(randomNumber);
            System.out.println(fromAuthor);
            System.out.println(IOUtils.toString(testFile.getInputStream()));
            System.out.println(IOUtils.toString(testUrl.getInputStream()));
            System.out.println(bookName);
            System.out.println(environment.getProperty("book.author"));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}