I. 1. Creating Service classes and DAO classes
(1) Get the DAO object in the service
2. Specific implementation process
(1) Use DAO as type attribute in service
(2) Set method to generate a DAO Type property
Public class Userdao { publicvoid Add () { System.out.println ("DAO--------------" ); }}
Class UserService { private Userdao Userdao; voidThis.userdao =void Add () {System.out.println ("service--------------"); Userdao.add ();}}
3.xml configuration:
<bean id= "UserService" class= "Com.example.propetys.UserService" >
<!--the value of name is the value of the UserService Class Property ref is the value of the ID of the Userdao configuration--
<property name= "Userdao" ref= "Userdao" ></property>
</bean>
<bean id= "Userdao" class= "Com.example.propetys.UserDao" ></bean>
Two: P namespace injection
1. Build a class:
Public class Person { private String pname; Public void setpname (String pname) { this. pname = pname; } Public void Test () { System.out.println ("ASDHJCBSDJ" +pname);} }
2.xml file <beans> Next
xmlns:p= "http://www.springframework.org/schema/p"
<bean id= "Person" class= "Com.example.propetys.Person" P:pname= "Lucy" ></bean>
Three. Injecting complex type properties
1. Arrays
2.list Collection
3.map Collection
4.properties type
(1) Building a class
Importjava.util.List;ImportJava.util.Map;Importjava.util.Properties; Public classPerson {PrivateString pname; Public voidsetpname (String pname) { This. pname =pname; } Privatestring[] Arrs; PrivateList<string>list; PrivateMap<string, string>map; PrivateProperties Properties; Public voidSetarrs (string[] arrs) { This. Arrs =Arrs; } Public voidSetlist (list<string>list) { This. List =list; } Public voidSetmap (map<string, string>map) { This. Map =map; } Public voidSetProperties (Properties properties) { This. Properties =properties; } Public voidTest () {System.out.println ("Arrs:" +Arrs); System.out.println ("List:" +list); System.out.println ("Map:" +map); System.out.println ("Properties:" +properties); }}
(2) Configuring the XML file
<bean id= "Person"class= "Com.example.propetys.Person" ><!--array--<property name= "Arrs" > <list> < value> Math </value> <value> languages </value> <value> history </value> < ;value> English </value> </list> </property><!--List--<property name= "list" ; <list> <value>c/c++</value> <value>java</value> </list> ; </property><!--Map--<property name= "map" > <map> <entry key= "a" value= "Library" ></entry> <entry key= "B" value= "Teaching building" ></entry> <entry key= "C" value= "Dorm" &G T;</entry> </map> </property><!--Properties--<property name= "Properties" ; <props> <prop key= "Driverclass" >com.mysql.jdbc.Driver</prop> <prop key= "username" >root ≪/prop> </props> </property></bean>
Spring injects properties of object type