Java Framework Spring Boot learning Note (vii): constructor-based Dependency injection

Source: Internet
Author: User

Writing User.java

1  Packagecom.example.spring;2 3  Public classUser {4     PrivateString name;5     PrivateInteger age;6     PrivateString Country;7 8      PublicUser (string name, Integer age, String country) {9          This. Name =name;Ten          This. Age =Age ; One          This. Country =Country; A     } -  -      PublicString toString () { the         returnName + ' is ' + age + ' years old ', living in ' +Country; -     } -}

Writing Beans.xml

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">5 6     <BeanID= "User"class= "Com.example.spring.User" >7         <Constructor-argname= "Name"type= "Java.lang.String"value= "Jacky" />8         <Constructor-argname= "Age"type= "Java.lang.Integer"value= "All"/>9         <Constructor-argname= "Country"type= "Java.lang.String"value= "China"/>Ten     </Bean> One  A </Beans>

Writing Application.java

 Packagecom.example.spring;ImportOrg.springframework.context.support.AbstractApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classApplication { Public Static voidMain (string[] args) {//Where the bean configuration file is located d:\\ideaprojects\\spring\\src\\beans.xml//using the Abstractapplicationcontext containerAbstractapplicationcontext context =NewClasspathxmlapplicationcontext ("File:d:\\ideaprojects\\spring\\src\\beans.xml"); User User= (user) Context.getbean ("user"));    SYSTEM.OUT.PRINTLN (user); }}

Run output

Jacky is years-old, living in China

Java Framework Spring Boot learning Note (vii): constructor-based Dependency 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.