Java Combat (vii)--------MYECLIPSE build Spring's development environment Spring Framework Configuration

Source: Internet
Author: User
Tags getmessage java web

To develop a feature based on spring, you need to add the development support environment for spring. The so-called spring support environment is to add the jar, TLD, and XML files needed for spring in a Java Web application, and with these files we can develop the spring-related functional code.


This article has two content:

1.Spring Environment Construction

Sample----test using 2.Spring


1.Spring Environment Construction

The build steps are as follows:

Right-select MyEclipse-----ADD Spring capabilities for project projects

A




You can select the appropriate class library


And the configuration of the path


When the configuration is complete, click the Finish button to complete the add to the spring support environment.



Sample----test using 2.Spring


2.1 Adding bean Configuration to Applicationcontext.xml


First, add the bean configuration element in Applicationcontext.xml, as shown in program 2.1.


Program 2.1 Applicationcontext.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns=
	"http://www.springframework.org/schema/" Beans "
	xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
	xmlns:p=" http://www.springframework.org/ schema/p "
	xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.springframework.org/ Schema/beans/spring-beans-3.0.xsd ">


<bean id=" HelloWorld "class=" Test.helloworld ">
<property Name= "message" >
<value>World</value>
</property>
</bean>

Here we have a Bean object with the id "HelloWorld", the class used is Test.helloworld, and an injected value "world" is set for the class's property "message". This is one of the most commonly used configuration forms for <bean> elements.


2.2 New Bean Class--helloworld.java


New Bean class Test.HelloWorld.java below. Depending on the XML file configuration above, the class needs to include a property message, so add a variable message of type string and add the Getter/setter function, then add a test function execute () To output the injected parameter variable message, as shown in program 2.2.


Program 2.2 Helloworld.java

Package Test;


public class HelloWorld {


	    protected String message;
	    Public String GetMessage () {return message
	;
	}
	    public void Setmessage (String message) {
	this.message = message;
	}
	    public void Execute () {
	System.out.println ("Hello" + getMessage () + "!");
	}
	


2.3 Run test class Test.java

Then, write the test class Test.java. Using Filesystemxmlapplicationcontext to read the XML configuration file ApplicationContext, the variable ctx of the ApplicationContext type is returned. The Getbean () function of CTX is then used to obtain the configured Bean object "HelloWorld", which is the name of the bean configured in the XML file. The object that is obtained is forced type conversion into the HelloWorld type, named Hello, and calls execute () of hello to perform the output. The complete code for implementation is shown in program 2.3.



Program 2.3 Test Class Test.java

Package Test;


Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.FileSystemXmlApplicationContext;


public class Springtest {public
	static void Main (string[] args) {
		ApplicationContext ctx = new Filesystemxmlappli Cationcontext (
		"Src/applicationcontext.xml");
		HelloWorld Hello = (HelloWorld) ctx.getbean ("HelloWorld");
		
		Hello.execute ();
		}


Running the program, if the output "Hello world" indicates that the environment already supports spring.



If a "Java.io.filenotfoundexception:webroot/web-inf/applicationcontext.xml" exception occurs, Indicates that the Applicationcontext.xml resource file is placed in a wrong path, which needs to be the path relative to the current project demo.

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.