自动装配是Spring满足bean依赖的一种方式。
Spring会在上下文中自动寻找,并自动给bean装配属性
在xml中显式的配置。
在java中显式的配置
隐式的自动装配bean
cat
package com.rzp.pojo; ? public class Cat { public void shout(){ System.out.println("meow~"); } } ?
dog
package com.rzp.pojo; ? public class Dog { public void shout(){ System.out.println("wang!"); } } ?
people
package com.rzp.pojo; ? public class People { private Cat cat; private Dog dog; private String name; ? @Override public String toString() { return "People{" + "cat=" + cat + ", dog=" + dog + ", name=‘" + name + ‘\‘‘ + ‘}‘; } ? public Cat getCat() { return cat; } ? public void setCat(Cat cat) { this.cat = cat; } ? public Dog getDog() { return dog; } ? public void setDog(Dog dog) { this.dog = dog; } ? public String getName() { return name; } ? public void setName(String name) { this.name = name; } } ?
xml
在bean中设置autowire为byName
实例化对象的时候会自动在上下文中查找和自己对象set方法后面的值对应的beanId
<?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="cat" class="com.rzp.pojo.Cat"/> <bean id="dog" class="com.rzp.pojo.Dog"/> ? <!--会自动在容器上下文查找,和自己对象set方法后面的值对应的beanId--> <bean id="people" class="com.rzp.pojo.People" autowire="byName"> <property name="name" value="rzp"/> </bean> ? </beans>
比如people有setCat方法,就会在上下文中找id为cat的对象,并自动注入。
测试类
public class Mytest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); People people = context.getBean("people", People.class); people.getDog().shout(); people.getCat().shout(); } } ?
因此如果把dog的id修改后,就不能注入了。
<bean id="dog1" class="com.rzp.pojo.Dog"/>
在bean中设置autowire为byType
实例化对象的时候会自动在上下文中查找和自己对象set方法相同的对象
这种情况想完全只看类型,可以不写类的名字。
<bean class="com.rzp.pojo.Dog"/> ? <!--会自动在容器上下文查找,和自己对象set方法后面的值对应的beanId--> <bean id="people" class="com.rzp.pojo.People" autowire="byName"> <property name="name" value="rzp"/> </bean>
?
测试:Dog也被注入到people中去了
但是如果people中有多个Dog就会报错。
ByName:保证bean的id唯一,而且和注入的set方法值一致。
ByType:保证bean的class唯一,并且和注入的类型一致。
The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML. ----使用注解比xml更好。
注意aop这个包导入成功
导入约束
xmlns:context="http://www.springframework.org/schema/context"
配置注解的支持
<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 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>
在bean的属性上增加@Autowired注解,spring会自动根据id在xml文件中查找,找不到就找唯一的同类,如果也没有就会报错。
这种情况还可以增加Qualifier注解指定id的名字
示例
public class People { @Autowired private Cat cat; //使用注解以后,set方法也可以不需要 @Autowired private Dog dog; private String name; .....
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 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.rzp.pojo.Cat"/> <bean id="dog" class="com.rzp.pojo.Dog"/> <bean id="people" class="com.rzp.pojo.People"/> ? ? </beans>
使用Autowired,可以加在属性上面,也可以加在set方法上面。
使用Autowired和xml不同,不必要给set方法。
如果xml中有多个相同类的配置,可以增加Qualifier直接配置id
示例:
<bean id="cat" class="com.rzp.pojo.Cat"/> <bean id="dog" class="com.rzp.pojo.Dog"/> <bean id="dog1" class="com.rzp.pojo.Dog"/> <bean id="people" class="com.rzp.pojo.People"/> @Autowired @Qualifier("dog") private Dog dog;
Resource是Java提供的注解,不是spring的
<bean id="cat" class="com.rzp.pojo.Cat"/> <bean id="dog31" class="com.rzp.pojo.Dog"/> <bean id="dog1" class="com.rzp.pojo.Dog"/> <bean id="people" class="com.rzp.pojo.People"/> ? @Resource private Cat cat; @Resource(name = "dog1") private Dog dog; private String name;
原文:https://www.cnblogs.com/renzhongpei/p/12635114.html