7.3.2: Set Value Injection

Source: Internet
Author: User
Tags chop

Set Value InjectionThe IOC container uses the setter method of the attribute to inject the dependent instance. This injection method is simple and intuitive, so it is widely used in spring dependency injection.

Spring recommendationInterface-Oriented ProgrammingIn this way, the specification and implementation can be better separated to provide better decoupling. For a Java EE application, whether it is a DaO component or a business logic component, you should first define an interface that defines the functions that the component should implement, however, the implementation of the function is provided by its implementation class.

Person. Java:

public interface Person {public void useAxe();}

Axe. Java:

public interface Axe {public String chop();}

Chinese. Java:

public class Chinese implements Person {private Axe axe;public void setAxe(Axe axe) {this.axe = axe;}@Overridepublic void useAxe() {System.out.println(axe.chop());}}

Stoneaxe. Java:

Public class stoneaxe implements axe {@ overridepublic string chop () {return "";}}

Steelaxe. Java:

Public class steelaxe implements axe {@ overridepublic string chop () {return "cutting firewood well ";}}

Bean. 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:context="http://www.springframework.org/schema/context"        xmlns:tx="http://www.springframework.org/schema/tx"        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx                http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">        <bean id="chinese" class="com.bean.Chinese">        <property name="axe" ref="stoneAxe"/>    </bean>        <bean id="stoneAxe" class="com.bean.StoneAxe"></bean>    <bean id="steelAxe" class="com.bean.SteelAxe"></bean></beans>

Test. Java:

public class Test {public static void main(String[] args) {ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");Person p=(Person) ctx.getBean("chinese",Person.class);p.useAxe();}}

Run test. Java and the console outputs: it takes a long time to cut firewood.

Modify the ref value of <property name = "Axe" ref = "stoneaxe"/> in the bean. xml file to steelaxe. Run test. Java again, and the console outputs: cutting firewood with a steel axe is fast.

In the configuration file, spring ConfigurationBean instanceTwo attributes are usually specified;

① ID:Specifies the unique identifier of the bean. The program accesses the bean instance through the ID attribute value.

② Class:Specifies the bean implementation class. You cannot use interfaces here. You must use implementation classes.

We can see the flexibility of spring bean management. The dependencies between bean and bean are organized in the configuration file, rather than written in the code. By specifying the configuration file, spring can precisely inject attributes into each bean. Therefore, the Class Attribute Value of the <bean.../> element in the configuration file cannot be an interface or a real implementation class.

As described above, it is not difficult to findThree basic points of using Spring IoC containers:

(1) The components of the application are interface-oriented.For interface-oriented programming, the coupling between components can be upgraded to the interface level, which facilitates the extension of the project in the later stage.

(2) components of an application are generated and initialized by the spring container instead of the program.

(3) Spring uses the configuration file or annotation to manage bean implementation classes and dependencies. Spring containers use reflection to create instances based on the configuration file and inject dependencies to them.

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.