spring注解驱动第四节之Bean的属性赋值

细说Spring——IoC详解(注解驱动开发之Bean的注入) 一、前言 之前的IoC讲解部分大多是理论内容,感觉缺少一些操作示例,接下来我就会用Spring的注解开发,将一些主要的Spring黑科技展示出来,而所要展示的内容很多,可能一次写不完整,所以分为多篇博客进行讲解。下面我们一起学习一下Spring的注解驱动开发,我是参照尚硅谷的Spring注解驱动开发视频学习的此部分内容,自己实现了所有的代码,而这个视频大家可以在B站看到,也可以去尚硅谷官网下载,... 阅读详情

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
  • 修改User.java,引入配置文件和给属性赋值
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容器初始化-------------------------");
        // 创建一个基于配置类启动的IOC容器,如果主配置类扫描包的路径下包含其他配置类,则其他配置类可以被自动识别
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AnnotationConfiguration.class);
        System.out.println("-----------------------IOC容器初始化完成-------------------------\n");
        // 获取当前IOC中所有bean的名称,即使是懒加载类型的bean也会获取到
        String[] definitionNames = applicationContext.getBeanDefinitionNames();
        // 打印当前IOC中对应名称的bean和bean的类型
        for (String name : definitionNames) {
            // 这个会影响到测试懒加载的效果,如果需要测试懒加载,这行代码需要注释掉,因为getBean方法一旦调用则会初始化
            Object bean = applicationContext.getBean(name);
            System.out.println("bean name:" + name + ", type: " + bean.getClass());
        }

        // 测试@Scope bean的作用域
        testPrototypeScopeService(applicationContext);
        // 测试单实例bean的@Lazy懒加载
        testLazyBeanService(applicationContext);
        // 测试FactoryBean接口导入单实例与Prototype作用域的组件
        testFactoryBeanPrototypeBean(applicationContext);
        // 测试@PropertySource和@Value属性赋值
        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指定销毁方法......
Spring IOC容器中添加bean的几种方式(注解) Spring 注解驱动开发(使用) 一. 注册bean的几种注册方式 1. 通过包扫描(ComponentScan)+注解(@component/@Controller…) ComponentScan 常用属性 basePackages 指定包路径 在指定路径下的带有 @component/@Controller… 注解的类都会被加载进ioc 容器 includeFilters 包含规则... 阅读详情

相关推荐

模拟Spring框架:自定义注解与注解驱动bean实例创建

在Java中,注解是一种特殊的标记接口,用于为代码提供元数据。注解不会直接影响代码的执行,但是可以被编译器或其他工具读取,以执行某些操作或生成额外的代码。注解的语法结构如下:// 属性定义在上述代码中,@interface关键字用于声明一个注解。注解中可以定义多个方法,这些方法定义了注解的属性。属性后面可以使用default关键字来指定默认值,如果在使用注解时不指定该属性的值,则会自动采用默认值。

weixin_36474001的博客 906

给Bean属性赋值(使用注解)

3: 还可以使用${}的方式,取出配置文件[properties]中的值(在运行环境变量中的值)1:可以使用@Value注解直接赋值在需要赋值的属性之上。以上注解声明出内路径,才可赋值,后面是设置编码。2: 也可以使用SpEl方式赋值;使用配置文件的方式赋值需要使用。...

qq_52263468的博客 814

非华为电脑也能用!华为电脑管家完美安装教程来了

GitHub 上有一个开源项目 ​****​,专门解决“非华为设备无法安装华为电脑管家”的问题。它通过智能模拟 + DLL 劫持技术,绕过官方对硬件型号的检测,让你在联想、戴尔、华硕甚至 DIY 主机上也能流畅使用华为电脑管家!⭐ 项目已获 ​700+ Stars​,社区活跃,持续更新,兼容性优秀!科技不该有围墙。如果你成功在非华为电脑上跑起了 PCManager,欢迎在评论区晒出你的设备型号 + 使用体验!也别忘了把这篇推文分享给同样“眼馋”华为生态的朋友 ~📱 ​关注我们。

程序员朱永胜 4925

spring源码:bean标签的解析之设置BeanDefinition的属性值

一、目的   BeanDefinition是spring中对bean的一个抽象表示,bean的各种信息在BeanDefinition中都有对应的属性表示。本章分析的内容比较简单,就是在创建BeanDefinition之后,如何给BeanDefinition中的各种属性设置值。 二、相关类 org.springframework.beans.factory.xml.BeanDefinitionPa...

咔咔学长 1327

Fanuc机器人的坐标变换矩阵的研究2

