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