Spring Construction Injection

Source: Internet
Author: User

Sping Structure Architecture 4 Core components

Beans:bean is the bject data that wraps our application custom objects object.

Core:context Some of the tools that are needed to establish and maintain relationships between beans. such as the loading of resources, abstraction of resources and so on.

Context:context is a collection of bean relationships.

expression:spring expression Language.

Construction Injection:

The premises are constructed

Student class

Public classStudent {
PublicString toString () {
"Name= '"+name+'\ ''+
", age="+ Age+
", car="+Car+
'} ';
private int Age;

PublicStudent (String name,intAge, car car) {
This.name= name;
This. Age= age;
This.Car= Car;
PublicString GetName () {
Public voidSetName (String name) {
This.name= name;
public intGetage () {
return Age;
Public voidSetage (intAge) {
This. Age= age;
PublicCar Getcar () {
Public voidSetcar (car car) {
This.Car= Car;

Car class

Public classCar {

PublicString toString () {
"Brand= '"+Brand+'\ ''+
", color= '"+Color+'\ ''+
'} ';
PublicString Getbrand () {
Public voidSetbrand (String brand) {
This.Brand= Brand;
PublicString GetColor () {
Public voidSetColor (String color) {
This.Color= color;



<?XML version= "1.0"encoding= "UTF-8"?>
<Beansxmlns= "Http://www.springframework.org/schema/beans"
       xmlns:XSI= "Http://www.w3.org/2001/XMLSchema-instance"
       xmlns:Context= "Http://www.springframework.org/schema/context"
       XSI: schemalocation= "Http://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/http Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<BeanID= "Car"class= "Cn.happy.spring03xmldi.Car">
< Propertyname= "Brand"value= "Porsche"></ Property>
< Propertyname= "Color"value= "Blue"></ Property></Bean>
<!--construction injection --
 <BeanID= "Stu"class= "Cn.happy.spring03xmldi.Student">
<Constructor-argIndex= "0"value= "Wang Wang"></Constructor-arg>
<Constructor-argIndex= "1"value= "+"></Constructor-arg>
<Constructor-argIndex= "2"ref= "Car"></Constructor-arg></Bean>

Test class:

/Construction Injection
public void test01 () {
ApplicationContext ctx=New classpathxmlapplicationcontext ("Applicationcontextspring03xmldl.xml" );
Student stu= (Student) Ctx.getbean ("Stu");
System. out. println (Stu);

Results :

P Namespace Introduction

xmlns: P = "http://www.springframework.org/schema/p"

<!--P-namespace injection--
<Bean id= "Stu" class= "Cn.happy.spring03xmldi.Student" p : Name = "Egg" P : Age = "+" P : Car-ref = "Car"></Bean>

Spring Construction Injection

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.