1、applicationContext.xml); TelePhone t = ac.getBean(,TelePhone.class); t.show(); org.junit.Test publicvoid test5() ApplicationContext ac = new ClassPathXmlApplicationContext(bean对应的java类 TelePhone.java/* * 手机类,模拟构造器注入 * author haifeng * */publicclassTelePhone private String cpu; private String ram; pu
2、blicTelePhone() super(); publicTelePhone(String cpu, String ram) this.cpu = cpu; this.ram = ram; publicvoidshow() System.out.println( TelePhone cpu: + cpu + , ram: + ram + /* * 手机类,模拟构造器注入 * author haifeng * */publicclassTelePhone private String cpu; public TelePhone() super(); public TelePhone(Stri
3、ng cpu, String ram) this.cpu = cpu; publicvoid show() System.out.println( SET方式注入对象:注入的对象需要在beans中声明,使用ref进行注入- 声明puter -studentmodel.Studentpropertyname=namehaydn/propertyputerref= publicvoidtest6() Student stu = ac.getBean(,Student.class); stu.show();org.junit.Test publicvoid test6() ApplicationCo
4、ntext ac = new ClassPathXmlApplicationContext(bean的对应JAVA类,Student.java * student实体类,模拟注入类对象 */publicclassStudent private String name; private puter puter; private TelePhone phone; publicvoidsetName(String name) this.name = name; publicvoidsetputer(puter puter) this.puter = puter; publicvoidsetPhone
5、(TelePhone phone) this.phone = phone; System.out.println(学生XX: + name); puter.show(); phone.show();/* * student实体类,模拟注入类对象 * author haifeng * */publicclassStudent private String name; publicvoid setName(String name) this.name = name; publicvoid setputer(puter puter) this.puter = puter; publicvoid se
6、tPhone(TelePhone phone) this.phone = phone; publicvoid show() System.out.println(List set map propetries 注入- 注入集合 -messagebean.Message- 字符串注入null值,方式一:不写该配置,方式2:不写value,加个null标签 - null/age23- 注入list -friendslistvalue摩严白子画杀阡陌花千骨洛十一糖宝- 注入set -citiessetXX- 注入MAP -scoremapentrykey=语文98/entry数学99英语100/ma
7、p- 注入配置文件 -dbParamspropspropkey=urllocalhostusernamerootpassword123- 表达式注入,类似EL表达式用#bean对象名.属性标记, -#dbProps.password- spring标签注入 ,其他bean中使用ref调用,set Map Propties都可以,使用时需要加入命名空间-util:listid=someList/util:- 加载properties文件创建Properties对象,location=classpath:dbcp.properties location表示指定文件路径 classpath表示绝对路
8、径 -propertiesid=dbPropslocation=properties location表示指定文件路径 classpath表示绝对路径 -测试类JAVA代码: publicvoidtest7() Message msg = ac.getBean(,Message.class); msg.show();org.junit.Test publicvoid test7() ApplicationContext ac = new ClassPathXmlApplicationContext(bean的对应JAVA类,Message.java * 消息类,模拟各种类型对象注入 */pub
9、licclassMessage private String name; privateint age; private List friends; private Set cities; private Map score; private Properties dbParams; private String password; publicvoidsetAge(int age) this.age = age; publicvoidsetFriends(List friends) this.friends = friends; publicvoidsetCities(Set cities)
10、 this.cities = cities; publicvoidsetScore(Map score) this.score = score; publicvoidsetDbParams(Properties dbParams) this.dbParams = dbParams; publicvoidsetPassword(String password) this.password = password;Message name: + name + , age: + age +, friends+ friends.toString()+,cities: +cities.toString()
11、+,score:+score.toString()+ ,properties:+dbParams.toString()+password:+password+ /* * map的遍历 */publicvoidshowMap() SetEntry map = score.entrySet(); for(Entry keys = dbParams.keySet(); for(Object o : keys) System.out.println(dbParams.get(o);/* * 消息类,模拟各种类型对象注入 * author haifeng * */publicclassMessage p
12、rivate String name; publicvoid setAge(int age) this.age = age; publicvoid setFriends(List friends) this.friends = friends; publicvoid setCities(Set cities) this.cities = cities; publicvoid setScore(Map score) this.score = score; publicvoid setDbParams(Properties dbParams) this.dbParams = dbParams; p
13、ublicvoid setPassword(String password) this.password = password;+ friends.toString()+score.toString()+ /* * map的遍历 */publicvoid showMap() Set map) System.out.println(e.getKey()+e.getValue(); /* * protries的遍历 */publicvoid showProp() Set- 自动装配的bean的id需要和待注入的bean的属性name一致,注入的名字需要和set方法的名字一致(不含set,首字母小写) - byType和名字没关系,根据类型自动装配 - 但是byType的bean中有相同类型的bean出现则会出错,而且还要以单例模式声明要注入的bean -autowire=byName
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1