通过构造器配置Bean
发布时间:2020-12-16 23:39:50 所属栏目:百科 来源:网络整理
导读:? 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= "
? 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); } ? ?结果: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |