spring容器创建bean对象的方式

xml文件中有bean的配置,而且这个bean所对应的java类中存在一个无参构造器

那么这个时候spring容器就可以使用反射调用无参构造器来创建实例了(常规的方式)

通过工厂类获得实例(工厂类实现了接口FactoryBean<?>
注意spring中的PropertyPlaceholderConfigurer类的使用,在htmlsingle中直接搜索类名即可
例如:

//工厂类实现指定接口并且实现接口中的三个抽象方法:
public class ConnectionFactory implements FactoryBean<Connection>{
private String driver;
private String url;
private String username;
private String password;

@Override
public Connection getObject() throws Exception {
Class.forName(driver);
Connection conn = 
DriverManager.getConnection(url,username,password);
return conn;
}

@Override
public boolean isSingleton() {
return false;
}

@Override
public Class<Connection> getObjectType() {
return Connection.class;
}
set/get
....
}

xml文件:

下面配置的这个类,可以自动的帮我们去读取指定的properties文件的内容,文件中用key-value的形式存放数据
读完之后我们就可以用${key}这种形式去拿文件中的value值了

在htmlsingle中直接搜索类名即可得到相关配置样例

classpath指的是从src下面找.

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:oracle.properties</value>
</property>
</bean>

因为这个类是一个工厂类,所以我们用名字conn在容器中拿对象的时候,
拿到并不是这个工厂类对象,而是这个工厂类对象调用完工厂方法后所返回的对象.

<bean name="conn" class="com.briup.ioc.factory.ConnectionFactory">
<property name="driver">
<value>${driver}</value>
</property>

<property name="url">
<value>${url}</value>
</property>

<property name="username">
<value>${username}</value>
</property>

<property name="password">
<value>${password}</value>
</property>
</bean>

main:

String path = "com/briup/ioc/factory/factory.xml";
ApplicationContext container = 
new ClassPathXmlApplicationContext(path);
Connection conn = 
(Connection)container.getBean("conn");

System.out.println(conn);

通过实例工厂获得bean对象(不需要实现或者继承任何接口或者父类),就是一个普通的工厂实例instanceFactory

//一个普通的工厂类 
public class ConnectionFactory{
private String driver;
private String url;
private String username;
private String password;

public Object getConnection() throws Exception {
Class.forName(driver);
Connection conn = 
DriverManager.getConnection(url,username,password);
return conn;
}
get/set
....
}

xml文件:

<!-- 读取properties文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:oracle.properties</value>
</property>
</bean>
<!-- 配置工厂类 --> <bean name="factory" class="com.briup.ioc.instanceFactory.ConnectionFactory"> <property name="driver"> <value>${driver}</value> </property> <property name="url"> <value>${url}</value> </property> <property name="username"> <value>${username}</value> </property> <property name="password"> <value>${password}</value> </property> </bean> <!-- 将来通过这个conn来拿对象,拿到的是名字为factory的工厂类调用完名字为getConnection方法之后所返回的对象。--> <bean name="conn" factory-bean="factory" factory-method="getConnection"></bean>

main:

String path = "com/briup/ioc/instanceFactory/instanceFactory.xml";
ApplicationContext container = 
new ClassPathXmlApplicationContext(path);

Connection conn = 
(Connection)container.getBean("conn");

System.out.println(conn); 

通过静态工厂获得实例
例如

//静态工厂类
public class ConnectionFactory{
private static String driver = 
"oracle.jdbc.driver.OracleDriver";
private static String url = 
"jdbc:oracle:thin:@127.0.0.1:1521:XE";
private static String username = "briup";
private static String password = "briup";

public static Object getConnection() throws Exception {
Class.forName(driver);
Connection conn = 
DriverManager.getConnection(url,username,password);
return conn;
}
}

xml文件:

<!-- 这样配置一定要求getConnection方法是静态方法 -->
<bean name="conn" class="com.briup.ioc.staticFactory.ConnectionFactory" factory-method="getConnection"></bean>

main:

String path = "com/briup/ioc/staticFactory/staticFactory.xml";
ApplicationContext container = 
new ClassPathXmlApplicationContext(path);

Connection conn = 
(Connection)container.getBean("conn");

System.out.println(conn);
相关文章
相关标签/搜索