Before learning how to inject dependent objects, now learn how to inject basic types.
Personservicebean.java
Package Xjj.service.impl;import Xjj.dao.persondao;import Xjj.service.personservice;public class PersonServiceBean Implements Personservice {private Persondao persondao;private String name;private integer id;public integer getId () {Retu RN ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Personservicebean () {}public void Setpersondao (Persondao persondao) {This.persondao = Persondao;} Public Persondao Getpersondao () {return persondao;} @Overridepublic void Save () {System.out.println ("Name:" +name+ "\ n" + "ID:" +id);p Ersondao.add ();} }
Beans.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 "> <bean id=" Personservice "class=" Xjj.service.impl.PersonServiceBean "> <property name=" Persondao "><bean class = "Xjj.dao.impl.PersonDaoBean"/> </property> <property name= "name" value= "XJJ" ></ property> <property name= "id" value= "></property>" </bean></beans>
Springtest.java
Package Junit.test;import Org.junit.beforeclass;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Xjj.service.personservice;public Class springtest {@BeforeClasspublic static void Setupbeforeclass () throws Exception {} @Test public void instancespring () { ApplicationContext CTX = new Classpathxmlapplicationcontext ("Beans.xml"); Personservice Personservice = (personservice) ctx.getbean ("Personservice");p Ersonservice.save ();}}
Results:
Spring2.5 Learning 2.3_ How to inject basic types