A, Spring IOC (dependency injection in three ways):
1, Setter injection method
package com.jpeony.spring.setter; import com.jpeony.spring.common.HelloServiceImpl;
public class HelloWord {
private HelloService helloService; // setter方式注入Bean public void setHelloService(HelloService helloService) { this.helloService = helloService; } @Override public void selfIntroduction () { // to say hello helloService.sayHello ( "Hello everybody!" ); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Bean declaration: This is similar to bean @Bean annotations in javaConfig; For creating a class specified by the bean class attribute, and requires the use of fully qualified class name. ID specified by the bean id. If you do not show specified, the fully qualified name of the class named. eg: com.jpeony.spring.common.HelloServiceImpl # 0, # 0 which is in the form of a counter, To distinguish it from other types of the same bean. Using an automated naming is convenient, but not much practical use, it is recommended to own bean display set ID. --> <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <!-- setter注入bean --> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"> <property name="helloService" ref="helloService"/> </bean> </beans>
2, the configuration method of injection
package com.jpeony.spring.setter; import com.jpeony.spring.common.HelloServiceImpl; public class HelloWord { private HelloService helloService; // Constructor injection public HelloWord (the HelloService the helloService) { the this .helloService = the helloService; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Bean declaration: This is similar to bean @Bean annotations in javaConfig; For creating a class specified by the bean class attribute, and requires the use of fully qualified class name. ID specified by the bean id. If you do not show specified, the fully qualified name of the class named. eg: com.jpeony.spring.common.HelloServiceImpl # 0, # 0 which is in the form of a counter, To distinguish it from other types of the same bean. Using an automated naming is convenient, but not much practical use, it is recommended to own bean display set ID. --> <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <!-- 构造方法注入bean --> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"> <constructor-arg><ref bean="helloService"/></constructor-arg> </bean> </beans>
3, P injection namespace
package com.jpeony.spring.setter; import com.jpeony.spring.common.HelloServiceImpl; public class HelloWord { // Name Private String name; // Age Private String Age; // Method in class Private the HelloService helloService; public void setName (String name) { this.name = name; } public void setAge (String age) { this.age = age; } public void setHelloService(HelloService helloService) { this.helloService = helloService; } @Override public void selfIntroduction () { // to say hello helloService.sayHello ( "My name is" + name + ", this year's" + age + "years old, Hello, everyone!" ); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <!-- 引入p命名标签 --> xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <!-- p标签注入bean --> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord" p:name="明明" p:age="24" p:helloService-ref="helloService"></bean> </beans>
P-tag set injection bean
package com.jpeony.spring.setter; import com.jpeony.spring.common.HelloServiceImpl; import java.util.List; public class HelloWord { // Name Private String name; // Age Private String Age; // Method in class Private List <the HelloService> helloServices; public void setName (String name) { this.name = name; } public void setAge (String age) { this.age = age; } public void setHelloServices(List<HelloService> helloServices) { this.helloServices = helloServices; } @Override public void selfIntroduction () { // to say hello helloServices [0] .sayHello ( "My name is" + name + ", this year's" + age + "years old, Hello, everyone!" ); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <!-- 引入p命名标签 --> xmlns:p="http://www.springframework.org/schema/p" <! - introduced util name tag -> xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <bean id="helloService2" class="com.jpeony.spring.common.HelloServiceImpl"> ........... </bean> <util:list id="helloServices"> <ref bean="helloService"/> <ref bean="helloService2"/> </util:list> <!-- p标签注入bean --> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord" p:name="明明" p:age="24" p:helloServices-ref="helloServices"></bean> </beans>
Two, Spring IOC (dependency injection of common data types):
1, direct injection amount (basic data types, string)
<bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"> <property name="name" value="明明"></property> <property name="age" value="24"></property> </bean>
2, reference other components Bean (oriented programming interface)
Using the ref attribute:
<bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"> <property name="helloService" ref="helloService"></property> </bean>
Using the ref tag:
<bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"> <property name="helloService"> <ref bean="helloService" /> </property> </bean>
Use P namespace:
<! - header file with the phrase -> xmlns:p="http://www.springframework.org/schema/p" <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord" p:helloService-ref="helloService"></bean>
3, using internal Bean
<bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"> <property name="helloService"> <bean class="com.jpeony.spring.common.HelloServiceImpl" /> </property> </bean>
4, a set of attributes of type
// 对应的getter setter public class ALLCollection { private List listElement; private String[] arrayElement; private Set setElement; private Map mapElement; private Properties propsElement; public void setListElement (List listElement) { this.listElement = listElement; } public List getListElement () { return listElement; } public void setArrayElement (String[] arrayElement) { this.arrayElement= arrayElement; } public String[] getArrayElement () { return arrayElement; } public void setSetElement (Set setElement) { this .setElement = setElement; } public Set getSetElement () { return setElement; } Public void setMapElement (Map mapElement) { this .mapElement = mapElement; } Public Map getMaptElement () { return mapElement; } public void setPropsElement (Properties propsElement) { this.propsElement= propsElement; } public Properties getpropsElement () { return propsElement; } }
<bean id="collection" class="com.zxf.DO.ALLCollection"> <property name="listElement"> <list> <value>list苹果</value> <value>list香蕉</value> </list> </property> <property name="arrayElement"> <array> <value>>value</array Apple <value>array香蕉</value> </array> </property> <property name="setElement"> <set> <value>set苹果</value> <value>set香蕉</value> </set> </property> <property name="mapElement"> <map> <entry> <key><value>map1</value></key> <value>map苹果</value> </entry> <entry> <key><value>map2</value></key> <value>map香蕉</value> </entry> </map> </property> <property name="propsElement"> <props> <prop key="prop1">prop苹果</prop> <prop key="porp2">prop香蕉</prop> </props> </property> </bean>
Integrated into article: https://blog.csdn.net/joe18576558921/article/details/80973385 , https://www.cnblogs.com/DDiamondd/p/11398678.html