Spring framework Demo1 --- bean Management: inject attributes into classes based on XML configuration files
Normally:
class demo1{ String data1="Laughing lazy cat"; }
This method is often encountered when we learn Java, but this disadvantage is that the coupling degree is particularly high. It can be said that it leads the whole body. As long as the data is modified on it, some operations done through it must be modified. It is very short, At this time, we can solve the above problems through Bean management, the basic operation in the Spring framework.
1: Property injection
1: Inject attribute values through the set method
An attribute class, a test class, and an XML file
Attribute class:
public class set_inject { private String name; public void setName(String name) { this.name = name; } public void print(){ System.out.println(name); } }
Configuration file 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="set_inject" class="com.Property_injection.set_inject"> <property name="name" value="Laughing lazy cat"></property> </bean> </beans>
Test class:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class set_inject_test { public static void test_Demo(){ ApplicationContext context = new ClassPathXmlApplicationContext("com/Property_injection/set_inject.xml"); set_inject test1 = context.getBean("set_inject",com.Property_injection.set_inject.class); test1.print(); } public static void main(String args[]){ test_Demo(); } }
Output result: lazy cat who can laugh
2: Injection is carried out by the method of parametric construction
Attribute class:
public class parametric_inject { public void add() { System.out.println("Lazy cat who can laugh, different fireworks"); } private String name; private String author; private String address; public parametric_inject(String name,String author,String address){ this.name=name; this.author=author; this.address = address; } public void testdemo(){ System.out.println(name+"::::"+author+":::"+address); } }
Configuration file 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="parametric_inject" class="com.Property_injection.parametric_inject"> <constructor-arg name="name" value="Property injection"></constructor-arg> <constructor-arg name="author" value="Laughing lazy cat"></constructor-arg> <constructor-arg name="address" value="CSDN"></constructor-arg> </bean> </beans>
Test class:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class set_inject_test { public static void test_Demo(){ ApplicationContext context = new ClassPathXmlApplicationContext("com/Property_injection/parametric_inject.xml"); parametric_inject test1 = context.getBean("parametric_inject",com.Property_injection.parametric_inject.class); test1.testdemo(); } public static void main(String args[]){ test_Demo(); } }
Output result: attribute injection:: laughing lazy cat:: CSDN
3: Inject through the P name tag (this is not often understood)
Attribute class:
package com.Property_injection; public class label_inject { private String name; private String author; public void setName(String name) { this.name = name; } public void setAuthor(String author) { this.author = author; } public void print(){ System.out.println(name+"::::"+author); } }
Configuration file 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.xsd"> <bean id="label_inject" class="com.Property_injection.label_inject" p:name="frame foundation" p:author="Laughing lazy cat"></bean> </beans>
Test class:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class set_inject_test { public static void test_Demo(){ ApplicationContext context = new ClassPathXmlApplicationContext("com/Property_injection/plabel_inject.xml"); label_inject test1 = context.getBean("label_inject",com.Property_injection.label_inject.class); test1.print(); } public static void main(String args[]){ test_Demo(); } }
Output result: Framework Foundation:: lazy cat who can laugh
4: Injection through external bean methods
Attribute class I: interface implementation:
interface demo1{ public void print(); } public class externalbean_inject implements demo1 { @Override public void print() { System.out.println("A lazy cat who can laugh, a different flower fire"); } }
Attribute class II:
public class externalbean { private externalbean_inject externalbean_inject; public void setExternalbean_inject(com.Property_injection.externalbean_inject externalbean_inject) { this.externalbean_inject = externalbean_inject; } public void out(){ externalbean_inject.print(); System.out.println("A different flower fire, a lazy cat who can laugh"); } }
Configuration file xml file:
<?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 id="d" class="com.Property_injection.externalbean"> <property name="externalbean_inject" ref="exteralbeann_inject"></property> </bean> <bean id="exteralbeann_inject" class="com.Property_injection.externalbean_inject"></bean> </beans>
Test class:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class set_inject_test { public static void test_Demo(){ ApplicationContext context = new ClassPathXmlApplicationContext("com/Property_injection/exteralbean.xml"); externalbean test1 = context.getBean("d",com.Property_injection.externalbean.class); test1.out(); } public static void main(String args[]){ test_Demo(); } }
5: Injection through internal bean methods
Internal class:
public class interiorbean_inject { String data1; public void setData1(String data1) { this.data1 = data1; } }
Internal class 2:
public class interiorbean { private String data2; private String data3; private interiorbean_inject interiorbean_inject; public void setData2(String data2) { this.data2 = data2; } public void setData3(String data3) { this.data3 = data3; } public void setInteriorbean_inject(com.Property_injection.interiorbean_inject interiorbean_inject) { this.interiorbean_inject = interiorbean_inject; } public void demo1(){ System.out.println(interiorbean_inject.data1+"::::"+data2+":::"+data3); } }
Configuration file 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="interiorbean" class="com.Property_injection.interiorbean"> <property name="data2" value="Laughing lazy cat 2"></property> <property name="data3" value="Laughing lazy cat 3"></property> <property name="interiorbean_inject"> <bean id="interiorbean_inject" class="com.Property_injection.interiorbean_inject"> <property name="data1" value="Laughing lazy cat 1"></property> </bean> </property> </bean> </beans>
Test class:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class set_inject_test { public static void test_Demo(){ ApplicationContext context = new ClassPathXmlApplicationContext("com/Property_injection/interiorbean.xml"); interiorbean test1 = context.getBean("interiorbean",com.Property_injection.interiorbean.class); test1.demo1(); } public static void main(String args[]){ test_Demo(); } }
6: Attribute injection through cascade assignment
1) : the first method is modified under the premise of the fifth method (internal bean). Only the configuration file needs to be modified, and other files remain unchanged. The output result is the same as the above result
<?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 id="interiorbean" class="com.Property_injection.interiorbean"> <property name="data2" value="Laughing lazy cat 2"></property> <property name="data3" value="Laughing lazy cat 3"></property> <property name="interiorbean_inject" ref="interior"></property> </bean> <bean id="interior" class="com.Property_injection.interiorbean_inject"> <property name="data1" value="Laughing lazy cat 1"></property> </bean> </beans>
2) : the second writing method: it is also modified under the premise of the fifth method
Attribute class 1:
public class interiorbean_inject { String data1; public void setData1(String data1) { this.data1 = data1; } }
Attribute class 2:
public class interiorbean { private String data2; private String data3; private interiorbean_inject interiorbean_inject; public void setData2(String data2) { this.data2 = data2; } public void setData3(String data3) { this.data3 = data3; } public void setInteriorbean_inject(com.Property_injection.interiorbean_inject interiorbean_inject) { this.interiorbean_inject = interiorbean_inject; } public com.Property_injection.interiorbean_inject getInteriorbean_inject() { return interiorbean_inject; } public void demo1(){ System.out.println(interiorbean_inject.data1+"::::"+data2+":::"+data3); } }
Configuration file 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="interiorbean" class="com.Property_injection.interiorbean"> <property name="data2" value="Laughing lazy cat 2"></property> <property name="data3" value="Laughing lazy cat 3"></property> <property name="interiorbean_inject" ref="interiorbean_inject"></property> <property name="interiorbean_inject.data1" value="Cascade assignment"></property> </bean> <bean id="interiorbean_inject" class="com.Property_injection.interiorbean_inject"> </bean> </beans>
Test class:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class set_inject_test { public static void test_Demo(){ ApplicationContext context = new ClassPathXmlApplicationContext("com/Property_injection/interiorbean.xml"); interiorbean test1 = context.getBean("interiorbean",com.Property_injection.interiorbean.class); test1.demo1(); } public static void main(String args[]){ test_Demo(); } }
Output result: cascade assignment:: laughing lazy cat 2::: laughing lazy cat 3