1. What is Bean management
bean management refers to the following two operations
1. Create object
2. Injection attribute
2. Two methods of bean management operation
xml based implementation of configuration file 1
2. Implementation based on annotation
3. Implement Bean management and attribute injection based on xml configuration file
(1). Creating objects based on xml
① : This is what we configured before
② : when creating an object, the default is to execute the parameterless construction method to complete the object
<!-- IOC Component process management javaBean --> <!-- id:Unique indicator class: Full path name of the class --> <bean id="demo" class="com.qcby.Demo" />
(2). Attribute injection based on xml
① Overview of dependency injection
IOC and DI concepts
IOC: reverse of control, which reverses the creation right of the object to Spring!!
DI: Dependency Injection, Dependency Injection, is the injection attribute
② Property
Write the attribute, provide the set method corresponding to the attribute, and write the configuration file to complete the injection of attribute value
public class User { // When writing a member attribute, you must provide the set method of the attribute //The IOC container bottom layer injects values through the set method of attributes private int age; private String name; private Demo demo; public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public void setDemo(Demo demo) { this.demo = demo; } @Override public String toString() { return "User{" + "age=" + age + ", name='" + name + '\'' + ", demo=" + demo + '}'; } }
<!‐‐DI: Dependency injection‐‐> <bean id="user" class="com.qcby.service.User" > <!--use property Complete attribute injection name:Attribute name in class value:Inject values into attributes ref: Object mapping--> <property name="age" value="18"></property> <property name="name" value="Zhang San"></property> <property name="demo" ref="demo"></property> </bean> <bean id="demo" class="com.qcby.service.Demo" />
@Test public void run1(){ //Create a spring factory and load the configuration file ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml"); //Get bean object User user = ac.getBean("user", User.class); System.out.println(user.toString()); }
③ Attribute construction method injection value
For class member variables, constructor injection.
public class Car1 { // name private String cname; // amount of money private Double money; public Car1(String cname, Double money){ this.cname = cname; this.money = money; } @Override public String toString() { return "Car{" + "cname='" + cname + '\'' + ", money=" + money + '}'; } }
<bean id="car1" class="com.qcby.Car1"> <constructor-arg name="cname" value="Benz"></constructor-arg> <constructor-arg name="money" value="350000"></constructor-arg> </bean>
@Test public void run(){ //Create a spring factory and load the configuration file ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml"); //Get bean object Car1 car = ac.getBean("car1", Car1.class); System.out.println(car.toString()); }
④ injection of arrays, sets (List,Set,Map), etc
import java.util.Arrays; import java.util.List; import java.util.Map; public class User { private String [] strs; private List<String> list; private Map<String,String> map; public void setStrs(String[] strs) { this.strs = strs; } public void setList(List<String> list) { this.list = list; } public void setMap(Map<String, String> map) { this.map = map; } @Override public String toString() { return "User{" + "strs=" + Arrays.toString(strs) + ", list=" + list + ", map=" + map + '}'; } }
<!-- Inject values into collection properties--> <bean id="user" class="com.qcby.User"> <property name="strs"> <array> <value>Alexander</value> <value>Beautiful sheep</value> </array> </property> <property name="list"> <list> <value>Xiong Da</value> <value>Xiong er</value> </list> </property> <property name="map"> <map> <entry key="dad" value="Lao Wang"/> <entry key="Son" value="Xiao Wang"/> </map> </property> </bean>
@Test public void getValue(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = (User) applicationContext.getBean("user"); System.out.println(user.toString()); }
4. Implement Bean management and attribute injection based on annotation
(1). What is annotation
① : annotation is a code special tag, format: @ annotation name (attribute name = attribute value, attribute name = attribute value...)
② : use annotations, which act on classes, methods and attributes
③ : purpose of using annotations: to simplify XML configuration
(2). Annotations provided by spring for creating objects in Bean management
@ Component ordinary class
@ Controller presentation layer
@ Service business layer
@ Repository persistence layer
*The above four functions are the same and can be used to create bean instances
(3). Creating objects with annotations
① : add @ Repository annotation on the class to be managed
package com.qcby.sevice; import org.springframework.stereotype.Repository; //@Component(value = "car") //@Controller(value = "car") //@Service(value = "car") @Repository(value = "car") public class Car { public void hello(){ System.out.println("hello"); } }
② : write a configuration file, focusing on opening annotation scanning.
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- <!–Enable annotation scanning com.qcby All classes in all packages–>--> <context:component-scan base-package="com.qcby.sevice"/> </beans>
@Test public void testDemo1() { // Create a Spring factory and load the configuration file ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class); Car us = (Car)ac.getBean("car"); us.hello(); }
(4). Implementing attribute injection with annotation
@ Value is used to inject common types (String, int, double, etc.) - Common
@ Autowired auto assemble by type by default (reference type) - Common
@ Qualifier cannot be used alone. It must be used together with @ Autowired. Name injection is mandatory
Annotations provided by @ Resource Java are also supported. Use the name attribute to inject by name
Specific code
package com.qcby.sevice; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component(value = "car") //@Controller(value = "car") //@Service(value = "car") //@Repository(value = "car") public class Car { // Annotation injection value, attribute set method can be omitted. // There is only one attribute. The name of the attribute is value. Value can be omitted @Value("Benz") private String name; @Value(value = "400000") private Double money; // There is no need to provide a set method // The annotation of automatic assembly by type has nothing to do with the id name @Autowired // Inject by the name of id. Qualifier cannot be used alone. It needs to be used together with Autowired. // @Qualifier(value = "person") // @The annotation provided by Resource Java injects the object by name, and the attribute name is name // @Resource(name = "person") private Person person; public void hello(){ System.out.println("hello"); } @Override public String toString() { return "Car{" + "name='" + name + '\'' + ", money=" + money + ", person=" + person + '}'; } }
<!-- <!–Enable annotation scanning com.qcby All classes in all packages–>--> <context:component-scan base-package="com.qcby.sevice"/>
package com.qcby.sevice; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; @Controller //@Component(value = "person") public class Person { @Value("Xiong Da") private String name; @Value("20") private String age; @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age='" + age + '\'' + '}'; } }
@Test public void testDemo1() { // Create a Spring factory and load the configuration file ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Car us = (Car)ac.getBean("car"); System.out.println(us.toString()); }
(5). IOC annotation only
Pure annotation is the main way of microservice architecture development, so it is also very important. The purpose of pure annotation is to replace all configuration files. But you need to write configuration classes.
Summary of common annotations
@The Configuration declaration is a Configuration class
@ComponentScan scans the specific package structure
package com.qcby.sevice; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; @Controller //@Component(value = "person") public class Person { @Value("Xiong Da") private String name; @Value("20") private String age; @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age='" + age + '\'' + '}'; } }
Write the configuration class and replace the ApplicationContext XML configuration file
package com.qcby; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration//Declare that this class is a configuration class @ComponentScan(value = "com.qcby.sevice")//Scan specific package structure public class SpringConfig { }
import com.qcby.SpringConfig; import com.qcby.sevice.Person; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class CarTest { @Test public void run1(){ // Create factory and load configuration class ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class); // Get object Person person = (Person) ac.getBean("person"); System.out.println(person.toString()); } }