知识点:
1) @Component使用之后需要在xml文件配置一个标签:`<context:component-scan/>`
2) @Component注解可以直接定义bean,而无需在xml定义bean。但是若两种定义同时存在,xml中的定义会覆盖类中注解的Bean定义(走无参构造)
3) @Component注解`直接写在类上面`即可
4) @Component如果`不指定参数,则bean的名称为当前类的类名首字母小写,其余的不变`
@Component//相当于Boss boss=new Boss();
public class Boss{
}
5) @Component有一个`可选的参数,用于指定bean的名称`
@Component("car1")//相当于Car car1=new Car();
public class Car{
}
@Autowired
@Qualifier("car1")//注入的是car1
public class Boss{
private Car car;
}
6) <context:component-scan base-package="com.briup.ioc.annotation" />
表示spring会检查`指定包下的java类,看它们是否使用了@Component注解`
7) @Component定义的bean默认情况下都是`单例模式`的,如果要让这个bean变为非单例,但是可以再结合这个@Scope注解来达到目标,@Scope("prototype")
@Component是Spring中所有bean组件的通用形式, 而@Repository @Service @Controller
则是@Component的细化,用来表示更具体的用例,分别对应了持久化层、服务层和表现层。
但是至少到现在为止这个四种注解的实质区别很小(甚至几乎没有),都是把当前类注册为spring容器中的一个bean
注意:
1.component-scan标签默认情况下自动扫描指定路径下的包(含所有子包)
2.component-scan标签将带有@Component @Repository @Service @Controller注解的类自动注册到spring容器中
3.component-scan标签对标记了@Required @Autowired @PostConstruct @PreDestroy @Resource @WebServiceRef @EJB @PersistenceContext @PersistenceUnit等注解的类进行对应的操作,使注解生效
4.`component-scan标签包含了annotation-config标签的作用`,所以只需要在xml文件中`只写<context:component-scan>,无需再写<context:annotation-config/>`
简单的大概例子:
@Component("car")
public class Car{ }
@Component("office")
public class Office{ }
@Component("boss")
public Boss{
@Autowired
private Car car;
@Autowired
private Office office;
//假设直接从数据库中获得值,就不用再new对象了,因为已经走了无参构造new好了对象
public void test(){
car.setName("奥迪");
office.setNum("002");
}
}
再加上扫描注解的标签,当前内存中有了car对象,office对象,boss对象,对应的注入也没有问题
使用注解记得走无参构造器初始化对象`(要想给car里面加name值和price值,就必须用xml的配置文件,<bean>里面的<property>具体设置属性值)`
annotation.xml
只需要配置@component 初始化对象要用的context:component-scan和@Autowired 自动注入要用的context:annotation-config/
<?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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- @component 初始化对象要用的-->
<context:component-scan base-package="com.briup.ioc.annotation" />
<!-- @Autowired 自动注入要用的context:annotation-config被包含了,不需要写<context:annotation-config/>-->
</beans>
3个实体类
/com.briup.ioc.annotation.Office.java
package com.briup.ioc.annotation;
import org.springframework.stereotype.Component;
@Component
public class Office {
private String num = "001";
public Office(){
}
public Office(String num) {
this.num = num;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
}
/com.briup.ioc.annotation.Car.java
package com.briup.ioc.annotation;
import org.springframework.stereotype.Component;
//@Component("car1")
@Component //等同于Car car=new Car();等同于在配置文件中配置的bean
public class Car {
private double price;
private String name;
public Car(){
}
public Car(double price, String name) {
this.price = price;
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/com.briup.ioc.annotation.Boss.java
package com.briup.ioc.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Boss {
private String name;
@Autowired
private Car car;
@Autowired
private Office office;
public Boss(){
}
public Boss(String name, Car car, Office office) {
this.name = name;
this.car = car;
this.office = office;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public Office getOffice() {
return office;
}
public void setOffice(Office office) {
this.office = office;
}
}
测试类:
@Test
//知识点: ioc中的注解
public void ioc_annotation() {
try {
String path = "com/briup/ioc/annotation/annotation.xml";
ClassPathXmlApplicationContext container = new ClassPathXmlApplicationContext(path);
Boss boss = (Boss) container.getBean("boss");
System.out.println(boss);
System.out.println(boss.getOffice());
System.out.println(boss.getCar());
container.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
结果:能够拿到对应的具体对象