Recognize Spring-1.10 from the beginning use namespace p to inject properties

Source: Internet
Author: User

In this chapter we introduce the use of namespace p to inject properties.

1.domain

Cake Category: (unchanged)

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_10;public class Cake {private final int id = index++; private static int index = 0;private String name = "";p rivate Double size = 0;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public double GetSize () {return size;} public void SetSize (double size) {this.size = size;} public int getId () {return ID;} @Overridepublic String toString () {return "Create the Cake,its ID:" + ID + ", size:" + size + "inch, Name:" + Name;}}


Chef class:

Note: The configuration file uses the namespace P and cannot have the same property injected constructor

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_10;public class Chief {private static int index = 0;private Cake Cake = null;private final int id = index++;p rivate String name = "";//config file use namespace p, cannot have the following constructor//public chief (String nam E, Cake Cake) {//this.name = name;//This.cake = cake;//}public Cake getcake () {return Cake;} public int getId () {return ID;} Public String GetName () {return name;} Public Cake Makeonecake () {return Cake;} public void Setcake (Cake Cake) {this.cake = Cake;} public void SetName (String name) {this.name = name;}}

2. Test class: (unchanged)

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_10;import Org.junit.test;import Org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.applicationcontext;import org.springframework.test.context.ContextConfiguration; Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith (Springjunit4classrunner.class) @ Contextconfiguration (locations = {"/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_10/ Applicationcontext-test.xml "}) public class Chieftest {@Autowiredprivate ApplicationContext applicationcontext;@ testpublic void Testchief () {Chief chief = Applicationcontext.getbean (Chief.class); System.out.println ("Chief name:" + chief.getname ()); System.out.println (Chief.getname () + "Make a cake, cake ' s name:" + chief.makeonecake ());}}

3. Configuration files

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= " Http://www.springframework.org/schema/aop "xsi:schemalocation=" Http://www.springframework.org/schema/beans http ://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http:/ /www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/util/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/UTIL/SPRING-UTIL-3.1.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA/HTTP Www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsdhttp://www.springframework.org/schema/beans/HTTP Www.sprinGframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop.xsd "><bean id=" cake "class=" com.raylee.my_new_spring.my_new_ Spring.ch01.topic_1_10.Cake "P:name=" Jack ' s own Cake "p:size=" 7 "/><bean id=" Chief "class=" Com.raylee.my_new_ Spring.my_new_spring.ch01.topic_1_10.Chief "P:name=" Jack "p:cake-ref=" Cake "/></beans>

There are three points to note:

(1) The first letter of the name after P is case insensitive

Other words:

<bean id= "Chief" class= "Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_10.Chief" P:name= "Jack" p:cake-ref= "Cake"/>

And

<bean id= "Chief" class= "Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_10.Chief" P:came= "Jack" p:cake-ref= "Cake"/>

Same function

(2) Namespace p and property properties can be mixed

Other words:

<bean id= "Cake" class= "Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_10.Cake" P:name= "Jack ' s own cake" > <property name= "Size" value= "7" ></property></bean>

Summary: This chapter focuses on using the namespace p to inject attributes.


Catalog: http://blog.csdn.net/raylee2007/article/details/50611627

My github:https://github.com/raylee2015/my_new_spring.

Recognize Spring-1.10 from the beginning use namespace p to inject properties

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.