"Spring" Spring relies on injected IOC with setpoint injection setter

Source: Internet
Author: User

In fact, the title of such a tall noun, just a question, about the spring applicationcontext.xml in the following statement is what meaning?

<property name= "Aservice" ref= "Aservice"/>
This type of statement exists in the configuration of SSH because spring's so-called core mechanism is spring dependency injection into the IOC. Here's an example to illustrate the problem:

Paste the directory structure first:


Create a new Java project in Eclipse, not javaweb, in this Java configuration Good Spring3.0.5, this in "Spring" Spring3.0.5 download, Configuration and HelloWorld "(click Open link) has said, Do not repeat here, and then first create a new applicationcontext.xml in the SRC folder, the contents are 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/ Beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd "><!-- Specify the Aservice class in A.java, from the service package Aservice.java--><!--A.java without Aservice aservice=new aservice (); This statement-- <bean id= "Ajava" class= "A" ><property name= "Aservice" ref= "Aservice"/></bean><bean id= "AService "Class=" service. Aservice "/></beans>
Then SRC creates a new A.java, the contents of which are as follows:

Import service.*;p Ublic class A {private Aservice aservice;public void Setaservice (Aservice aservice) {this.aservice = ASe Rvice;} public void print () {System.out.println ("A.java in the Print () method with Aservice:" + aservice.print ());}}
After that, create a new service package in SRC and create a new Aservice.java in the service with the following code:

Package Service;public class Aservice {public String print () {return "hehe";}}
Then in the SRC package to create a new Springtest.java, this Java will contain the main function, is the entry of the program, the code is as follows:

Import Org.springframework.context.*;import org.springframework.context.support.*;p ublic class SpringTest {public static void Main (string[] args) {//Initialize Springapplicationcontext applicationcontext=new classpathxmlapplicationcontext ("Applicationcontext.xml");//inject A.java for this Springtest.java class, A A=applicationcontext.getbean ("Ajava", A.class);// After injection you can use the Class A a.print ();}}
This will run and the result is as follows:


So, what's the problem with the program above? That is to say, because spring relies on the value injected into the IOC to inject the setter and spring's own characteristics, so that the entire project does not have a new statement, easy to meet the specifications of the architecture design. Because it has the following process of passing the value:


It is very obvious that, of course, this can be done with a simple System.out.println statement, even if you want to use a class, you can also use import and static statements. However spring in the Javaweb project, a large number of Hibernate statements and database query methods, Struts action method, can be managed.

Assuming that the above A.java is a Java file about the action of Struts2, Aservice.java is the database business class for Hibernate. Well, you may not think spring is in the hehe.

"Spring" Spring relies on injected IOC with setpoint injection setter

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.