传统的手动配置bean
相关的准备类
- Cat类代码
public class Cat {
public void action(){
System.out.println("miao~");
}
}
- Dog类代码
public class Dog {
public void action(){
System.out.println("wang~");
}
}
- Person类代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
public class Person {
private Dog dog;
private Cat cat;
private String name;
public Person() {
}
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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"dog=" + dog +
", cat=" + cat +
", name='" + name + '\'' +
'}';
}
}
- xml配置文件:创建每个类与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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 手动配置bean-->
<bean id="dog" class="com.Dog"/>
<bean id="cat" class="com.Cat"/>
<bean id="person" class="com.Person">
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
</bean>
</beans>
- 测试类代码:
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
System.out.println("使用手动配置bean");
Person person = (Person) context.getBean("person");
System.out.println(person.getName());
person.getDog().action();
person.getCat().action();
}
}
反射机制与自动装配bean
反射机制是框架设计的灵魂,在spring和springBoot中应用了大量的注解,而注解配合反射机制的使用,使得框架在开发中更好的提高效率,降低代码的冗余,自动装配bean的本质就是利用反射机制来实现的。
自动装配bean的方法:
-
byType
-
byName
-
注解
xml自动配置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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 自动装配bean:byName,会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid,保证bean的id唯一 -->
<bean id="person-auto-byName" class="com.Person" autowire="byName">
<property name="name" value="zhangsan"/>
</bean>
<!-- 自动装配bean:byType,会自动在容器上下文中查找,和自己对象属性相同的的bean,必须保证类型数量全局为1,保证bean的class唯一不推荐使用 -->
<bean id="person-auto-byType" class="com.Person" autowire="byType">
<property name="name" value="lisi"/>
</bean>
<!-- 通过注解来实现自动装配bean -->
<import resource="bean-annotation.xml"/>
</beans>
开启注解的xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<!--添加context依赖-->
<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>
- 使用注解后的Person类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
public class Person {
//直接在需要配置的属性上使用@Autowired即可,可以省略set方法,因为注解是通过反射机制实现的
@Autowired
private Dog dog;
@Autowired
private Cat cat;
@Autowired
@Value("mazi")
private String name;
public Person() {
}
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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"dog=" + dog +
", cat=" + cat +
", name='" + name + '\'' +
'}';
}
}
- 使用注解实现自动配置bean的测试类代码
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
//自动装配bean讲解
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//1.使用byName
System.out.println("使用自动配置bean:byName方法");
Person personauto1 = (Person) context.getBean("person-auto-byName");
System.out.println(personauto1.getName());
personauto1.getDog().action();
personauto1.getCat().action();
//2.使用byType
System.out.println("使用自动配置bean:byType方法");
Person personauto2 = (Person) context.getBean("person-auto-byType");
System.out.println(personauto2.getName());
personauto2.getDog().action();
personauto2.getCat().action();
//3.使用注解实现bean的自动配置
System.out.println("使用注解实现bean的自动配置");
Person personannotation = (Person) context.getBean("person");
System.out.println(personannotation.getName());
personannotation.getCat().action();
personannotation.getDog().action();
}
}