一、 注解和包扫描
使用注解注入的方式主要是以下的两个注解
@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());
}
}