一、Funuc用户坐标系的介绍 二、用户坐标系与世界坐标系的坐标转换 三、 以用户坐标系1为基础旋转变换后的用户坐标系 四、关于点的WPR的含义 4.1:点在用户坐标系和世界坐标系下的坐标关系 4.2 通过转换矩阵返回去推导WPR

qq_34047402的博客 1524

告别CPU空转!用STC8的SPI+DMA高效驱动上百颗WS2812B灯珠

本文详细解析了如何利用STC8单片机的SPI+DMA外设高效驱动WS2812B全彩灯珠,解决传统软件延时驱动方式在长灯带应用中的性能瓶颈。通过硬件方案设计、数据格式转换和DMA传输配置,实现高达400Hz的刷新率和低于5%的CPU占用率,适用于大规模灯光项目。

weixin_30847939的博客 396

Spring - (2) bean属性赋值

一.依赖注入的方式 1.通过bean的setXxx()方法赋值 2.通过bean的构造器赋值 Spring自动匹配合适的构造器 通过索引值指定参数位置 通过类型区分重载的构造器 xml中构造器参数的配置会自动找寻Java代码中构造器最后出现且符合要求的构造器 二.p名称空间 为了简化XML文件的配置,越来越多的XML文件采用属性而非子元素配置信息。Spring从2.5版本开始引入了一个新的p命...

myllxy 1071

阿里云百炼平台创建智能体-上传文档

阿里云百炼智能体-上传文档文件

weixin_43930851的博客 1356

spring注解驱动开发-3 Spring bean的属性赋值

Spring Bean的生命周期前言Spring bean的属性赋值1.XML方式1.@Value方式 前言 bean的属性赋值是指给注入到IOC容器中的bean赋值,本文讲解了通过@Value的方式为bean赋值,补充讲解了springboot自动配置中的属性赋值,使用此方式可以非常方便的在yml文件中配置自己的属性值。文章课程链接:尚硅谷spring注解驱动教程(雷神) Spring bean的属性赋值 1.XML方式 例子: xml方式是在property 标签中指定value属性。 <be

weixin_44227063的博客 734

用Stata复现经典文献:上市公司全要素生产率(TFP)计算全流程(含LP/OP代码解读)

本文提供了使用Stata软件复现上市公司全要素生产率(TFP)计算的完整实战指南。重点解读了主流的Levinsohn-Petrin(LP)和Olley-Pakes(OP)半参数估计方法,通过详细的数据清洗、变量构建与价格平减步骤,手把手演示了如何准确测算TFP,并强调了LP方法在处理中国上市公司数据时的稳定性和适用性。

b0c1d2的博客 595

【数据分享】2012-2022年全国及各城市POI(兴趣点)数据(超20G)

POI(一般作为Point of Interest的缩写,也有Point of Information的说法),通常称作兴趣点,泛指互联网电子地图中的点类数据,基本包含名称、地址、坐标、类别四个属性;随着互联网电子地图服务与LBS应用的普及,POI无论从概念范畴,还是从信息纵深都有了长足发展,互联网各个风口或火山口都和POI有一定关系,如O2O、电商、社交、互联网金融、共享经济等。

J_Giser的博客 2738

springboot源码分析之beanDefinitionNames

可以看到如果是实现了ImportBeanDefinitionRegistrar的类,都会进入这个方法,也会进入我们自定义的类 这个就是自动装配。然后走完刷新上下文的方法,我们可以看到上下文的beanFactory的beanDifinitionNames就有132条数据了。只不过我们的postProcessor换成了箭头指向的位置的类,我们进入他的类的这个方法。然后启动完成之后我们,在看下数据,可以看到凡是带注解的和我们自动装配的类都获取到了。我们可以看下basePackages的数据,就是我们的包的路径。

qq_21277357的博客 874

从 SAP HANA 到 ABAP Cloud:彻底讲透 scalar function 的定位、实现方式与 Clean Core 实践

SAP项目中,scalar function是数据库层的重要计算单元,用于封装可复用的单值逻辑。不同于返回结果集的table function,它直接嵌入SQL表达式,适用于折扣率计算、数据标准化等场景。在SAP技术栈中,scalar function有三种形态:原生SQLScript函数、AMDP托管函数和CDS绑定函数。正确使用可提升查询性能,但需注意与procedure、table function的边界区分。AMDP scalar function是ABAP项目中的推荐实现方式,能确保函数生命周期受

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1677

Spring注解驱动开发第18讲——如何使用@Value注解为bean的属性赋值呢?

