spring实例化bean之构造方法实例化
2013-08-21 22:14
316 查看
PersonDao
public interface PersonDao {
}
PersonDaoImpl
public class PersonDaoImpl implements PersonDao{
public PersonDaoImpl(){
System.out.println("spring通过构造方法来实例化对象");
}
}
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- spring通过构造方法创建PersonDaoImpl -->
<bean id="personDao" class="com.xxc.initBean.one.dao.Impl.PersonDaoImpl"></bean>
</beans>
测试类
public class App {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/initBean/one/applicationContext.xml");
PersonDao personDao = (PersonDaoImpl)ac.getBean("personDao");
}
}
public interface PersonDao {
}
PersonDaoImpl
public class PersonDaoImpl implements PersonDao{
public PersonDaoImpl(){
System.out.println("spring通过构造方法来实例化对象");
}
}
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- spring通过构造方法创建PersonDaoImpl -->
<bean id="personDao" class="com.xxc.initBean.one.dao.Impl.PersonDaoImpl"></bean>
</beans>
测试类
public class App {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/initBean/one/applicationContext.xml");
PersonDao personDao = (PersonDaoImpl)ac.getBean("personDao");
}
}
相关文章推荐
- Spring使用实例工厂方法实例化Bean
- spring中构造bean实例的三种方法
- Spring 使用实例工厂方法实例化Bean
- spring实例化bean之简单工厂静态方法实例化
- spring实例化bean之简单工厂实例方法实例化
- 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法
- 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法
- 从spring容器中得到bean_使用类构造器实例化_使用静态工厂方法实例化_使用实例工厂方法实例化_例子
- Spring工厂方法创建(实例化)bean实例代码
- 创建Bean的三种方式 在大多数情况下,Spring容器直接通过new关键字调用构造器来创建Bean实例,而class属性指定Bean实例的实现类,但这不是实例化Bean的唯一方法。实际上,Sprin
- Spring、实例化Bean的三种方法
- spring笔记10---实例工厂方法配置bean
- spring 9 bean配置--通过工厂方法(静态工厂方法&实例工厂方法
- Spring-bean构造方法
- Spring之替换Bean的返回结果,替换Bean的方法实例
- Spring入门学习——调用一个实例工厂方法创建Bean
- Spring实例化bean的三种方法
- [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名
- 获取Spring容器中Bean实例的工具类(Java泛型方法实现)
- Spring实例化bean的三种方法