?
?
?
?
?
?
?
?
?
ConnectionFactory是已经准备好的,我们首先通过spring创建这个工厂,再通过spring整合这个工厂的这个方法从而创建conn这个复杂对象
?
?
?
?
?
a pplicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
id:名字唯一
class属性:类的全限定名
通过写bean标签告诉工厂生产的对象
-->
<!--构造方法注入(赋值)
构造方法有几个参数,这里constructor就有几个标签
-->
<!-- <bean id="customer" class="com.itheima.constructer.Customer">
<constructor-arg>
<value>suns</value>
</constructor-arg>
<constructor-arg>
<value>102</value>
</constructor-arg>
</bean>-->
<!-- 当选择不同的构造方法时,为了区分不同的参数类型,需要给constructor加一个属性type="int"-->
<bean id="customer" class="com.itheima.constructer.Customer">
<constructor-arg type="int">
<value>37</value>
</constructor-arg>
</bean>
<!--创建复杂对象-->
<!-- <bean id="conn" class="com.itheima.factorybean.ConnectionFactoryBean">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/library?useSSlL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>-->
<!-- 实体工厂:
factory-bean:与上面那个类建立联系 指定工厂
factory-method:指定调用对象的方法
-->
<!-- <bean id="connFactory" class="com.itheima.factorybean.ConnectionFactory"></bean>
<bean id="conn" factory-bean="connFactory" factory-method="getConnection"/>-->
<!--静态工厂:factory-method:指定静态工厂的方法
-->
<bean id="conn" class="com.itheima.factorybean.StaticConnectionFactory" factory-method="getConnection"/>
<!--创建简单对象的次数
scope="singleton":表示只会创建一次
scope="prototype":表示创建多个对象
-->
<!-- <bean id="account" scope="singleton" class="com.itheima.scope.Account"/>-->
<!--scope="singleton":表示工厂创建的同时,这个对象被创建出来
-->
<!-- <bean id="product" scope="singleton" class="com.itheima.life.Product"/>-->
<!--<bean id="product" scope="prototype" class="com.itheima.life.Product"/>-->
<!--对象的初始化,需要加上一个属性-->
<!-- <bean id="product" scope="singleton" class="com.itheima.life.Product" init-method="myInit"/>-->
</beans>
FactoryBean接口:
ConnectionFactoryBean类:
package com.itheima.factorybean;
import org.springframework.beans.factory.FactoryBean;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionFactoryBean implements FactoryBean<Connection> {
//把依赖的信息,通过配置文件进行依赖注入,解耦合
private String driverClassName;
private String url;
private String username;
private String password;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//用于书写获取复杂对象的代码
@Override
public Connection getObject() throws Exception {
Class.forName(driverClassName);
Connection conn= DriverManager.getConnection(url,username,password);
return conn;
}
//返回所创建复杂对象的Class对象
@Override
public Class<?> getObjectType() {
return Connection.class;
}
@Override
public boolean isSingleton() {
//方法中是false,则每次会创建一个新的对象,如果是true,则每次返回通过个对象
return false;
}
}
实例工厂
ConnectionFactory:
package com.itheima.factorybean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
public Connection getConnection(){
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/library?useSSL=false","root","123456");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
静态工厂:
StaticConnectionFactory:
package com.itheima.factorybean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/*
实例工厂:
ConnectionFactory cf=new ConnectionFactory();
cf.getConnection();
静态工厂
StaticConnectionFactory.getConnection();
*/
public class StaticConnectionFactory {
public static Connection getConnection(){
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/library?useSSL=false","root","123456");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
Product类:
package com.itheima.life;
import org.springframework.beans.factory.InitializingBean;
//public class Product implements InitializingBean {
public class Product{
public Product() {
System.out.println("Product.product");
}
//初始化方法:做一些初始化操作 spring调用
/* @Override
public void afterPropertiesSet() throws Exception {
System.out.println("Product.afterPropertiesSet");
}*/
//自定义初始化方法
public void myInit(){
System.out.println("Product.myInit");
}
}
Account类:
package com.itheima.scope;
public class Account {
}
SpringTest:
package com.itheima;
import com.itheima.constructer.Customer;
import com.itheima.factorybean.ConnectionFactoryBean;
import com.itheima.life.Product;
import com.itheima.scope.Account;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class SpringTest {
//通过构造方法创建对象赋初值测试
@Test
public void test1(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象
Customer customer = (Customer) ctx.getBean("customer");
System.out.println("customer="+customer);
}
//创建复杂对象的测试,返回的不是ConnectionFactoryBean对象,而是Connection对象
//isSingleton()方法中是false,则每次会创建一个新的对象,如果是true,则每次返回通过个对象
/*
<bean id="conn" class="com.itheima.factorybean.ConnectionFactoryBean">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/library?useSSlL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
*/
@Test
public void test2(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象
Connection conn = (Connection) ctx.getBean("conn");
Connection conn2 = (Connection) ctx.getBean("conn");
System.out.println("conn="+conn);
System.out.println("conn="+conn2);
}
//创建复杂对象的测试,返回的不是ConnectionFactoryBean对象,而是Connection对象
//如果想要返回ConnectionFactoryBean对象,需要加一个&
@Test
public void test3(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象
ConnectionFactoryBean conn = (ConnectionFactoryBean) ctx.getBean("&conn");
System.out.println("conn="+conn);
}
//测试实例工厂、静态工厂:创建Connection复杂对象
/*
<bean id="connFactory" class="com.itheima.factorybean.ConnectionFactory"></bean>
<bean id="conn" factory-bean="connFactory" factory-method="getConnection"/>
<bean id="conn" class="com.itheima.factorybean.StaticConnectionFactory" factory-method="getConnection"/>
*/
@Test
public void test4(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象
Connection conn = (Connection) ctx.getBean("conn");
System.out.println("conn="+conn);
}
//测试创建简单对象的次数
/*
创建简单对象的次数
scope="singleton":表示只会创建一次
scope="prototype":表示创建多个对象
<bean id="account" scope="singleton" class="com.itheima.scope.Account"/>
*/
@Test
public void test5(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象
Account account1 = (Account) ctx.getBean("account");
Account account2 = (Account) ctx.getBean("account");
System.out.println("account1="+account1);
System.out.println("account2="+account2);
}
//测试对象的生命周期,对象的创建时期
//当scope="singleton":表示工厂创建的同时,这个对象被创建出来
/*
<bean id="product" scope="singleton" class="com.itheima.life.Product"/>
*/
@Test
public void test6(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
}
/*
<bean id="product" scope="prototype" class="com.itheima.life.Product"/>
<!--对象的初始化,需要加上一个属性-->
对象的销毁,需要加上一个属性destroy-method="mydestroy"
<bean id="product" scope="singleton" class="com.itheima.life.Product" init-method="myInit" destroy-method="mydestroy"/>
*/
// scope="prototype",工厂会在获取对象时创建对象 对象的初始化和销毁测试
@Test
public void test7(){
//1.获得spring的工厂
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
Product product = (Product) ctx.getBean("product");
//关闭工厂前,调用对象销毁方法
ctx.close();
}
}
test1:
2.
?
3.
?
4.
?
5.
?
6.
?
7.
?
运行test7测试初始化:
?
?
?
|