赞
踩
据我所知,创建bean实例的方式有4种方式~
下面我会一一写出来这4种方式~
第一种:xml文件中有bean的配置,而且这个bean所对应的java类中存在一个无参构造器,那么这个时候spring容器就可以使用反射调用无参构造器来创建实例了~
代码如下:
- <?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 name="stu" class="com.x.spring.test1.Student">
- <property name="name">
- <value>张三</value>
- </property>
- </bean>
-
- </beans>
这就是一个bean实例~,我前几篇就是这样创建bean实例的,我这里就不多说了~接下来看第二种方式
第二种:通过工厂类获得实例(工厂类实现了接口FactoryBean<?>)
注意:spring中的PropertyPlaceholderConfigurer类的使用,在htmlsingle中直接搜索类名即可
例如:
工厂类ConnectionFactory实现指定接口并且实现接口中的三个抽象方法:
- public class ConnectionFactory implements FactoryBean<Connection>{
-
- 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<Connection> getObjectType() {
- // TODO Auto-generated method stub
- return Connection.class;
- }
- }
注意:一定要加上这个jar包-mysql-connector-java-5.1.18-bin.jar,这个jar包我在Hibernate里面就用过,这是连接数据库的~如图:
配置文件factory.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-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
-
-
-
- <!-- 通过conn拿到的是对应的这个工厂类所生产的产品对象 -->
- <!-- 造成这种现象的原因:因为这个类ConnectionFactory是一个工厂类,所以我们用名字conn在容器中拿对象的时候,
- 拿到并不是这个工厂类对象,而是这个工厂类对象调用完工厂方法后所返回的对象. -->
- <bean name="conn" class="com.x.spring.test3.factory.ConnectionFactory">
- <!-- 从一个配置文件中以key—value的形式拿value -->
- <property name="url" value="${url}"></property>
- <property name="driver" value="${driver}"></property>
- <property name="username" value="${username}"></property>
- <property name="password" value="${password}"></property>
- </bean>
-
- <!--
- 下面配置的这个类,可以自动的帮我们去读取指定的properties文件的
- 内容,文件中用key-value的形式存放数据,读完之后我们就可以用
- ${key}这种形式去拿文件中的value值了。
- classpath指的是从src下面找.
- -->
- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations" value="classpath:mysql.properties"/>
- </bean>
- </beans>
还需要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:
- <?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">
- <span style="color:#ff0000;"><!-- 读取properties文件 --></span>
- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations" value="classpath:mysql.properties"/>
- </bean>
-
- <bean name="factory" class="com.x.spring.test3.instanceFactory.ConnectionFactory">
- <property name="url" value="${url}"></property>
- <property name="driver" value="${driver}"></property>
- <property name="username" value="${username}"></property>
- <property name="password" value="${password}"></property>
- </bean>
-
- <span style="color:#ff0000;"> <!--
- 将来通过这个conn来拿对象,拿到的是名字为factory的工厂类调用完
- 名字为getConnection方法之后所返回的对象。
- --></span>
- <bean name="conn" factory-bean="factory" factory-method="getConnection"></bean>
-
- </beans>
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:
- <?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">
- <!-- 这样配置一定要求getConnection方法是静态方法 -->
- <bean name="conn" class="com.x.spring.test3.staticFactory.ConnectionFactory" factory-method="getConnection"></bean>
-
- </beans>
测试类:
- 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拿到的并不是要拿到工厂类的对象,而是对应的这个工厂类所生产的产品对象~这一点要记住
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。