一、手动装配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.xiaolu.pojo.Cat"></bean>
<bean id="dog" class="com.xiaolu.pojo.Dog"></bean>
<bean id="people" class="com.xiaolu.pojo.People">
<property name="name" value="李四"></property>
<property name="cat" ref="cat"></property>
<property name="dog" ref="dog"></property>
</bean>
</beans>
需要自己在xml里面添加属性的配置
<property name="name" value="李四"></property>
<property name="cat" ref="cat"></property>
<property name="dog" ref="dog"></property>
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
People people = (People) context.getBean("people");
people.getCat().shout();
people.getDog().shout();
}
}
二、bean的自动装配
spring自动在上下文中寻找配置的对象装配。
通过byname
使用autowire=byName
<?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.xiaolu.pojo.Cat"></bean>
<bean id="dog" class="com.xiaolu.pojo.Dog"></bean>
<bean id="people" class="com.xiaolu.pojo.People" autowire="byName">
<property name="name" value="李四"></property>
</bean>
</beans>
通过bytype
同理使用autowire=byType
通过注解
1、开启注解方式装配
<context:annotation-config></context:annotation-config>
<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">
<bean id="cat" class="com.xiaolu.pojo.Cat"></bean>
<bean id="dog" class="com.xiaolu.pojo.Dog"></bean>
<bean id="people" class="com.xiaolu.pojo.People">
<property name="name" value="李四"></property>
</bean>
<context:annotation-config></context:annotation-config>
</beans>
2、在需要装配的位置使用注解@Autowired
public class People {
@Autowired
private Cat cat;
@Autowired
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 + '\'' +
'}';
}
}
三、bean的作用域
- singleton
- prototype
- request
- session
- application
- websocket
默认为单例模式。
可以通过scope修改
<bean id="cat" class="com.xiaolu.pojo.Cat" scope="prototype"></bean>