写在前面 在之前的文章中,我们探讨了如何向Spring的IOC容器中注册bean组件,并且还讲解了有关bean组件的生命周期的知识。今天,我们就来一起聊聊@Value注解的用法。 @Value注解 Spring中的@Value注解可以为bean中的属性赋值。我们先来看看@Value注解的源码,如下所示。 从@Value注解的源码中我们可以看出,@Value注解可以标注在字段、方法、参数以及注解上,而且在程序运行期间生效。 @Value注解的用法 不通过配置文件注入属性的情况 通过@Value注解将外部的值

李阿昀的博客 4916

注意力机制篇 | YOLOv8改进之在C2f模块添加EMA注意力机制(附2种改进方法)

EMA(Exponential Moving Average)注意力机制是一种用于增强模型性能的注意力机制,它通过对模型的特征图进行加权平均来提取更有用的特征信息。具体来说,EMA注意力机制通过引入一个权重因子来调整特征图中每个位置的重要性,从而使模型能够更好地关注重要的特征。🌈

小哥谈 4180

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

【Spring注解驱动开发】如何使用@Value注解为bean的属性赋值,我们一起吊打面试官!

写在前面 在之前的文章中,我们探讨了如何向Spring的IOC容器中注册bean组件,讲解了有关bean组件的生命周期的知识。今天,我们就来一起聊聊@Value注解的用法。 项目工程源码已经提交到GitHub:https://github.com/sunshinelyz/spring-annotation @Value注解 Spring中的@Value注解可以为bean中的属性赋值。我们先来看看@Value注解的源码,如下所示。 package org.springframework.beans.fa

冰河的专栏 2156

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Spring注解驱动开发——给bean的属性复制

属性赋值:Bean的属性上使用@Value注解:必须是由容器调用构造器创建的bean这些@Value中的属性值才会被注入,自己显示写了new方法的@Value属性就不起作用了,因为自己写了new方法则实例不是由容器创建 public class Person { @Value("张三丰") private String name; @Value("#{20-2}") private ...

如不來的博客 408

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Spring注解驱动开发

Spring注解驱动开发

weixin_52055811的博客 1501

学习springboot必备---spring注解驱动开发3-bean的属性赋值

1

jsty181818的博客 364

Spring注解驱动开发之常用注解案例_告别在XML中配置Bean

SpSpring注解驱动开发之常用注解案例_告别在XML中配置Bean

yuanjinshenglife的专栏 1174

Spring注解10——用@value注解为bean的属性赋值

该系列文章主要学习 雷丰阳老师的《Spring注解驱动》课程总结。 原课程地址:课程地址 包括了自己阅读其他书籍《Spring揭秘》《Spring Boot 实战》等课程。 该系列文档会不断的完善,欢迎大家留言及提意见。 文章目录@value 作用 @value 作用 给 Bean 属性注入常量值。 一般有几种使用方式 1、直接注入常量值 @Configuration public class User { @Value("张三") private String name; .

大黄奔跑 618

Spring注解驱动的bean属性赋值和自动装配

@PropertySource和@Value的使用例子 import com.atguigu.bean.Person; //使用@PropertySource读取外部配置文件中的k/v保存到运行的环境变量中;加载完外部的配置文件以后使用${}取出配置文件的值 @PropertySource(value={"classpath:/person.properties"}) @Configuration public class MainConfigOfPropertyValues { @Bea...

zzd的博客 211

Spring注解驱动开发学习总结7:属性赋值@Value详解

Spring注解驱动开发学习总结7:属性赋值@Value详解1、属性赋值1.1 创建配置文件1.2 Person类添加属性赋值1.3 新建配置文件1.4 测试 1、属性赋值 给bean的属性赋值,可以使用@Value注解添加属性初始值。 1)可以添加基本数值 2)可以写Spring的SpEL表达式:#{} 3)可以写${},取出配置文件[properties]中的值。同时需要在配置文件上使用@PropertySource注解,标明配置文件的类路径。 以下还是以之前创建的Person类进行举例,来给属性赋值。

xueping_wu的博客 827

Spring注解驱动(二)生命周期和属性赋值

一、springbean的生命周期 1、bean生命周期包含的内容模块 bean的生命周期:bean创建-------初始化------销毁的过程 bean创建:构造对象 单实例:在容器启动的时候创建对象。 多实例:在每次获取的时候创建对象。 bean初始化:对象创建完成,并赋值好,调用初始化方法。 bean销毁:单实例:容器关闭的时候销毁。 多实例:容器不会管理这个bean,...

root 330

Spring注解驱动开发 第十一节默认赋值注解

Spring注解驱动开发 第十一节默认赋值注解

William_HoF的博客 632
上一篇: spring注解驱动第三节之Bean的生命周期
下一篇: spring注解驱动第五节之Bean的依赖注入
DDF_YiChen
博客等级 码龄12年 59粉丝 76原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值