Spring Dependency Injection Setter injection (via get and set method injection)

Source: Internet
Author: User

Spring Dependency Injection Setter injection (via get and set method injection)

Import the necessary jar packages (Spring.jar and Commonslogging.jar)

Build Applicationcontext.xml (Spring Management Bean configuration file) in src directory

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Beans Public "-//speing//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd "><Beans>    <BeanID= "User"class= "Com.cc8w.entity.User">        < Propertyname= "Names">            <value>1111</value>        </ Property>        < Propertyname= "Ages">            <value>30</value>        </ Property>    </Bean></Beans>

2.Setter Injection

 Packagecom.cc8w;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.cc8w.entity.User; Public classTest1 { Public Static voidMain (string[] args) {//TODO auto-generated Method StubApplicationContext context =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); User User= (user) Context.getbean ("user"));    SYSTEM.OUT.PRINTLN (user); }}

Data types for Java

 Packagecom.cc8w.entity; Public classUser {PrivateString names; Private intages;  PublicString GetNames () {returnnames; }     Public voidsetnames (String names) { This. Names =names; }     Public intgetages () {returnages; }     Public voidSetages (intages) {         This. ages =ages; }        }  

Results:

Spring Dependency Injection setter injection (via get and set method 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.