码迷,mamicode.com
首页 > 其他好文 > 详细

通过构造器配置Bean

时间:2018-12-15 00:15:45      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:src   image   override   frame   pre   int   nbsp   contex   code   

 

public class Role {
    private Log id;
    private String roleName;
    private String note;
    
    public Role(String roleName, String note) {
        this.roleName = roleName;
        this.note = note;
    }

    @Override
    public String toString() {
        return "Role{" +
                "id=" + id +
                ", roleName=‘" + roleName + \‘ +
                ", note=‘" + note + \‘ +
                };
    }
}

 

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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="role1" class="com.nf147.manage.spring.Role">
        <constructor-arg index="0" value="总经理"/>
        <constructor-arg index="1" value="公司管理者"/>
    </bean>


</beans>

 

  测试代码:

  public static void main(String[] args) {
        ApplicationContext A=new ClassPathXmlApplicationContext("spring/my-spring.xml");
        Object role1 = A.getBean("role1");
        //System.out.println(((Role)role1).toString());
        System.out.println((Role)role1);

    }

 

 结果:

技术分享图片

 

通过构造器配置Bean

标签:src   image   override   frame   pre   int   nbsp   contex   code   

原文地址:https://www.cnblogs.com/nongzihong/p/10122203.html

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