SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)

一、

直观的给bean注入值如下:

@Bean
public CompactDisc sgtPeppers() {
    return new BlankDisc(
        "Sgt. Pepper‘s Lonely Hearts Club Band",
        "The Beatles");
}

< bean id = "sgtPeppers"
class = "soundsystem.BlankDisc"
c: _title = "Sgt. Pepper‘s Lonely Hearts Club Band"
c: _artist = "The Beatles" / >

都是以硬编码的形式,spring提供了提供了两种方式以运行进注入(1)Property placeholders  (2)The Spring Expression Language ( S p EL )

二、Property placeholders

1.app.properties

1 disc.title=Sgt. Peppers Lonely Hearts Club Band
2 disc.artist=The Beatles

2.

 1 package com.soundsystem;
 2
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.context.annotation.PropertySource;
 7 import org.springframework.core.env.Environment;
 8
 9 @Configuration
10 @PropertySource("classpath:/com/soundsystem/app.properties")
11 public class EnvironmentConfig {
12
13   @Autowired
14   Environment env;
15
16   @Bean
17   public BlankDisc blankDisc() {
18     return new BlankDisc(
19         env.getProperty("disc.title"),
20         env.getProperty("disc.artist"));
21   }
22
23 }

3.

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4   xmlns:c="http://www.springframework.org/schema/c"
 5   xmlns:context="http://www.springframework.org/schema/context"
 6   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 8
 9   <context:property-placeholder
10     location="com/soundsystem/app.properties" />
11
12   <bean class="com.soundsystem.BlankDisc"
13     c:_0 = "${disc.title}"
14     c:_1 = "${disc.artist}"/>
15
16 </beans>

三、进一步讨论SPRING ’ S E NVIRONMENT

getProperty() is overloaded into four variations:
? String getProperty(String key)
? String getProperty(String key, String defaultValue)
? T getProperty(String key, Class<T> type)
? T getProperty(String key, Class<T> type, T defaultValue)

给定默认值

@Bean
public BlankDisc disc() {
    return new BlankDisc(
        env.getProperty("disc.title", "Rattle and Hum"),
        env.getProperty("disc.artist", "U2"));
}

自动转型

int connectionCount =
env.getProperty("db.connection.count", Integer.class, 30);
@Bean
public BlankDisc disc() {
return new BlankDisc(
env.getRequiredProperty("disc.title"),
env.getRequiredProperty("disc.artist"));
}

Here, if either the disc.title property or the disc.artist property is undefined, an
IllegalStateException will be thrown.

检查是否存在

boolean titleExists = env.containsProperty("disc.title");
时间: 2024-10-05 05:31:39

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)的相关文章

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@Scope、ProxyMode

一. Spring的bean默认是单例的 But sometimes you may find yourself working with a mutable class that does main-tain some state and therefore isn’t safe for reuse. In that case, declaring the class as asingleton bean probably isn’t a good idea because that obje

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍

一. 1.SpEL expressions are framed with  #{ ... } 2.SpEl的作用 Sp EL has a lot of tricks up its sleeves, including the following:? The ability to reference beans by their ID s? Invoking methods and accessing properties on objects? Mathematical, relational

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-010-Introduction为类增加新方法

一. 1.Introduction的作用是给类动态的增加方法 When Spring discovers a bean annotated with @Aspect , it will automatically create a proxy that delegates calls to either the proxied bean or to the introduction implementation, depending on whether the method called be

SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-003- SPRING的GENERAL TAG LIBRARY简介及用&lt;s:message&gt;和ReloadableResourceBundleMessageSource实现国际化

一. SPRING支持的GENERAL TAG LIBRARY 1. 二.用<s:message>和ReloadableResourceBundleMessageSource实现国际化 1.配置ReloadableResourceBundleMessageSource,它能it has the ability to reload message properties without recompiling or restarting the application. 1 package spi

SPRING IN ACTION 第4版笔记-第六章Rendering web views-001- Spring支持的View Resolver、InternalResourceViewResolver、JstlView

一.Spring支持的View Resolver 二.InternalResourceViewResolver Spring supports JSP views in two ways:? InternalResourceViewResolver ? Spring provides two JSP tag libraries, one for form-to-model binding and one providing general utility features. 1.在java中定义

SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-006Spring-Data的运行规则(@EnableJpaRepositories、&lt;jpa:repositories&gt;)

一.JpaRepository 1.要使Spring自动生成实现类的步骤 (1)配置文件xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xm

SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-006- 使用thymeleaf(TemplateResolver、SpringTemplateEngine、ThymeleafViewResolver、th:include、th:object、th:field=&quot;*{firstName}&quot;)

一.在Spring中使用thymeleaf的步骤 1.配置 In order to use Thymeleaf with Spring, you’ll need to configure three beans that enable Thymeleaf-Spring integration:? A ThymeleafViewResolver that resolves Thymeleaf template views from logical view names? A SpringTempl

SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-002-Controller的requestMapping、model

一.RequestMapping 1.可以写在方法上或类上,且值可以是数组 1 package spittr.web; 2 3 import static org.springframework.web.bind.annotation.RequestMethod.*; 4 5 import org.springframework.stereotype.Controller; 6 import org.springframework.ui.Model; 7 import org.springfra

SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-002设置JPA的EntityManagerFactory(&lt;persistence-unit&gt;、&lt;jee:jndi-lookup&gt;)

一.EntityManagerFactory的种类 1.The JPA specification defines two kinds of entity managers: ? Application-managed—Entity managers are created when an application directly requests one from an entity manager factory. With application-managed entity manage