1:Instantiation with a constructor
类:
package com.myapp.core;
/**
* Spring bean
*
*/
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void printHello() {
System.out.println("Spring 3 : Hello ! " + name);
}
}
services.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
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- services -->
<!-- more bean definitions for services go here -->
<!-- Instantiation with a constructor -->
<bean id="helloBean" class="com.myapp.core.HelloWorld">
<property name="name" value="wqp" />
</bean>
<!-- Instantiation with a static factory method -->
<bean id="clientService" class="com.myapp.core.staticfactory.ClientService" factory-method="createInstance">
</bean>
<!-- Instantiation using an instance factory method -->
<bean id="serviceLocator" class="com.myapp.core.instancefactory.DefaultServiceLocator">
</bean>
<bean id="clientService2" factory-bean="serviceLocator" factory-method="createClientServiceInstance">
</bean>
<bean id="accountService" factory-bean="serviceLocator" factory-method="createAccountService">
</bean>
</beans>
daos.xml用来配置 persistence layer 的beans 这是预留的 里面没有beans
<?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">
<!-- more bean definitions for data access objects go here -->
</beans>
SpringBeans.xml 中导入了 services.xml
<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">
<import resource="resource/services.xml"/>
<import resource="resource/daos.xml"/>
</beans>
package com.myapp.core;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.myapp.core.staticfactory.ClientService;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"SpringBeans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloBean");
obj.printHello();
//ClientService clientService = (ClientService) context.getBean("clientService");
}
}
类分布:
![](https://img-my.csdn.net/uploads/201303/11/1363015193_2095.png)
以下代码是用静态工厂创建对象:
一:Instantiation with a constructor 通过构造器初始化对象:
<!-- Instantiation with a constructor -->
<bean id="helloBean" class="com.myapp.core.HelloWorld">
<property name="name" value="wqp" />
</bean>
<bean id="helloBean" class="com.myapp.core.HelloWorld">
<property name="name" value="wqp" />
</bean>
二:Instantiation with a static factory method 通过静态工厂方法创建对象
package com.myapp.core.staticfactory;
/**
* Instantiation with a static factory method
* @author topwqp
*
*/
public class ClientService {
//first create a private contrutor method; private responses that only itself can Instantiation
private ClientService(){}
private static ClientService clientService = new ClientService();
public static ClientService createInstance(){
//when spring container is startup the createInstance method will be invoked
// output this word a clientService object is created ohye is a good illustration
System.out.println("a clientService object is created ohye");
return clientService;
}
}
对应的配置:
<!-- Instantiation with a static factory method -->
<bean id="clientService" class="com.myapp.core.staticfactory.ClientService" factory-method="createInstance">
</bean>
三: Instantiation using an instance factory method 通过对象工厂方法创建对象
package com.myapp.core.instancefactory;
public interface AccountService {
}
package com.myapp.core.instancefactory;
public interface ClientService {
}
package com.myapp.core.instancefactory;
public class ClientServiceImpl implements ClientService{
}
package com.myapp.core.instancefactory;
public class AccountServiceImpl implements AccountService{
}
工厂方法创建对象:
package com.myapp.core.instancefactory;
/**
* Instantiation using an instance factory method
* @author topwqp
*
*/
public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();
private static AccountService accountService = new AccountServiceImpl();
public DefaultServiceLocator(){}
public ClientService createClientServiceInstance(){
System.out.println("factory instance method is created clientService........");
return clientService;
}
public AccountService createAccountService(){
System.out.println("factory instace method is created accountService...........");
return accountService;
}
}
对应的配置:
<!-- Instantiation using an instance factory method -->
<bean id="serviceLocator" class="com.myapp.core.instancefactory.DefaultServiceLocator">
</bean>
<bean id="clientService2" factory-bean="serviceLocator" factory-method="createClientServiceInstance">
</bean>
<bean id="accountService" factory-bean="serviceLocator" factory-method="createAccountService">
</bean>
验证输出:
![](https://img-my.csdn.net/uploads/201303/11/1363015664_2069.png)
在启动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