3.1 Bean的配置
Spring可以看作是一个工厂,用于生产和管理Spring容器中的Bean,如果要使用工厂生产和管理Bean,需要开发者在Spring配置文件中配置Bean,Spring框架支持XML和Properties两种格式的配置文件。
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-3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
3.2 Bean实例化
Spring框架实例化Bean有三种方式,构造方法实例化,静态工厂实例化和实例化工厂实例化,最常用的是构造方法实例化。
3.2.1 构造方法实例化
Spring容器可以通过调用Bean对应类中的无参数构造方法来实例化Bean,这种方