Java笔记--基于注解的DI 之 @Component和@Value--2021-08-14

11 篇文章 0 订阅

基于注解的DI 之 @Component和@Value

一、@Component

  • @Component 创建对象的,等同于<bean>的功能

属性:value 就是对象的名称,也就是bean的id值 value的值是唯一的,创建的对象在整个spring容器中的就一个
位置:在类的上面

	@Component(value = "myStudent")等同于 \<bean id = "myStudent" class="com.apps.pojo.Student" />

	 spring中和@Component功能一致,创建对象的注解还有:
		 1.@Repository(用在持久层类的上面):放在dao的实现类上面,
		            表示创建dao对象, dao对象是能访问数据库的。
		 2.@Service(用在业务层类的上面):放在service的实现类上面,
		            创建创service对象, service对象是做业务处理,可以有事务等功能的。
		 3.@Controller(用在控制器的上面):放在控制器(处理器)类的上面,创建控制器对象的,
		            控制器对象,能够接受用户提交的参数,显示请求的处理结果。
		            
	 以上三个注解的使用语法和@component一样的。都能创建对象,但是这三个注解还有额外的功能。
	 @Repository,@Service,@Controller是给项目的对象分层的

例子

package com.apps.pojo01;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

/**
 * @author hk Email:2113438464@qq.com
 * @ClassName Student
 * @Description : 实体类
 * @date 2021/6/7
 */

//使用value,指对象名称··
//@Component(value = "myStudent")

//省略value
@Component("myStudent")

//不指定对象名称,由spring提供默认名称:类名的首字母小写
//@Component
public class Student {

    public String name;
    public Integer age;

    public Student() {
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

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

二、@Value

在非引用属性上使用注解@Value,该注解的 value 属性用于指定要注入的值。使用该注解完成属性注入时,类中无需 setter。当然,若属性有 setter,则也可将其加到 setter 上。

package com.apps.pojo02;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author hk Email:2113438464@qq.com
 * @ClassName Student
 * @Description : 实体类
 * @date 2021/6/7
 */

//省略value
@Component("myStudent")
public class Student {

    /**
     * @Value: 简单类型的属性赋值
     */

    @Value("张德帅")
    public String name;

    public Integer age;

    public Student() {
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    @Value(value = "23")
    public void setAge(Integer age) {
        System.out.println("setAge:"+age);
        this.age = age;
    }

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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张德帅-001

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值