码迷,mamicode.com
首页 > 编程语言 > 详细

spring装配bean

时间:2015-12-17 10:32:30      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

  1:如果static方法
  public static UserService creatUserServiceimpl() {
        return new UserServiceimpl();
    }
<bean id="userservice" class="com.bj.jiekou.UserServiceFactory" factory-method="creatUserServiceimpl"></bean>

2:非静态方法创建实例
  public UserService creatUserServiceimpl() {
        return new UserServiceimpl();
    }
<bean id="factory" class="com.bj.jiekou.UserServiceFactory"></bean>
<bean id="userFactory" factory-bean="factory" factory-method="creatUserServiceimpl"></bean>
    

ApplicationContext accContext = new ClassPathXmlApplicationContext("com/bj/jiekou/beans.xml");
        UserService bean2 = (UserService)accContext.getBean("userFactory");
        bean2.saveUser();

 

spring装配bean

标签:

原文地址:http://www.cnblogs.com/baijin05/p/5053053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!