spring--bean装配(四)
运行时注入
package com.spring.study.runtime.di;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
private String id;
private String name;
public void printInfo(){
System.out.println("Employee: id:" + id + "," + " name:"+name);
}
}
package com.spring.study.runtime.di;
import org.springframework.beans.factory.annotation.Autowired;
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;
@Configuration
@ComponentScan
@PropertySource("classpath:employee.properties")
public class EmployeeConfig {
@Autowired
private Environment environment;
//外部属性注入避免硬编码
@Bean
public Employee employee(){
return new Employee(environment.getProperty("employee.id"),environment.getProperty("employee.name"));
}
//使用占位符
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
return new PropertySourcesPlaceholderConfigurer();
}
}
package com.spring.study.runtime.di;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Data
@Component
public class Manager {
/**
* @Value(${})代替Environment
*/
@Value("${employee.id}")
private String id;
@Value("${employee.name}")
private String name;
}
package com.spring.study.bean.runtime.di;
import com.spring.study.runtime.di.Employee;
import com.spring.study.runtime.di.EmployeeConfig;
import com.spring.study.runtime.di.Manager;
import lombok.extern.slf4j.Slf4j;
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 = EmployeeConfig.class)
@Slf4j
public class EmployeeTest {
@Autowired
private Employee employee;
@Autowired
private Manager manager;
@Test
public void testEmp(){
employee.printInfo();
}
@Test
public void testManager(){
log.info("manager:{}",manager);
}
}
SpEL表达式
package com.spring.study.runtime.di.spel;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Data
@Component
public class ElDemo {
//EL表达式引用其他bean的属性
//#{systemProperties}引用系统属性
//#{3.14}.#{'hello'} #{true}
//?.
//T()静态方法
@Value("#{employee.name}")
private String name;
}
package com.spring.study.runtime.di.spel;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Data
@Component
public class ElDemo {
//EL表达式引用其他bean的属性
//#{systemProperties}引用系统属性
//#{3.14}.#{'hello'} #{true}
//?.
//T()静态方法
@Value("#{employee.name}")
private String name;
}