Dependency injection method in Spring control flipping
Dependency injection refers to the control of the relationship between objects. The application code is transferred to the external container. The Spring framework mainly provides two dependency injection methods: Set injection and constructor injection.
1: Set injection refers to defining a set method of the type to be injected in the class that accepts injection, and defining the need to inject in the parameters Elements. Set injection is a direct method of assembling Bean properties, but one disadvantage of Set injection is that it assumes that all variable properties can be accessed through the set method, and cannot clearly indicate which properties are required and which properties are optional. of.
2: Constructor injection is to define a constructor method in the class that receives the injection, and define the parameters that need to be injected in the constructor method. The advantage of constructor injection is that dependencies are enforced through constructors.
Here are two ways to use it:
One: In Myeclipse (the author is using Myeclipse10 Version) Create a new project (either java project or web project)
2: Right-click the project-MyEclipse-Add Spring Capabilities (add Spring support), select version 3.0 and add the reference core class Library, click Next
Three: Choose to create a new spring bean configuration file and place it in the project src directory.
Four: Click Next, specify the Hibernate configuration page, default directly, and click Finish.
Five: Create a new class HelloWorld.java
##
package com.xiami.spring; public class HelloWorld { private String str; /** * 默认构造方法 */ public HelloWorld() { } /** * 用来进行构造注入的构造方法 * * @param str */ public HelloWorld(String str) { this.str = str; } /** * 用来进行Set注入的方法 * @param str */ public void setStr(String str) { this.str = str; } /** * 输出字符串的方法 */ public void sayHello() { System.out.println(str); } }
Six: Create a new test class Test .java
package com.xiami.spring; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //载入spring配置文件 BeanFactory bFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); HelloWorld helloWorld = (HelloWorld) bFactory.getBean("helloService"); helloWorld.sayHello(); } }
Eight: Open the applicationContext.xml file and add bean configuration
The following are two ways to add beans:
1: Using Set injection method Bean class configuration
Right-click the editing interface of applicationContext.xml - Spring - new bean to open the Bean wizard window and fill in the Bean Id (custom naming and Test. Corresponds to getBean("???") in java. Select the HelloWorld class to be injected. Click the Properties tab to create a new property for the bean.
##9: In the properties. In the wizard window, fill in the Name corresponding to the attribute name in HelloWorld.java, select value for Spring Type, select String for Type, and fill in whatever value you want for Value##. : Save applicationContext.xml. After adding the bean, the configuration file has a red mark. Readers can run Test.java to test it. It is found that the hello world string has been injected into the str variable.
<?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"> <!-- 使用Set方式注入 --> <!-- <span style="color:#ff0000;"><bean id="helloService" class="com.xiami.spring.HelloWorld" abstract="false" lazy-init="default" autowire="default"> <property name="str"> <value type="java.lang.String">hello world</value> </property> </bean></span> --> <!-- 使用构造方法方式注入 <bean id="helloService" class="com.xiami.spring.HelloWorld" abstract="false" lazy-init="default" autowire="default"> <constructor-arg> <value type="java.lang.String">构造方法注入方式</value> </constructor-arg> </bean> --> </beans>
2: Configuration of Bean class using constructor injection
In the Bean Wizard window in the above Set mode, do not select the Properties tab, change it to the Constructor Args tab, and add a new construct##. #Create parameters. Index and Java Class do not need to be filled in.
##11: When adding a constructor bean, the first one must be commented or deleted. Multiple beans with the same ID are not allowed.
The above is the detailed content of Detailed examples of Spring's two injection methods (Set and construction). For more information, please follow other related articles on the PHP Chinese website!