@Aurowired:自动注入
在对象类型上使用@Autowired,该注解默认使用 按类型自动装配Bean的方式
联合使用@Autowired与@Qualifier,
@Qualifier的value属性用于指定要匹配的Bean的id值
JDK注解@Resource:自动注入
Spring提供了对jdk中@Resource注解的支持
@Resource:可以按名称匹配也可以安装类型匹配,默认是按名称匹配的
导入jar包
编写School类和Student类
Student.java
package cn.lexed.pojo;
import javax.annotation.Resource;
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;
@Component("stu")
public class Student {
@Value("李四")
private String name;
@Value("21")
private int age;
//@Autowired
//@Qualifier("scc")
@Resource(name="scc")
private School sc;//name address
public Student() {
super();
}
public Student(String name, int age, School sc) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public School getSc() {
return sc;
}
public void setSc(School sc) {
this.sc = sc;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sc=" + sc + "]";
}
}
School.java
package cn.lexed.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("scc")
public class School {
@Value("哈工大")
private String name;
@Value("qqqqqqq")
private String address;
public School() {
super();
}
public String getName() {
return name;
}
/*public void setName(String name) {
this.name = name;
}*/
public String getAddress() {
return address;
}
/*public void setAddress(String address) {
this.address = address;
}*/
@Override
public String toString() {
return "School [name=" + name + ", address=" + address + "]";
}
}
编写配置文件
组件扫描器,用于在指定的包中扫描你定义的注解
base-package属性:值是包名,容器启动会扫描包中以及子包中的注解
app.xml
<?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-3.2.xsd">
<!-- 组件扫描器,用于在指定的包中扫描你定义的注解
base-package属性:值是包名,容器启动会扫描包中以及子包中的注解
-->
<context:component-scan base-package="cn.lexed.pojo"></context:component-scan>
</beans>
编写测试类
TestSpring.java
package cn.lexed.test;
import static org.junit.Assert.*;
import java.util.Date;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.lexed.pojo.Student;
public class TestSpring {
@Test
public void test() {
//1.创建Spring容器的对象
ApplicationContext ac=new ClassPathXmlApplicationContext("app.xml");
//2.使用getBean方法,getBean(配置文件中bean的id值)
Student s1=(Student)ac.getBean("stu");
System.out.println(s1);
}
}