Spring Bean学习笔记--使用注解的方式实现依赖注入

一、 注解和包扫描

使用注解注入的方式主要是以下的两个注解
@Component从源码可以看出@Component主要用于类上面的注解。

@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Documented
@org.springframework.stereotype.Indexed
public @interface Component {
    java.lang.String value() default "";
}

@Autowire 从源代码的元注解@Tartget中我们可以看到这个注解可以在Constructor、Method、Field上面添加。

package org.springframework.beans.factory.annotation;

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {

    /**
     * Declares whether the annotated dependency is required.
     * <p>Defaults to {@code true}.
     */
    boolean required() default true;

}

classpath-scanning从某一顶级包开始扫描。当扫描到某个类上面的时的注解时,就会提取该类的信息,构建对应的BeanDefinition,然后把BeanDefinition注册到容器当中。
classpath-scanning实现的方式是在配置文件中添加下面的语句,这个配置主要是扫描@Component注解,但是现在也可以用来扫描@Repository、@Service、@Controller注解

<context:component-scan base-package="?"/>//在问号处填写你要扫描的包。

如果我们不想扫描一些包,那么采用下面的方式进行扫描

<context:component-scan base-package="?"/>
    <context:include-filter expression="?"/>
    <context:exclude-filter expression="?"/>
</context:component-scan>

二、 案例

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

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

    public Student(){}

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

配置文档

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.zbt.springbean_day02"/>
</beans>

测试代码

package com.zbt.springbean_day03;

import com.zbt.springbean_day02.Student;
import com.zbt.springbean_day02.StudentDao;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;

/**
 * Created by luckyboy on 2018/8/18.
 */
public class StudentTest {
    @Test
    public void test1(){
        ClassPathResource resource = new ClassPathResource("contextConfig.xml");
        DefaultListableBeanFactory container = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(container);
        reader.loadBeanDefinitions(resource);
        StudentDao stuDao = (StudentDao) container.getBean("studentDao");
        Student stu = stuDao.getStudent();
        System.out.println(stu.getName());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值