【Spring】Spring使用注解

  • 有那几个注解
    @Component
    @Respotory
    @Service
    @Controller
    @Value
    @Autowired
    @Resource

解释各个注解

 Component是参加对象的
 与Component功能一致的,创建对象的注解还有:
 @Repository(持久层):放在dao的实现类上的,表示创建dao对象,dao对象可以访问数据库
 @Service(业务层):是业务处理,有事务功能
 @Controller(控制器上的):可以接收用户提供的参数,显示请求处理的结果
 以上三个注解的使用方法和@Component一样,这三个是给项目分层的,这四个作用和用法是一样的,但是功能不完全一样,因为要考虑到分层

Component的使用


  • 简绍
    创建对象的,等同于bean
    属性:value值是唯一的,创建的对象在整个spring容器中只有一个
    位置在类的上面
    调用的是无参构造

  • 创建测试类
import org.springframework.stereotype.Component;

@Component
public class Student {
    private String name;
    private int age;

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

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

}

  • xml文件,声明组件扫描器
    <!--声明组件扫描器
       用来指定注解在你的项目中的包名
       组件就是Java对象
    -->
    <context:component-scan base-package="spring.ba05注解"/>

  • 测试
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
		String config = "classpath:spring/ac.xml";
		ApplicationContext ac = new ClassPathXmlApplicationContext(config);
		Student student = (Student)ac.getBean("myStudent");
		System.out.println(student.toString());
	}
}

结果
在这里插入图片描述

扫描多个包

    <!--声明组件扫描器
       用来指定注解在你的项目中的包名
    -->
    <context:component-scan base-package="spring.ba05注解"/>

    <!--指定多个包的三种方式:第一种:多次组件扫描器,指定不同的包-->
    <context:component-scan base-package="spring.ba05注解"/>
    <context:component-scan base-package="spring.ba05注解"/>

    <!--使用分割符(;,)-->
    <context:component-scan base-package="spring.ba05注解;spring.ba04_多配置文件"/>

    <!--使用父包-->
    <context:component-scan base-package="spring"/>

value为简单类型属性赋值


  @value:简单属性赋值
  属性:value是String类型,表示简单类型的属性值
  位置:1、在属性定义上面,无需set方法,推荐使用
             2、在set方法上面

  • 测试
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class School {
	/**
	 * {@value:简单属性赋值}
	 * 属性:value是String类型,表示简单类型的属性值
	 * 位置:1、在属性定义上面,无需set方法,推荐使用
	 *       2、在set方法上面
	 */
	
	//address属性不写set方法
	@Value(value = "南京")
	private String address;
	private String name;

	@Override
	public String toString() {
		return "School [address=" + address + ", name=" + name + "]";
	}
	
	public String getName() {
		return name;
	}

	//属性name在set方法上面赋值
	@Value(value = "南大")
	public void setName(String name) {
		this.name = name;
	}
}

实现类略,结果

在这里插入图片描述

为引用类型赋值

autowire


  • Autowire:spring框架提供的注解,实现引用类型的赋值
  • spring中通过注解给引用类型赋值,使用的是自动注入的原理,支持byName,byType
  • 默认使用的是byType自动注入
  • 位置:在属性上使用不需要set方法
  • 在set方法上面使用
  • 还有个属性required,默认是true,意思是当没有赋值成功就报错

  • 测试
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Autowire:spring框架提供的注解,实现引用类型的赋值
 * spring中通过注解给引用类型赋值,使用的是自动注入的原理,支持byName,byType
 * 默认使用的是byType自动注入
 * 位置:在属性上使用不需要set方法
 *      在set方法上面使用
 * @author HR
 *
 */
@Component("stu")
public class Student {
	@Autowired(required = true)
	private School school;
	@Value(value = "张三")
	private String name;

	@Override
	public String toString() {
		return "Student [school=" + school + ", name=" + name + "]";
	}

	public School getSchool() {
		return school;
	}

	public void setSchool(School school) {
		this.school = school;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("sch")
public class School {
	@Value(value = "南京")
	private String address;
	@Value(value = "南大")
	private String name;

	@Override
	public String toString() {
		return "School [address=" + address + ", name=" + name + "]";
	}
}

  • 结果
    在这里插入图片描述

使用autowire的byName类型赋值

只要将@Autowired改为以下即可

	@Autowired
	@Qualifier(value = bean的id)

resources

  • 用法和auto wire差不多,,他是来自jdk中的注解,也是使用自动注入的方式,支持buName和byType默认是byName,当默认的不行就会自动采样bytype,当只使用byName时,需要加个属性name值是id
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值