首页 > 其他分享 >spring set注入-null和空字符串

spring set注入-null和空字符串

时间:2022-11-13 11:00:42浏览次数:39  
标签:set name spring age Cat null public String

举例说明:

Cat类

package per.sxhzs.spring6.bean;

public class Cat {
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

配置文件set-di.xml

<bean id="catBean" class="per.sxhzs.spring6.bean.Cat">
        <!--不给属性注入,属性的默认值就是null-->
        <!--<property name="name" value="hxh"/>-->

        <!--这不是注入null,这只是注入了一个“null“字符串-->
        <!--<property name="name" value="null"/>-->

        <!--这种方式是手动注入null-->
        <property name="name">
            <null/>
        </property>
        <property name="age" value="20"/>
    </bean>

测试类

@Test
    public void testNull() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
        Cat catBean = applicationContext.getBean("catBean", Cat.class);
        System.out.println(catBean);
    }

运行结果

标签:set,name,spring,age,Cat,null,public,String
From: https://www.cnblogs.com/sxhxh/p/16885569.html

相关文章

  • 第6章Spring与Web-使用 Spring 的监听器 ContextLoaderListener使得将spring容器对象
    第6章Spring与Web在Web项目中使用Spring框架,首先要解决在web层(这里指Servlet)中获取到Spring容器的问题。只要在web层获取到了Spring容器,便可从容器中获取到......
  • SpringBoot注入
    SpringBoot属性注入涉及注解:@Configuration:声明一个类作为配置类@Bean:声明在方法上,将方法的返回值加入Bean容器@Value:属性注入@ConfigurationProperties(prefix=“j......
  • 第5章Spring 事务(测试)-Spring的事务注解(小项目中),AspectJ的AOP配置管理事务(大项目中)
    第5章Spring事务(测试)-Spring的事务注解(小项目中),AspectJ的AOP配置管理事务(大项目中)spring框架中提供的事务处理方案适合中小项目使用的,注解方案。1.适合中小项目使用......
  • Spring Cloud Loadbalancer
    SpringCloudLoadbalancer---客户端负载均衡器springcloud2020.0.1版本之后删除了eureka中的ribbon,替代ribbon的是springcloud自带的LoadBalancer,但公司开发中并没有那......
  • Spring Cloud Loadbalancer
    SpringCloudLoadbalancer---客户端负载均衡器springcloud2020.0.1版本之后删除了eureka中的ribbon,替代ribbon的是springcloud自带的LoadBalancer,但公司开发中并没有......
  • Spring 事务(测试)--在这个笔记中记录的是没有添加事务,数据库返回的效果。
    第5章Spring事务(测试)--在这个笔记中记录的是没有添加事务,数据库返回的效果。1.首先搞两张表,商品表和订单表举例:购买商品trans_sale项目本例要实现购买商品,模拟用......
  • SpringMVC-解析@ResponseBody
    ServletInvocableHandlerMethod.invokeAndHandle处理完request得到结果后调用returnValueHandlers.handleReturnValue处理返回值。HandlerMethodReturnValueHandlerCompos......
  • spring底层核心概念解析
    1.BeanDefinition包含bean的一些基本元信息,如bean的类型,作用域,初始化方法...等等。申明式的定义,如@Bean,等等<beanclass="com.test.service.UserService"id="userSe......
  • Redis集合(Set)
    简介Redisset对外提供的功能与list类似是一个列表的功能,特殊之处在于set是可以自动排重的,当你需要存储一个列表数据,又不希望出现重复数据时,set是一个很好的选择,并且set提......
  • Redis有序集合Zset(sorted set)
    Redis有序集合zset与普通集合set非常相似,是一个没有重复元素的字符串集合。不同之处是有序集合的每个成员都关联了一个评分(score),这个评分(score)被用来按照从最低分到最高......