There is bean configuration in the xml file, and the java class corresponding to this bean There is a parameterless constructor in
Then at this time, the spring container can use reflection to call the parameterless constructor to create an instance (normal way)< /p>
Get an instance through the factory class (the factory class implements the interface FactoryBean>< /span>
Pay attention to the use of the PropertyPlaceholderConfigurer class in spring, just search for the class name directly in htmlsingle
For example:
public class ConnectionFactory implements FactoryBean
private String driver;
private String url;
private String username;
private String password;
@Override
Class.forName(driver);
Connection conn =
DriverManager.getConnection(url,username,password);
return conn;
}
@Override
return false;
}
@Override
return Connection.class;
}
set/get
….
}
xml file:
The class configured below can automatically help us read the content of the specified properties file. The data is stored in the file in the form of key-value
After reading, we can use the form of ${key} to get the value in the file
Search for the class name directly in htmlsingle. Relevant configuration examples are available
The classpath refers to the search under src.
< bean class ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value >classpath:oracle.propertiesvalue>
property>
bean>
Because this class is a factory class, when we use the name conn to get the object in the container,
the object is not the factory class. It is the object returned by this factory class object after calling the factory method.
<bean name="conn" span> 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);
Obtain the bean object through the instance factory (no need to implement or inherit any interface or parent class), it is an ordinary factory instance instanceFactory
//A normal factory class
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 file:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> span>
<property name="location">
<value >classpath:oracle.propertiesvalue>
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>
<bean name="conn" factory-bean="factory" factory-method="getConnection"< /span>>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);
Get an instance through a static factory
For example
//Static factory class
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 file:
span>
<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);
//< /span>The factory class implements the specified interface and implements three abstract methods in the interface:
public class ConnectionFactory implements FactoryBean{
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 getObjectType() {
return Connection.class;
}
set/get
....
}
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="location">
<value >classpath:oracle.propertiesvalue>
property>
bean>
<bean name="conn" class="com.briup.ioc.factory.ConnectionFactory">< /span>
<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>
String path = "com/briup/ioc/factory/factory.xml";
ApplicationContext container =
new ClassPathXmlApplicationContext(path);
Connection conn =
(Connection)container.getBean("conn");
System.out.println(conn);
//< span style="color: #008000;">An ordinary factory class
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
....
}
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> span>
<property name="location">
<value >classpath:oracle.propertiesvalue>
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>
<bean name="conn" factory-bean="factory" factory-method="getConnection"< /span>>bean>
String path = "com/briup/ioc/instanceFactory/instanceFactory.xml";
ApplicationContext container =
new ClassPathXmlApplicationContext(path);
Connection conn =
(Connection)container.getBean("conn");
System.out.println(conn);
//< span style="color: #008000;">Static factory class
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;
}
}
<bean name="conn" class="com.briup.ioc.staticFactory.ConnectionFactory" factory-method="getConnection">bean>
String path = "com/briup/ioc/staticFactory/staticFactory.xml";
ApplicationContext container =
new ClassPathXmlApplicationContext(path);
Connection conn =
(Connection)container.getBean("conn");
System.out.println(conn);