在Spring中有三种装配的方式:
环境搭建 : 一个人有两个宠物
People
package com.xg.pojo;
public class People {
private Cat cat;
private Dog dog;
private String 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;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name=‘" + name + ‘\‘‘ +
‘}‘;
}
}
Cat
package com.xg.pojo;
public class Cat {
public void shout(){
System.out.println("喵喵喵!");
}
}
Dog
package com.xg.pojo;
public class Dog {
public void shout(){
System.out.println("汪汪汪!");
}
}
beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.xg.pojo.Cat"/>
<bean id="dog" class="com.xg.pojo.Dog"/>
<bean id="people" class="com.xg.pojo.People">
<property name="name" value="遇见星光"/>
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
</bean>
</beans>
测试类
import com.xg.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getCat().shout();
people.getDog().shout();
}
}
byName : 会自动在容器上下文中查找,和自己对象 set方法 后面的值对应的 bean id !
如果没有找到将会报错!
beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.xg.pojo.Cat"/>
<bean id="dog" class="com.xg.pojo.Dog"/>
<bean id="people" class="com.xg.pojo.People" autowire="byName">
<property name="name" value="遇见星光"/>
</bean>
</beans>
byName : 会自动在容器上下文中查找,和自己对象 类型相同的 bean !
<?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.xg.pojo.Cat"/>
<bean id="dogNew" class="com.xg.pojo.Dog"/>
<bean id="people" class="com.xg.pojo.People" autowire="byType">
<property name="name" value="遇见星光"/>
</bean>
</beans>
JDK1.5支持的注解,Spring2.5就支持注解了。
要使用注解须知
导入约束
配置注解支持
<?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.xsd">
<context:annotation-config/>
</beans>
@Autowired 直接在属性上使用即可!也可以在Set方法上使用!
使用@Autowired 在属性上 可以不写 Set方法,前提是这个自动装配的属性在IOC(Spring)容器中存在,且符合byName!
如果使用@Autowired 自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候、可以使用 @Qualifier(value="xxx")
配合 @Autowired
使用,指定一个唯一的bean对象注入!
beans.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.xsd">
<context:annotation-config/>
<bean id="cat" class="com.xg.pojo.Cat"/>
<bean id="cat2" class="com.xg.pojo.Cat"/>
<bean id="dog1" class="com.xg.pojo.Dog"/>
<bean id="dogNew" class="com.xg.pojo.Dog"/>
<bean id="people" class="com.xg.pojo.People"/>
</beans>
People
package com.xg.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class People {
// 如果显示定义了Autowired 的 required 属性为 false,说明这个对象可以为null,否则不能为空
@Autowired
private Cat cat;
@Autowired
@Qualifier("dog1")
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
public String getName() {
return name;
}
}
@Nullable // 字段标记了这个注解,说明这个字段可以为null
public @interface Autowired {
boolean required() default true;
}
// 如果显示定义了Autowired 的 required 属性为 false,说明这个对象可以为null,否则不能为空
@Autowired(required = false)
private Cat cat;
@Resource Java原生注解
@Resource(name = "cat2")
private Cat cat;
@Autowired 和 @Resource 的区别
原文:https://www.cnblogs.com/Right-A/p/14989084.html