Spring——${}和#{}

Spring——${}和#{}

本篇文章主要介绍一下${}和#{}的区别。首先,${}是用来时间变量插值的一种表达式。#{}是SPEL表达式。他们的作用是通过spring把值注入给某个属性。

spring的主要功能之一是依赖注入,注入的是类这种级别的,同时也是可以注入值的。

好,下面上货。

1、新建maven项目
mvn archetype:generate -DarchetypeCatalog=internal

2、修改pom文件
    
        UTF-8
        4.3.7.RELEASE
    

    
        
            junit
            junit
            4.12
            test
        
        
        
            org.springframework
            spring-context
            ${springframework.version}
        
        
        
            org.springframework
            spring-test
            ${springframework.version}
        

    
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                    utf-8
                
            
            
                maven-assembly-plugin
                3.0.0
                
                    
                        
                            com.xueyou.demo.App
                        
                    
                    
                        jar-with-dependencies
                    
                
                
                    
                        make-assembly 
                        package 
                        
                            single
                        
                    
                
            
        
    

3、看一下目录结构:
Spring——${}和#{}_第1张图片

4、查看每个文件的内容:
App.java
package com.xueyou.demo;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * Hello world!
 */
@Configuration
@ComponentScan(basePackages = {"com.xueyou.demo"})
@PropertySource(value = {"classpath:app.properties", "classpath:part1.properties", "classpath:config.properties"})
public class App {

    public static void main(String[] args) {
        System.out.println("Hello World!");
        ConfigurableApplicationContext configurableApplicationContext = new AnnotationConfigApplicationContext(com.xueyou.demo.App.class);
        Person p = configurableApplicationContext.getBean(Person.class);
        p.introduceMyself();
    }
}


Config.java
package com.xueyou.demo;

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

@Component
public class Config {

    @Value("${config.p1}")
    public String param1;

    @Value("${config.p2}")
    public String param2;
}

Person.java
package com.xueyou.demo;

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

@Component
public class Person {

    @Value("${app.count}")
    private int id;

    @Value("${part1.name}")
    private String name;

    @Value("${app.name}")
    private String spelName;

    @Value("${part1.count}")
    private int spelId;

    @Value("#{config.param1}")
    private String cc;

    @Value("#{config.param2}")
    private int bb;
    public void introduceMyself() {
        System.out.println(id + "\t" + name + "\t" + spelId + "\t" + spelName);
        System.out.println(cc + "\t" + bb);
    }
}

app.properties
app.name=myapp
app.count=1

config.properties
config.p1=abcdefg
config.p2=34

part1.properties
part1.name=part1cc
part1.count=3

SpringTest.java
package com.xueyou.demo;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = App.class)
public class SpringTest {

    @Autowired
    private Person person;

    @Test
    public void goTest(){
        person.introduceMyself();
    }
}


5、运行结果:

你可能感兴趣的:(spring)