Spring——${}和#{}

Spring——${}和#{}

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

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

好,下面上货。

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

2、修改pom文件
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <springframework.version>4.3.7.RELEASE</springframework.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${springframework.version}</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.xueyou.demo.App</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

3、看一下目录结构:


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、运行结果:

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值