Spring Container 初始化 beans 的三种方式
以下代码是用静态工厂创建对象:一:Instantiation with a constructor 通过构造器初始化对象: <!-- Instantiation with a constructor -->
<bean id="helloBean" class="com.myapp.core.HelloWorld">
<property name="name" value="wqp" />
</bean>
二:Instantiation with a static factory method 通过静态工厂方法创建对象
三: Instantiation using an instance factory method 通过对象工厂方法创建对象
在启动spring的 spring容器 装入这些对象, 这样 构造方法中的 打印就会输出。 根据打印可以看出 对象均创建成功。
以上是通过maven构建项目 其中 pom.xml的配置如下:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.myapp.core</groupId> <artifactId>Spring3Example</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>Spring3Example</name> <url>http://maven.apache.org</url> <properties><spring.version>3.0.5.RELEASE</spring.version></properties> <dependencies> <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency> <!-- Spring 3 dependencies --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency> <!-- JSTL --> <dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.1.2</version></dependency> <dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version></dependency> </dependencies> <build> <plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.7</source><target>1.7</target></configuration></plugin> </plugins></build></project>
REFERENCE spring IOC