在java开发时,大家会经常使用工厂模式。获取工厂很简单,只需要配置相应的bean。但是如果想要获得factory中的方法的返回值该怎么办呢。我下面就将几个方法,已连接数据库的connectionfactory为例。
一、使用factorybean这个接口,重写方法
public class ConnectionFactory
implements FactoryBean<Connection>{
private String driver;
private String url;
private String name;
private String pwd;
public ConnectionFactory() {
super();
}
public ConnectionFactory(String driver, String url, String name, String pwd) {
super();
this.driver = driver;
this.url = url;
this.name = name;
this.pwd = pwd;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public Connection getObject() throws Exception {
// TODO Auto-generated method stub
Class.forName(driver);
Connection connection=DriverManager.getConnection(url, name, pwd);
return connection;
}
@Override
public Class<?> getObjectType() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isSingleton() {
// TODO Auto-generated method stub
return false;
}
}
factorybean后面泛型为需要获取的对象。重现接口的三个方法,其中getobject()方法修改返回值为对应的类型,并在其中操作,返回所需要的返回值。该方法局限性比较大,只能获取工厂中的一个对象。
下面是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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="conn" class="com.briup.ioc.factins.ConnectionFactory">
<property name="driver" value="${driver}"></property>
<property name="name" value="${username}"></property>
<property name="pwd" value="${password}"></property>
<property name="url" value="${url}"></property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:sql.properties</value>
</list>
</property>
</bean>
</beans>
大家会奇怪,配置文件里都没有connection这个bean。实际上,当我们的工厂继承了factorybean后,我们只需要获取factory这个实体,得到的则是其中我们想要的对象。如下:
Connection connection=(Connection)containerApplicationContext.getBean("conn");
二、在工厂中自己写出获得相应对象的方法,并在xml中配置。
factory
public class ConnectionFactory {
private String driver;
private String url;
private String name;
private String pwd;
public Connection getConnection() throws Exception{
Class.forName(driver);
Connection connection=DriverManager.getConnection(url,name,pwd);
return connection;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
工厂中getconnection()为获取所需对象的方法
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="factory" class="com.briup.ioc.insfact.ConnectionFactory">
<property name="driver" value="${driver}"></property>
<property name="name" value="${username}"></property>
<property name="pwd" value="${password}"></property>
<property name="url" value="${url}"></property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:sql.properties</value>
</list>
</property>
</bean>
<bean id="conn"
factory-bean="factory"
factory-method="getConnection"></bean>
</beans>
配置文件中通过factory-bean和factory-method两个属性,指出对应的工厂和方法,方法的返回值即所需要的对象。
三、对于第二种方法,如果getconnection()方法是静态方法,我们该怎么解决呢?让我们yiqilaikankan。
factory几乎不需要改变。但有一点需要注意,由于是静态方法,所以在程序运行前就已经编译好,那么其中的参数需要写死或者在静态方法中获得。如果使用上面在xml中配置的方法,就会出现获取不到的情况。
factory
package com.briup.ioc.staticfact;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionFactory {
private static String driver="oracle.jdbc.driver.OracleDriver";
private static String url="jdbc:oracle:thin:@172.16.12.130:1521:xe";
private static String name="kobe";
private static String pwd="kobe";
public static Connection getConnection() throws Exception{
Class.forName(driver);
Connection connection=DriverManager.getConnection(url,name,pwd);
return connection;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="conn"
class="com.briup.ioc.staticfact.ConnectionFactory"
factory-method="getConnection">
</bean>
</beans>
class属性写factory类,factory-method属性写静态方法。
总结:至此,三种factorybea的三种配置方法介绍完。