4. Bean
的属性赋值
4.1 @Value
与@PropertySource
@Value
可以标注在类的字段上,以表示该该字段赋值,可以直接使用目标值,支持SPEl
,也支持外部配置文件加载
@PropertySource
可以标注在类上,value
可以指定一个资源文件,则可以导入该文件以便解析读取
- 在
src/main/resources
下新建一个配置文件User.properties
,用来给User
初始化赋值
user.id=1
user.userName=ddf
user.password=123456
user.tel=18356785555
package com.ddf.spring.annotation.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @author DDf on 2018/7/19
* 使用@PropertySource引入外部配置文件
* @Component或@Configuration将该类交由IOC容器保管(这里有一个很奇怪的地方,明明使用了@Bean在配置类中已经注入了这个Bean,但是如果
* 这里只使用了@PropertySource依然无法对User赋值,所以这里需要再加上一个@Component,很奇怪)
*/
@PropertySource("classpath:User.properties")
@Component
public class User {
@Value("${user.id}")
private Integer id;
@Value("${user.userName}")
private String userName;
@Value("${user.password}")
private String password;
@Value("${user.tel}")
private String tel;
@Value("用户数据")
private String defaultMessage;
public void init() {
System.out.println("User创建后调用初始化方法..........");
}
public void destory() {
System.out.println("User销毁后调用销毁方法....通过@Bean的destoryMethod指定销毁方法......");
}
public User() {
System.out.println("User创建完成...通过@Bean的initMethod调用初始化方法............");
}
public User(Integer id, String userName, String password, String tel, String defaultMessage) {
this.id = id;
this.userName = userName;
this.password = password;
this.tel = tel;
this.defaultMessage = defaultMessage;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getDefaultMessage() {
return defaultMessage;
}
public void setDefaultMessage(String defaultMessage) {
this.defaultMessage = defaultMessage;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", tel='" + tel + '\'' +
", defaultMessage='" + defaultMessage + '\'' +
'}';
}
}
- 修改
Application.java
,增加测试代码testPropertySourceValue
package com.ddf.spring.annotation;
import com.ddf.spring.annotation.service.*;
import com.ddf.spring.annotation.configuration.AnnotationConfiguration;
import com.ddf.spring.annotation.entity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author DDf on 2018/7/19
*/
public class Application {
public static void main(String[] args) {
System.out.println("-----------------------IOC容器初始化-------------------------");
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AnnotationConfiguration.class);
System.out.println("-----------------------IOC容器初始化完成-------------------------\n");
String[] definitionNames = applicationContext.getBeanDefinitionNames();
for (String name : definitionNames) {
Object bean = applicationContext.getBean(name);
System.out.println("bean name:" + name + ", type: " + bean.getClass());
}
testPrototypeScopeService(applicationContext);
testLazyBeanService(applicationContext);
testFactoryBeanPrototypeBean(applicationContext);
testPropertySourceValue(applicationContext);
applicationContext.close();
}
/**
* 测试@Scope bean的作用域
*
* @param applicationContext
*/
public static void testPrototypeScopeService(ApplicationContext applicationContext) {
System.out.println("\n-----------------------测试@Scope开始-------------------------");
UserService userService = (UserService) applicationContext.getBean("userService");
UserService userService1 = applicationContext.getBean(UserService.class);
System.out.println("默认单实例bean UserService是否相等 " + (userService == userService1));
PrototypeScopeService prototypeScopeService = applicationContext.getBean(PrototypeScopeService.class);
PrototypeScopeService prototypeScopeService1 = applicationContext.getBean(PrototypeScopeService.class);
System.out.println("PrototypeScopeService prototype scope作用域是否相等: " + (prototypeScopeService == prototypeScopeService1));
System.out.println("-----------------------测试@Scope结束-------------------------\n");
}
/**
* 测试单实例bean的懒加载,只有等使用的时候再创建实例。
* IOC容器启动后不会创建该bean的实例,如果是在该方法中才创建这个bean的实例,并且获得的两个bean是同一个的话,则测试通过。
*/
public static void testLazyBeanService(ApplicationContext applicationContext) {
System.out.println("\n---------------测试单实例bean的@Lazy懒加载开始----------------------");
LazyBeanService lazyBeanService = applicationContext.getBean(LazyBeanService.class);
LazyBeanService lazyBeanService1 = applicationContext.getBean(LazyBeanService.class);
System.out.println("lazyBeanService==lazyBeanService1?: " + (lazyBeanService == lazyBeanService1));
System.out.println("---------------测试单实例bean的@Lazy懒加载结束----------------------\n");
}
/**
* 测试通过FactoryBean接口导入单实例与Prototype作用域的组件,根据打印可以看出FactoryBean创建的单实例Bean都是懒加载的
* @param applicationContext
*/
public static void testFactoryBeanPrototypeBean(ApplicationContext applicationContext) {
System.out.println("\n----------测试通过FactoryBean注册单实例和Prototype作用域的组件开始----------");
FactorySingletonBean factorySingletonBean = applicationContext.getBean(FactorySingletonBean.class);
FactorySingletonBean factorySingletonBean1 = applicationContext.getBean(FactorySingletonBean.class);
FactoryPrototypeBean factoryPrototypeBean = applicationContext.getBean(FactoryPrototypeBean.class);
FactoryPrototypeBean factoryPrototypeBean1 = applicationContext.getBean(FactoryPrototypeBean.class);
System.out.println("单实例factorySingletonBean==factorySingletonBean1?" + (factorySingletonBean==factorySingletonBean1));
System.out.println("Prototype作用域factoryPrototypeBean==factoryPrototypeBean1?" + (factoryPrototypeBean==factoryPrototypeBean1));
System.out.println("----------测试通过FactoryBean注册单实例和Prototype作用域的组件结束----------\n");
}
/**
* 测试通过@PropertySource和@Value注解来对属性进行赋值
* @param applicationContext
*/
public static void testPropertySourceValue(ApplicationContext applicationContext) {
System.out.println("\n---------------测试@PropertySource和@Value赋值开始----------------");
User user = applicationContext.getBean(User.class);
System.out.println("user属性为: " + user.toString());
System.out.println("---------------测试@PropertySource和@Value赋值结束----------------\n");
}
}
-----------------------IOC容器初始化-------------------------
八月 04, 2018 7:14:19 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5197848c: startup date [Sat Aug 04 19:14:19 CST 2018]; root of context hierarchy
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.Application
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.configuration.ApplicationContextUtil
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.configuration.CustomBeanPostProcessor
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.configuration.CustomImportBeanDefinitionRegistrar
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.configuration.CustomImportSelector
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.configuration.DevelopmentProfileCondition
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.configuration.ExcludeTypeFilter
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.configuration.FactoryPrototypeBeanConfiguration
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.configuration.FactorySingletonBeanConfiguration
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.configuration.ProductionProfileCondition
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.controller.UserController
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.entity.User
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.bean.DevelopmentBean
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.bean.DevelopmentBeanLog
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.service.ExcludeFilterService
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.bean.FactoryBean
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.bean.FactoryPrototypeBean
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.bean.FactorySingletonBean
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.bean.ImportBean
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.bean.ImportBeanDefinitionRegistrarBean
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.bean.ImportSelectorBean
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.service.IncludeFilterService
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.bean.InitAndDisposableBean
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.service.LazyBeanService
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.bean.PostConstructAndPreDestoryBean
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.bean.ProductionBean
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.bean.ProductionBeanLog
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.service.PrototypeScopeService
自定义扫描类规则当前扫描类为: com.ddf.spring.annotation.service.UserService
八月 04, 2018 7:14:19 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
信息: Overriding bean definition for bean 'user' with a different definition: replacing [Generic bean: class [com.ddf.spring.annotation.entity.User]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [J:\dev-tools\idea-root\spring-annotation\target\classes\com\ddf\spring\annotation\entity\User.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=annotationConfiguration; factoryMethodName=user; initMethodName=init; destroyMethodName=destory; defined in com.ddf.spring.annotation.configuration.AnnotationConfiguration]
DevelopmentProfileCondition profile: dev
ProductionProfileCondition profile: dev
八月 04, 2018 7:14:19 下午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【org.springframework.context.event.EventListenerMethodProcessor@b7f23d9】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【org.springframework.context.event.EventListenerMethodProcessor@b7f23d9】
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【org.springframework.context.event.DefaultEventListenerFactory@69b794e2】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【org.springframework.context.event.DefaultEventListenerFactory@69b794e2】
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.configuration.AnnotationConfiguration$$EnhancerBySpringCGLIB$$c59c92dd@4d339552】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.configuration.AnnotationConfiguration$$EnhancerBySpringCGLIB$$c59c92dd@4d339552】
解析的字符串:你好 Windows 10 我是 360
com.ddf.spring.annotation.configuration.ApplicationContextUtil@45018215
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.configuration.ApplicationContextUtil@45018215】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.configuration.ApplicationContextUtil@45018215】
User创建完成...通过@Bean的initMethod调用初始化方法............
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【User{id=1, userName='ddf', password='123456', tel='18356785555', defaultMessage='用户数据'}】
User创建后调用初始化方法..........
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【User{id=1, userName='ddf', password='123456', tel='18356785555', defaultMessage='用户数据'}】
UserService创建完成...................
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.service.UserService@47d90b9e】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.service.UserService@47d90b9e】
ImportBean创建完成(测试@Import导入组件)..........
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.ImportBean@1184ab05】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.ImportBean@1184ab05】
ImportSelectorBean创建完成,测试@Import通过ImportSelector接口导入组件
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.ImportSelectorBean@3aefe5e5】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.ImportSelectorBean@3aefe5e5】
DevelopmentBeanLog创建完成.......测试@Condition........
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.DevelopmentBeanLog@149e0f5d】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.DevelopmentBeanLog@149e0f5d】
DevelopmentBean创建完成.....测试@Condition........
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.DevelopmentBean@1b1473ab】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.DevelopmentBean@1b1473ab】
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.configuration.FactoryPrototypeBeanConfiguration@ef9296d】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.configuration.FactoryPrototypeBeanConfiguration@ef9296d】
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.configuration.FactorySingletonBeanConfiguration@1c93084c】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.configuration.FactorySingletonBeanConfiguration@1c93084c】
InitAndDisposableBean创建完成。。。。。。。。。。。。
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.InitAndDisposableBean@7ce3cb8e】
InitAndDisposableBean创建后实现InitializingBean调用初始化方法。。。。。。。。。。。。
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.InitAndDisposableBean@7ce3cb8e】
PostConstructAndPreDestoryBean创建完成.......
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.PostConstructAndPreDestoryBean@22a637e7】
PostConstructAndPreDestoryBean创建完成,使用@PostConstruct注解来调用初始化方法。。。。
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.PostConstructAndPreDestoryBean@22a637e7】
ImportBeanDefinitionRegistrarBean创建完成,测试@Import接口通过ImportBeanDefinitionRegistrar接口注入组件。。。。。
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.ImportBeanDefinitionRegistrarBean@6fe7aac8】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.ImportBeanDefinitionRegistrarBean@6fe7aac8】
-----------------------IOC容器初始化完成-------------------------
bean name:org.springframework.context.annotation.internalConfigurationAnnotationProcessor, type: class org.springframework.context.annotation.ConfigurationClassPostProcessor
bean name:org.springframework.context.annotation.internalAutowiredAnnotationProcessor, type: class org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
bean name:org.springframework.context.annotation.internalRequiredAnnotationProcessor, type: class org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
bean name:org.springframework.context.annotation.internalCommonAnnotationProcessor, type: class org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
bean name:org.springframework.context.event.internalEventListenerProcessor, type: class org.springframework.context.event.EventListenerMethodProcessor
bean name:org.springframework.context.event.internalEventListenerFactory, type: class org.springframework.context.event.DefaultEventListenerFactory
bean name:annotationConfiguration, type: class com.ddf.spring.annotation.configuration.AnnotationConfiguration$$EnhancerBySpringCGLIB$$c59c92dd
bean name:applicationContextUtil, type: class com.ddf.spring.annotation.configuration.ApplicationContextUtil
bean name:customBeanPostProcessor, type: class com.ddf.spring.annotation.configuration.CustomBeanPostProcessor
bean name:user, type: class com.ddf.spring.annotation.entity.User
LazyBeanService创建完成...............
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.service.LazyBeanService@1700915】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.service.LazyBeanService@1700915】
bean name:lazyBeanService, type: class com.ddf.spring.annotation.service.LazyBeanService
PrototypeScopeService创建完成。。。。。。。。
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.service.PrototypeScopeService@21de60b4】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.service.PrototypeScopeService@21de60b4】
bean name:prototypeScopeService, type: class com.ddf.spring.annotation.service.PrototypeScopeService
bean name:userService, type: class com.ddf.spring.annotation.service.UserService
bean name:com.ddf.spring.annotation.bean.ImportBean, type: class com.ddf.spring.annotation.bean.ImportBean
bean name:com.ddf.spring.annotation.bean.ImportSelectorBean, type: class com.ddf.spring.annotation.bean.ImportSelectorBean
bean name:DevelopmentBeanLog, type: class com.ddf.spring.annotation.bean.DevelopmentBeanLog
bean name:DevelopmentBean, type: class com.ddf.spring.annotation.bean.DevelopmentBean
FactoryPrototypeBean创建完成....,测试通过FactoryBean来注册Prototype组件。。。。
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.FactoryPrototypeBean@c267ef4】
bean name:factoryPrototypeBeanConfiguration, type: class com.ddf.spring.annotation.bean.FactoryPrototypeBean
FactorySingletonBean创建完成。。。。,测试通过FactoryBean来注册单实例组件。。。。
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.FactorySingletonBean@30ee2816】
bean name:factorySingletonBeanConfiguration, type: class com.ddf.spring.annotation.bean.FactorySingletonBean
bean name:initAndDisposableBean, type: class com.ddf.spring.annotation.bean.InitAndDisposableBean
bean name:postConstructAndPreDestoryBean, type: class com.ddf.spring.annotation.bean.PostConstructAndPreDestoryBean
bean name:importBeanDefinitionRegistrarBean, type: class com.ddf.spring.annotation.bean.ImportBeanDefinitionRegistrarBean
-----------------------测试@Scope开始-------------------------
默认单实例bean UserService是否相等 true
PrototypeScopeService创建完成。。。。。。。。
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.service.PrototypeScopeService@7a69b07】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.service.PrototypeScopeService@7a69b07】
PrototypeScopeService创建完成。。。。。。。。
BeanPostProcessor的postProcessBeforeInitialization方法执行,当前bean【com.ddf.spring.annotation.service.PrototypeScopeService@5e82df6a】
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.service.PrototypeScopeService@5e82df6a】
PrototypeScopeService prototype scope作用域是否相等: false
-----------------------测试@Scope结束-------------------------
---------------测试单实例bean的@Lazy懒加载开始----------------------
lazyBeanService==lazyBeanService1?: true
---------------测试单实例bean的@Lazy懒加载结束----------------------
----------测试通过FactoryBean注册单实例和Prototype作用域的组件开始----------
FactoryPrototypeBean创建完成....,测试通过FactoryBean来注册Prototype组件。。。。
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.FactoryPrototypeBean@3f197a46】
FactoryPrototypeBean创建完成....,测试通过FactoryBean来注册Prototype组件。。。。
BeanPostProcessor的postProcessAfterInitialization方法执行,当前bean【com.ddf.spring.annotation.bean.FactoryPrototypeBean@636be97c】
单实例factorySingletonBean==factorySingletonBean1?true
Prototype作用域factoryPrototypeBean==factoryPrototypeBean1?false
----------测试通过FactoryBean注册单实例和Prototype作用域的组件结束----------
---------------测试@PropertySource和@Value赋值开始----------------
user属性为: User{id=1, userName='ddf', password='123456', tel='18356785555', defaultMessage='用户数据'}
---------------测试@PropertySource和@Value赋值结束----------------
PostConstructAndPreDestoryBean容器销毁,使用@PreDestroy注解来指定调用销毁方法。。。。
InitAndDisposableBean容器销毁,实现DisposableBean接口调用销毁方法...........
User销毁后调用销毁方法....通过@Bean的destoryMethod指定销毁方法......