自动装配Bean可以简化我们的代码,这里以一个例子进行讲解,首先创建三个实体类
package com.zhiying.pojo;
public class Cat {
public void shout() {
System.out.println("miao~");
}
}
package com.zhiying.pojo;
public class Dog {
public void shout() {
System.out.println("wang~");
}
}
package com.zhiying.pojo;
public class People {
private String name;
private Dog dog;
private Cat cat;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", dog=" + dog +
", cat=" + cat +
'}';
}
}
然后是配置文件,核心
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dog" class="com.zhiying.pojo.Dog"/>
<bean id="cat" class="com.zhiying.pojo.Cat"/>
<!-- 这是正常的注入方式-->
<!-- <bean id="people" class="com.zhiying.pojo.People">-->
<!-- <property name="name" value="贺志营"/>-->
<!-- <property name="cat" ref="cat"/>-->
<!-- <property name="dog" ref="dog"/>-->
<!-- </bean>-->
<!--
用autowire实现自动装配
byName:会自动在容器上下文查找,和自己对象set方法后面的值对应的beanid
byType:会自动在容器上下文查找,和自己属性类型相同的bean
-->
<!-- 用autowire实现自动装配,使用byName-->
<!-- <bean id="people" class="com.zhiying.pojo.People" autowire="byName">-->
<!-- <property name="name" value="贺志营"/>-->
<!-- </bean>-->
<!-- 用autowire实现自动装配,使用byType-->
<bean id="people" class="com.zhiying.pojo.People" autowire="byType">
<property name="name" value="贺志营"/>
</bean>
</beans>
最后是测试
import com.zhiying.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
People people = (People) context.getBean("people");
people.getCat().shout();
people.getDog().shout();
}
}
总结:
byName的时候,需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致
byType的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致
注解实现自动装配
@Autowired注解
同样的先是实体类
package com.zhiying.pojo;
public class Cat {
public void shout() {
System.out.println("miao~");
}
}
package com.zhiying.pojo;
public class Dog {
public void shout() {
System.out.println("wang~");
}
}
package com.zhiying.pojo;
import org.springframework.beans.factory.annotation.Autowired;
public class People {
private String name;
@Autowired
private Dog dog;
@Autowired
private Cat cat;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", dog=" + dog +
", cat=" + cat +
'}';
}
}
然后是配置文件,需要导入context约束,加入注解的支持
<?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="cat" class="com.zhiying.pojo.Cat"/>
<bean id="dog" class="com.zhiying.pojo.Dog"/>
<bean id="people" class="com.zhiying.pojo.People"/>
</beans>
然后是测试
import com.zhiying.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
People people = (People) context.getBean("people");
people.getCat().shout();
people.getDog().shout();
}
}
@Resource注解
这个注解是Java提供的,但是配置文件还是不能少的,只是在People类中导入的是javax包,这里只是修改People类,其他不变
package com.zhiying.pojo;
import javax.annotation.Resource;
public class People {
private String name;
@Resource
private Dog dog;
@Resource
private Cat cat;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", dog=" + dog +
", cat=" + cat +
'}';
}
}
总结:
@Autowired和@Resource的区别:
@Autowired是通过byType的方式实现,且要求这个对象必须存在
@Resource是通过byName的方式实现,如果找不到名字,则通过byType的方式实现,都找不到报错