据我所知,创建bean实例的方式有4种方式~
下面我会一一写出来这4种方式~
第一种:xml文件中有bean的配置,而且这个bean所对应的java类中存在一个无参构造器,那么这个时候spring容器就可以使用反射调用无参构造器来创建实例了~
代码如下:
张三
这就是一个bean实例~,我前几篇就是这样创建bean实例的,我这里就不多说了~接下来看第二种方式
第二种:通过工厂类获得实例(工厂类实现了接口FactoryBean>)
注意:spring中的PropertyPlaceholderConfigurer类的使用,在htmlsingle中直接搜索类名即可
例如:
工厂类ConnectionFactory实现指定接口并且实现接口中的三个抽象方法:
public class ConnectionFactory implements FactoryBean{
private String driver;
private String url;
private String username;
private String password;
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 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(driver);
Connection conn =
DriverManager.getConnection(url,username,password);
return conn;
}
@Override
public boolean isSingleton() {
// TODO Auto-generated method stub
return false;
}
@Override
public Class getObjectType() {
// TODO Auto-generated method stub
return Connection.class;
}
}
注意:一定要加上这个jar包-mysql-connector-java-5.1.18-bin.jar,这个jar包我在Hibernate里面就用过,这是连接数据库的~如图:
配置文件factory.xml:
还需要properties文件~我放在src目录下面~
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssh
username=root
password=root
测试类FactoryTest:
public class FactoryTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//在xml中配置工厂类,然后通过这个工厂类获得工厂生产的实例
try {
String path = "com/x/spring/test3/factory/factory.xml";
ApplicationContext container =
new ClassPathXmlApplicationContext(path);
Connection conn = (Connection)container.getBean("conn");
System.out.println(conn);
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
效果图:
注意: 通过conn拿到的并不是要拿到工厂类的对象,而是对应的这个工厂类所生产的产品对象~这一点要记住
好了,第二种方式写完了,开始第三种方式~
第三种:通过实例工厂获得实例(不需要实现或者继承任何接口或者父类)
注意spring中的PropertyPlaceholderConfigurer类的使用,在htmlsingle中直接搜索类名即可
例如:
一个普通的工程类ConnectionFactory:
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;
}
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 getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
这个类没有继承和实现任何接口或类
配置文件instanceFactory.xml:
properties文件上面有代码,我就不贴了~
测试类InstanceTest:
public class InstanceTest {
public static void main(String[] args) {
//通过实例工厂获得实例(不需要实现或者继承任何接口或者父类)
try {
String path = "com/x/spring/test3/instanceFactory/instanceFactory.xml";
ApplicationContext container =
new ClassPathXmlApplicationContext(path);
Connection conn = (Connection)container.getBean("conn");
System.out.println(conn);
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
第四种:通过静态工厂获得实例
例如:含义静态方法的工厂类ConnectionFactory
public class ConnectionFactory {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/ssh";
private static String username = "root";
private static String password = "root";
public static Object getConnection() throws Exception {
Class.forName(driver);
Connection conn =
DriverManager.getConnection(url,username,password);
return conn;
}
}
有没有觉得这个很熟悉~不错,这就是properties文件的一些配置属性~
这就可以不用写properties文件了~
配置文件staticFactory.xml:
测试类:
public class StaticTest {
public static void main(String[] args) {
//通过静态工厂获得实例
try {
String path = "com/x/spring/test3/staticFactory/staticFactory.xml";
ApplicationContext container =
new ClassPathXmlApplicationContext(path);
Connection conn = (Connection)container.getBean("conn");
System.out.println(conn);
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
就后面的一些数字不一样~
看来这三种方式通过conn拿到的并不是要拿到工厂类的对象,而是对应的这个工厂类所生产的产品对象~这一点要记住