可以直接手动注入bean,可以直接在bean标签里来实现自动装配,也可以使用注解来实现自动装配。使用注解实现自动装配的尝试大致分为以下步骤(官方文档对这些进行了详细的介绍https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-annotation-config)
1、配置
要想使用注解,就得在beans.xml中做如下配置(官方文档上copy的)
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
2、写实体类
这里做4个实体类,分别是people、cat、dog、bird。以追求把每种注解都试一遍
people(每种注解的用法都写在注释里,很重要)
package com.wt.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ImportResource;
public class People {
private String name;
@Autowired//@Autowired会在beans.xml中寻找是否与cat相同类型(Cat类型)的bean,既@Autowired是ByType的
//然后实现自动装配,帮你实现<property name="cat" ref="与cat相同类型(就是Cat类型)的bean的id"/>
//自己尝试时发现,如果在xml中注册了cat,cat1,都是Cat类型,运行不会报错
//而如果将注册cat,cat2,都是Cat类型,会报错(因为ByType),猜测是不是如果发现多个同type的,
//就找bean的id与people的属性名相同的。
private Cat cat;
@Autowired
@Qualifier(value="dog2")//如果发现了多个类型相同的type,@Autowired不知装配哪一个,则可以使用@Qualifier来指定装配哪个id的bean
private Dog dog;
//@Resource还有一个@Resource,默认按照ByName进行查找,找不到则使用ByType,如果发现了多个类型相同的type,会报错
@Autowired
private Bird bird;
//无参有参构造函数和getter、setter太长了在这里省略,实际是要写的,下面几个类都做省略
}
cat
package com.wt.pojo;
public class Cat {
private String name;
public void makeNoise(){
System.out.println("miaomiao");
}
}
dog
package com.wt.pojo;
public class Dog {
private String name;
public void makeNoise(){
System.out.println("wangwang");
}
}
bird
package com.wt.pojo;
public class Bird {
private String name;
public void makeNoise(){
System.out.println("jijizhazha");
}
}
3、写配置文件(还是要注意看注释)
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<!--想要使用注解,必须先做如上配置-->
<bean id="cat1" class="com.wt.pojo.Cat"/>
<bean id="cat" class="com.wt.pojo.Cat"/>
<bean id="dog1" class="com.wt.pojo.Dog"/>
<bean id="dog2" class="com.wt.pojo.Dog"/>
<bean id="bird1" class="com.wt.pojo.Bird"/>
<bean id="people" class="com.wt.pojo.People">
<property name="name" value="wt"/>
<!--使用注解自动装配可以代替下面的两行手动注入-->
<!-- <property name="cat" ref="cat"/>-->
<!-- <property name="dog" ref="dog"/>-->
</bean>
</beans>
4、测试
import com.wt.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//这里传入要创建的对象的id后再写一个类参数,这样就不用强制转换了
People people = context.getBean("people",People.class);
people.getCat().makeNoise();
people.getDog().makeNoise();
people.getBird().makeNoise();
}
}