Spring Injection-map

Source: Internet
Author: User

Injecting attributes into a map in the spring framework

object creation for 1MAP mapping

 Packagecom;/*** Map collection Usage test in spring*/ Public classUser {Private intID; PrivateString name; PrivateString pwd;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString getpwd () {returnpwd; }     Public voidsetpwd (String pwd) { This. PWD =pwd; } @Override PublicString toString () {return"user[id=" + ID + ", name=" + name + ", pwd=" + pwd + "]"; }     PublicUser (intID, string name, string pwd) {        Super();  This. ID =ID;  This. Name =name;  This. PWD =pwd; }     PublicUser () {Super(); //TODO auto-generated Constructor stub    }}
View Code

Users of 2.Map

 Packagecom;ImportJava.util.Map;/*** Map Collection Use test in spring framework*/ Public classMapdemo {Private intID; PrivateString name; PrivateString pwd; PrivateMap<string,user>user;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString getpwd () {returnpwd; }     Public voidsetpwd (String pwd) { This. PWD =pwd; }     PublicMap<string, user>GetUser () {returnuser; }     Public voidSetUser (map<string, user>user) {         This. user =user; } @Override PublicString toString () {return"Mapdemo [id=" + ID + ", name=" + name + ", pwd=" + pwd + ", user=" + user + "]"; }     PublicMapdemo () {Super(); //TODO auto-generated Constructor stub    }     PublicMapdemo (intID, string name, string pwd, map<string, user>user) {        Super();  This. ID =ID;  This. Name =name;  This. PWD =pwd;  This. user =user; }}
View Code

3. Configuration files

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p"Xmlns:util= "Http://www.springframework.org/schema/util"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 http ://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd "> < Context:property-placeholder location= "classpath:jdbc.properties"/> <bean id= "user1"class= "com. User "> <property name=" id "value=" 1 "></property> <property name=" name "value=" Name1 "&GT;&L t;/property> <property name= "pwd" value= "pwd1" ></property> </bean> <bean id= "User2"class= "com. User "> <property name=" id "value=" 2 "></property> <property name=" name "value=" Name2 "&GT;&L t;/property> <property name= "pwd" value= "pwd2" ></property> </bean> <bean id= "User3"class= "com. User "> <property name=" id "value=" 3 "></property> <property name=" name "value=" Name3 "&GT;&L t;/property> <property name= "pwd" value= "PWD3" ></property> </bean> < injection--&G!--Map Collection    T <util:map id= "User" > <entry key= "1" value-ref= "user1"/> <entry key= "2" value-ref= "User2"/&gt        ; <entry key= "2" value-ref= "User3"/> </util:map> <bean id= "Mapdemo"class= "com. Mapdemo "> <property name=" id "value=" 001 "/> <property name=" name "value=" Tom "/> < Property name= "pwd" value= "123456"/> <!--to relate the user class to the Demo2 class--<property name= "user" ref= "user "/> </bean></beans>
View Code

4. Test code

 Packagetest;Importcom. Mapdemo;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by Administrator on 2016/12/4.*/ Public classMapdemotest { Public Static voidMain (string[] args) {Classpathxmlapplicationcontext cx=NewClasspathxmlapplicationcontext ("Demo1.xml"); Mapdemo Mapdemo= (Mapdemo) cx.getbean ("Mapdemo");    System.out.println (Mapdemo); }}
View Code

Note: There was a problem at the time of the test. Because there is a class name that modifies the user. Therefore, the property name and the method name are also replaced synchronously in the Mapdemo, but it fails when injected. Prompts the user for a question.

FIX: Delete set, get, regenerate.

Spring Injection-map

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.