Spring(五)Spring之注解

你自己理解的注解是什么?

那在Spring中注解的概念又是什么?(通过百度)

注解(Annotation),也叫元数据(MetaData)信息 。一种代码级别的说明。
它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。
它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,
用来对这些元素进行说明,注释

注解的本质其实就是带有@的借口

下面来介绍一下几种注解的作用

@Component     表示一个类是被Spring容器管理的一个Bean

@Value   表示给类的普通属性赋值

@Resource  给类的域属性赋值

@autoWire   给类的域属性赋值

后面两个的作用相同,但是他们来源的包不同

Resource是JDK提供的注解 import javax.annotation.Resource;

Spring提供的注解import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

那么怎么使用注解给类中的属性赋值呢

package demo06;


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

import javax.annotation.Resource;

/**
 * Created by mycom on 2018/3/5.
 */
@Component("student")
public class Student {
    @Value("张三")
    private String name;
    private Integer age;
    //@Resource(name="car")  //Resource是JDK提供的注解  import javax.annotation.Resource;
    @Autowired
    @Qualifier(value = "car") //Spring提供的注解import org.springframework.beans.factory.annotation.Autowired;
                              //import org.springframework.beans.factory.annotation.Qualifier;
    private Car car;

    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;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }
}
package demo06;

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

/**
 * Created by mycom on 2018/3/5.
 */
@Component("car")
public class Car {
    @Value("红色")
    private String color;
    private String brand;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {

        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
    <context:component-scan base-package="demo06"></context:component-scan>


</beans>

彤过编写测试类,运行的结果是:

@Test
    public void t1(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextAnnotation.xml");
        Student student =(Student) context.getBean("student");
        System.out.println(student.getCar().getColor());
    }

原文地址:https://www.cnblogs.com/my-123/p/8521770.html