- What is Bean management
- Two ways of Bean management
- xml based approach
- Create objects (xml based)
- Injection properties (xml based)
- Annotation based approach
What is Bean management
1. Create objects with spring
2.Spring injection properties
Two ways of Bean management
1) Implementation of configuration file based on xml
2) Annotation based implementation
xml based approach
Create objects (xml based)
When creating an object, the parameterless constructor is executed by default
Injection properties (xml based)
Dependency injection (DI)
package com.fly.spring5.entity; public class Person { private int age; private String name; public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public void hello() { System.out.println("hello world"); } @Override public String toString() { return "Person{" + "age=" + age + ", name='" + name + '\'' + '}'; } }
Configure property injection in the configuration 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"> <!--id It's your own alias,class Is the full class name of the class --> <!--ApplicationContent.xml--> <bean id="person" class="com.fly.spring5.entity.Person"> <property name="age" value="12"/> <property name="name" value="Zhang San"/> </bean> </beans>
Test and execute test1
package com.fly.spring5.test; import com.fly.spring5.entity.Person; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestPerson { @Test public void testHello() { //This will not be written in the actual process. This is for testing. If the configuration file is not under the class path, use FileSystemXmlApplicationContext() //1. Load profile ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml"); //2. Get the object created in the configuration file. The value of the first parameter should be consistent with the id of the bean Person person = context.getBean("person", Person.class); person.hello(); } @Test public void test1() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml"); Person person = context.getBean("person", Person.class); System.out.println(person); } }
Injection using parameterized constructs
Add a parameter constructor to the Person class
public Person(int age, String name) { this.age = age; this.name = name; }
Modify profile
<?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"> <!--id It's your own alias,class Is the full class name of the class --> <!--ApplicationContent.xml--> <bean id="person" class="com.fly.spring5.entity.Person"> <constructor-arg name="age" value="12"/> <constructor-arg name="name" value="Zhang San"/> </bean> </beans>
test
@Test public void test1() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml"); Person person = context.getBean("person", Person.class); System.out.println(person); }
p namespace
Add p namespace to configuration file
Modify profile
<?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"> <!--id It's your own alias,class Is the full class name of the class --> <!--ApplicationContent.xml--> <bean id="person" class="com.fly.spring5.entity.Person" p:age="12" p:name="Zhang San"> </bean> </beans>
test
@Test public void test1() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml"); Person person = context.getBean("person", Person.class); System.out.println(person); }
Inject special type attributes
1)null
Modify profile
<?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"> <!--id It's your own alias,class Is the full class name of the class --> <!--ApplicationContent.xml--> <bean id="person" class="com.fly.spring5.entity.Person" p:age="12"> <property name="name"> <null/> </property> </bean> </beans>
test
@Test public void test1() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml"); Person person = context.getBean("person", Person.class); System.out.println(person); }
2) The value contains special characters
1 escape special characters, such as & lt, & gt
2 using CDATA
Modify profile
<?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"> <!--id It's your own alias,class Is the full class name of the class --> <!--ApplicationContent.xml--> <bean id="person" class="com.fly.spring5.entity.Person" p:age="12"> <property name="name"> <value><![CDATA[<<ha-ha>>]]]]></value> </property> </bean> </beans>
test
@Test public void test1() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml"); Person person = context.getBean("person", Person.class); System.out.println(person); }
Inject external bean s
Create a new PersonDao interface, implementation class and PersonService class
package com.fly.spring5.dao; public interface PersonDao { void show(); }
package com.fly.spring5.dao.impl; import com.fly.spring5.dao.PersonDao; public class PersonDaoImpl implements PersonDao { @Override public void show() { System.out.println("======PersonDaoImpl show====="); } }
package com.fly.spring5.service; import com.fly.spring5.dao.PersonDao; public class PersonService { public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } private PersonDao personDao; public void show() { System.out.println("=====PersonService show======"); personDao.show(); } }
configuration 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" 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"> <!--id It's your own alias,class Is the full class name of the class --> <!--ApplicationContent.xml--> <bean id="person" class="com.fly.spring5.entity.Person" p:age="12"> <property name="name"> <value><![CDATA[<<ha-ha>>]]]]></value> </property> </bean> <bean id="personDaoImpl" class="com.fly.spring5.dao.impl.PersonDaoImpl"/> <bean id="personService" class="com.fly.spring5.service.PersonService"> <property name="personDao" ref="personDaoImpl"/> </bean> </beans>
test
@Test public void test2() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml"); PersonService personService = context.getBean("personService", PersonService.class); personService.show(); }
Inject internal bean s
New Grade class and Student class
package com.fly.spring5.entity; public class Grade { @Override public String toString() { return "Grade{" + "gradeName='" + gradeName + '\'' + '}'; } public void setGradeName(String gradeName) { this.gradeName = gradeName; } private String gradeName; }
package com.fly.spring5.entity; public class Student { private int stuAge; private String stuName; private Grade grade; @Override public String toString() { return "Student{" + "stuAge=" + stuAge + ", stuName='" + stuName + '\'' + ", grade=" + grade + '}'; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } public void setStuName(String stuName) { this.stuName = stuName; } public void setGrade(Grade grade) { this.grade = grade; } }
configuration 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" 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"> <!--id It's your own alias,class Is the full class name of the class --> <!--ApplicationContent.xml--> <bean id="person" class="com.fly.spring5.entity.Person" p:age="12"> <property name="name"> <value><![CDATA[<<ha-ha>>]]]]></value> </property> </bean> <bean id="personDaoImpl" class="com.fly.spring5.dao.impl.PersonDaoImpl"/> <bean id="personService" class="com.fly.spring5.service.PersonService"> <property name="personDao" ref="personDaoImpl"/> </bean> <bean id="student" class="com.fly.spring5.entity.Student"> <property name="stuAge" value="10"/> <property name="stuName" value="Zhang San"/> <property name="grade"> <bean class="com.fly.spring5.entity.Grade"> <property name="gradeName" value="1 grade"/> </bean> </property> </bean> </beans>
test
@Test public void test3() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml"); Student student = context.getBean("student", Student.class); System.out.println(student); }
Cascade assignment
1
<bean id="student" class="com.fly.spring5.entity.Student"> <property name="stuAge" value="10"/> <property name="stuName" value="Zhang San"/> <property name="grade" ref="grade"/> </bean> <bean id="grade" class="com.fly.spring5.entity.Grade"> <property name="gradeName" value="1 grade"/> </bean>
2
<bean id="student" class="com.fly.spring5.entity.Student"> <property name="stuAge" value="10"/> <property name="stuName" value="Zhang San"/> <property name="grade" ref="grade"/> <property name="grade.gradeName" value="1 grade"/> </bean> <bean id="grade" class="com.fly.spring5.entity.Grade"> </bean>
The object to be operated in this way should have a get method
Inject collection type properties
Inject collection properties of basic data types
Modify Student class
package com.fly.spring5.entity; import java.util.Arrays; import java.util.List; import java.util.Map; public class Student { private int stuAge; private String stuName; private Grade grade; private String[] hobbies; private List<String> list; private Map<String,String> map; public void setHobbies(String[] hobbies) { this.hobbies = hobbies; } public void setList(List<String> list) { this.list = list; } public void setMap(Map<String, String> map) { this.map = map; } public Grade getGrade() { return grade; } @Override public String toString() { return "Student{" + "stuAge=" + stuAge + ", stuName='" + stuName + '\'' + ", grade=" + grade + ", hobbies=" + Arrays.toString(hobbies) + ", list=" + list + ", map=" + map + '}'; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } public void setStuName(String stuName) { this.stuName = stuName; } public void setGrade(Grade grade) { this.grade = grade; } }
Modify profile
<?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"> <!--id It's your own alias,class Is the full class name of the class --> <!--ApplicationContent.xml--> <bean id="person" class="com.fly.spring5.entity.Person" p:age="12"> <property name="name"> <value><![CDATA[<<ha-ha>>]]]]></value> </property> </bean> <bean id="personDaoImpl" class="com.fly.spring5.dao.impl.PersonDaoImpl"/> <bean id="personService" class="com.fly.spring5.service.PersonService"> <property name="personDao" ref="personDaoImpl"/> </bean> <bean id="student" class="com.fly.spring5.entity.Student"> <property name="stuAge" value="10"/> <property name="stuName" value="Zhang San"/> <property name="grade" ref="grade"/> <property name="grade.gradeName" value="1 grade"/> <property name="hobbies"> <array> <value>arr1</value> <value>arr2</value> <value>arr3</value> </array> </property> <property name="list"> <list> <value>list1</value> <value>list2</value> <value>list3</value> </list> </property> <property name="map"> <map> <entry key="k1" value="v1"/> <entry key="k2" value="v2"/> <entry key="k3" value="v3"/> </map> </property> </bean> <bean id="grade" class="com.fly.spring5.entity.Grade"> </bean> </beans>
test
@Test public void test3() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml"); Student student = context.getBean("student", Student.class); System.out.println(student); }
Injection object type collection properties
Create School class
package com.fly.spring5.entity; import java.util.List; public class School { private List<Grade> grades; @Override public String toString() { return "School{" + "grades=" + grades + '}'; } public void setGrades(List<Grade> grades) { this.grades = grades; } }
Modify profile
<bean id="grade" class="com.fly.spring5.entity.Grade"> <property name="gradeName" value="1 grade"/> </bean> <bean id="grade2" class="com.fly.spring5.entity.Grade"> <property name="gradeName" value="2 grade"/> </bean> <bean id="school" class="com.fly.spring5.entity.School"> <property name="grades"> <list> <ref bean="grade"/> <ref bean="grade2"/> </list> </property> </bean>
test
@Test public void test4() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml"); School school = context.getBean("school", School.class); System.out.println(school); }
Extract set injection part
Introducing the namespace util into the spring configuration file
Modify profile
<bean id="grade" class="com.fly.spring5.entity.Grade"> <property name="gradeName" value="1 grade"/> </bean> <bean id="grade2" class="com.fly.spring5.entity.Grade"> <property name="gradeName" value="2 grade"/> </bean> <util:list id="gradeList"> <ref bean="grade"/> <ref bean="grade2"/> </util:list> <bean id="school" class="com.fly.spring5.entity.School"> <property name="grades" ref="gradeList"> </property> </bean>
test
@Test public void test4() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml"); School school = context.getBean("school", School.class); System.out.println(school); }
FactoryBean
1) Spring has two types of beans, a normal bean and a factory bean
2) Ordinary bean: the bean type defined in the configuration file is the return type
3) Factory bean: the bean type defined in the configuration file can be different from the return type
Create MyBean class
package com.fly.spring5.entity; import org.springframework.beans.factory.FactoryBean; public class MyBean implements FactoryBean<Grade> { @Override public Grade getObject() throws Exception { Grade grade = new Grade(); grade.setGradeName("1 grade"); return grade; } @Override public Class<?> getObjectType() { return null; } }
configuration file
<bean id="myBean" class="com.fly.spring5.entity.MyBean"/>
test
@Test public void test5() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml"); Grade myBean = context.getBean("myBean", Grade.class); System.out.println(myBean); }
Bean scope
In Spring, bean s are singletons by default
test
@Test public void test6() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml"); School school = context.getBean("school", School.class); School school2 = context.getBean("school", School.class); System.out.println(school == school2); }
How to set single instance or multi instance?
Modify the scope attribute of the bean tag. The default value is singleton. prototype indicates that it is a multi instance object. After modifying the configuration file, run test6 again
<bean id="school" class="com.fly.spring5.entity.School" scope="prototype"> <property name="grades" ref="gradeList"> </property> </bean> <bean id="myBean" class="com.fly.spring5.entity.MyBean"/>
Bean lifecycle
1) Create bean instance through constructor (no parameter construction)
2) Set values for bean properties and references to other beans (call the set method)
3) Pass the bean instance to the bean post processor's method postProcessBeforeInitialization
4) Call the initialization method of the bean (the method that needs configuration initialization)
5) Pass the bean instance to the bean post processor method postProcessAfterInitialization
6) The bean is ready to use (the object is obtained)
7) When the container is closed, the bean's destroy method is called
xml Automatic Assembly
1) According to the attribute name injection, the id value of the injected bean is the same as the class attribute name
Modify profile
<bean id="student" class="com.fly.spring5.entity.Student" autowire="byName"> <property name="stuAge" value="10"/> <property name="stuName" value="Zhang San"/> <property name="hobbies"> <array> <value>arr1</value> <value>arr2</value> <value>arr3</value> </array> </property> <property name="list"> <list> <value>list1</value> <value>list2</value> <value>list3</value> </list> </property> <property name="map"> <map> <entry key="k1" value="v1"/> <entry key="k2" value="v2"/> <entry key="k3" value="v3"/> </map> </property> </bean> <bean id="grade" class="com.fly.spring5.entity.Grade"> <property name="gradeName" value="1 grade"/> </bean>
test
@Test public void test3() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml"); Student student = context.getBean("student", Student.class); System.out.println(student); }
2) Inject according to the attribute type, but only one Bean of this type can be created
External properties file
Introduce the jar package required by the Druid connection pool
Create a file to store information about the database
Introduce context namespace
Modify profile
<?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" xmlns:util="http://www.springframework.org/schema/util" 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/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--id It's your own alias,class Is the full class name of the class --> <!--ApplicationContent.xml--> <bean id="person" class="com.fly.spring5.entity.Person" p:age="12"> <property name="name"> <value><![CDATA[<<ha-ha>>]]]]></value> </property> </bean> <bean id="personDaoImpl" class="com.fly.spring5.dao.impl.PersonDaoImpl"/> <bean id="personService" class="com.fly.spring5.service.PersonService"> <property name="personDao" ref="personDaoImpl"/> </bean> <bean id="student" class="com.fly.spring5.entity.Student"> <property name="stuAge" value="10"/> <property name="stuName" value="Zhang San"/> <property name="grade" ref="grade"/> <property name="grade.gradeName" value="1 grade"/> <property name="hobbies"> <array> <value>arr1</value> <value>arr2</value> <value>arr3</value> </array> </property> <property name="list"> <list> <value>list1</value> <value>list2</value> <value>list3</value> </list> </property> <property name="map"> <map> <entry key="k1" value="v1"/> <entry key="k2" value="v2"/> <entry key="k3" value="v3"/> </map> </property> </bean> <bean id="grade" class="com.fly.spring5.entity.Grade"> <property name="gradeName" value="1 grade"/> </bean> <bean id="grade2" class="com.fly.spring5.entity.Grade"> <property name="gradeName" value="2 grade"/> </bean> <util:list id="gradeList"> <ref bean="grade"/> <ref bean="grade2"/> </util:list> <bean id="school" class="com.fly.spring5.entity.School" scope="prototype"> <property name="grades" ref="gradeList"> </property> </bean> <bean id="myBean" class="com.fly.spring5.entity.MyBean"/> <!--Import external properties file--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--Configure connection pool--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${prop.driverClass}"/> <property name="url" value="${prop.url}"/> <property name="username" value="${prop.userName}"/> <property name="password" value="${prop.password}"/> </bean> </beans>
Annotation based approach
Common notes
1)@Component
2)@Service
3)@Controller
4)@Repository
Required dependencies
Enable annotation scanning in configuration file
<context:component-scan base-package="com.fly.spring5"/>