Spring injects properties of object type

Source: Internet
Author: User

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> &lt ;value> English </value> </list> </property><!--List--<property name= "list"         ; <list> <value>c/c++</value> <value>java</value> </list&gt      ; </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 &Lt;/prop> </props> </property></bean> 

Spring injects properties of object type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.