Java's Spring Learning 1--spring Reference and attribute (Setter,getter) injection

Source: Internet
Author: User

1. Directory structure

Project->src->main->java->com.test.ppmoney set Java folder for code in Project Structure modules select the appropriate directory and click the Sources menu

Project->src->main->resources set the resources folder for the resource in Project Structure, select the appropriate directory in the modules, and then click the Assets menu.

2. Adding spring dependencies to the dependent file Pom.xml

        <!--https://mvnrepository.com/artifact/org.springframework/spring-context-        < dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context< /artifactid>            <version>4.3.7.RELEASE</version>        </dependency>

3. Add the appropriate reference in the code file

Import Org.springframework.context.ApplicationContext; import Org.springframework.context.support.ClassPathXmlApplicationContext;

4. Add the appropriate bean file

Create a new file under the resources file, such as Bean1.xml

Enter the appropriate code as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"              xmlns= "Http://www.springframework.org/schema/beans"             xsi:schemalocation= "http// Www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd ">             class=" Com.test.com.Teacher ">         <property name=" name "value= "Yang"/>     </bean> </beans>

5. Enter the following code in the main program

classTeacherextendsobservable{ PublicTeacher () {} PublicTeacher (String name) { This. name=name; }    PrivateString name;  Public voidStartlesson () {System.out.println (String.Format ("Teacher%s Start class", name)); Super. setchanged (); Super. Notifyobservers (); }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}

6. The caller code is as follows:

New Classpathxmlapplicationcontext ("Bean1.xml"); // read the contents of the Bean.xml         Teacher Sir=ctx.getbean ("Teacher", Teacher.  Class);         if (sir==null)        {            "Teacher Init Failed ..." );        }         Else  {
System.out.println ("Teacher Init Succ ..." );
}

Java's Spring Learning 1--spring Reference and attribute (Setter,getter) injection

Related Article

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.