Spring IOC 创建bean实例的方式
来源:互联网 发布:图画软件下载 编辑:程序博客网 时间:2024/05/18 22:43
据我所知,创建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;} //连接@Overridepublic Connection getObject() throws Exception {Class.forName(driver);Connection conn = DriverManager.getConnection(url,username,password);return conn;}@Overridepublic boolean isSingleton() {// TODO Auto-generated method stubreturn false;}@Overridepublic Class<Connection> getObjectType() {// TODO Auto-generated method stubreturn 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.Driverurl=jdbc:mysql://localhost:3306/sshusername=rootpassword=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 blocke.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拿到的并不是要拿到工厂类的对象,而是对应的这个工厂类所生产的产品对象~这一点要记住
- Spring IOC 创建bean实例的方式
- Spring IoC容器实例化bean的三种方式
- Spring-----7、bean实例的创建方式及依赖配置
- Spring创建Bean实例的3种方式
- Spring工厂方式创建Bean实例
- Spring 工厂方式 创建bean实例
- Spring Bean 的创建方式
- spring创建bean的方式
- spring创建bean的方式
- 传智播客 Spring中bean的实例化方式与IOC容器总结
- Spring中bean的实例化方式与IOC容器总结
- spring IOC容器实例化Bean的方式与RequestContextListener应用
- spring IOC容器实例化Bean的方式与RequestContextListener应用
- spring IOC容器实例化Bean的方式与RequestContextListener应用
- spring IOC容器实例化Bean的方式与RequestContextListener应用
- spring IOC容器实例化Bean的方式与RequestContextListener应用
- 第二章 IoC 三种实例化Bean的方式
- spring内部创建对象的方式(或bean实例化方式)
- 比较常用的jQuery
- 欢迎使用CSDN-markdown编辑器
- C++ PP Chapter XI 使用类
- LeetCode 第 48 题(Rotate Image)
- CAP理论
- Spring IOC 创建bean实例的方式
- 文件的读写
- 韩信点兵算法
- Android启动模式与onNewIntent
- 二分图匹配(基础)——过山车 ( HDU 2063 )
- Git使用入门
- 机顶盒EPG菜单自动处理程序20131118
- C++ 封装 继承 多态 (重点多态的实现)
- 软件工程师面试及简历指南