1. Annotation development bean scope and life cycle management
The bean management has been completed using annotations earlier, and then the content implemented through configuration will be replaced with corresponding annotation implementations, including two parts: bean scope and bean life cycle.
1.1 Environment Preparation
First prepare the environment:
-
Create a Maven project
-
pom.xml add Spring dependencies
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.10.RELEASE</version> </dependency> </dependencies>
-
Add a configuration class SpringConfig
@Configuration @ComponentScan("com.itheima") public class SpringConfig { }
-
Add BookDao, BookDaoImpl classes
public interface BookDao { public void save(); } @Repository public class BookDaoImpl implements BookDao { public void save() { System.out.println("book dao save ..." ); } }
-
Create a running class App
public class App { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); BookDao bookDao1 = ctx.getBean(BookDao.class); BookDao bookDao2 = ctx.getBean(BookDao.class); System.out.println(bookDao1); System.out.println(bookDao2); } }
The final created project structure is as follows:
1.2 Scope of Bean
(1) Run the App class first, and print two identical addresses on the console, indicating that the bean is a singleton by default
(2) To turn BookDaoImpl into a non-singleton, just add the @scope annotation to its class
@Repository //@Scope sets the scope of the bean @Scope("prototype") public class BookDaoImpl implements BookDao { public void save() { System.out.println("book dao save ..."); } }
Execute the App class again and print the result:
Knowledge point 1: @Scope
name | @Scope |
---|---|
type | class annotation |
Location | Above the class definition |
effect | Set the scope of the object created by this class. It can be used to set whether the created bean is a singleton object |
Attributes | value (default): define the scope of the bean, the default value singleton (singleton), the optional value prototype (non-singleton) |
1.3 Bean life cycle
(1) Add two methods in BookDaoImpl, init and destroy, the method name can be arbitrary
@Repository public class BookDaoImpl implements BookDao { public void save() { System.out.println("book dao save ..."); } public void init() { System.out.println("init ..."); } public void destroy() { System.out.println("destroy ..."); } }
(2) How to identify the method, which is the initialization method and which is the destruction method?
Just add @PostConstruct and @PreDestroy annotations to the corresponding methods.
@Repository public class BookDaoImpl implements BookDao { public void save() { System.out.println("book dao save ..."); } @PostConstruct //Executed after the constructor, replacing init-method public void init() { System.out.println("init ..."); } @PreDestroy //Execute before destroy method, replace destroy-method public void destroy() { System.out.println("destroy ..."); } }
(3) To see the execution of the two methods, it should be noted that destroy will only be executed when the container is closed, so the class of App needs to be modified
public class App { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); BookDao bookDao1 = ctx.getBean(BookDao.class); BookDao bookDao2 = ctx.getBean(BookDao.class); System.out.println(bookDao1); System.out.println(bookDao2); ctx.close(); //close the container } }
(4) Run the App, check the print result of the class, and prove that the init and destroy methods have been executed.
Note: If the @PostConstruct and @PreDestroy annotations are not found, you need to import the following jar package==
<dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> </dependency>
The reason I can't find it is that the javax.annotation package in jdk has been removed since JDK9, and these two annotations are just in this package.
Knowledge point 1: @PostConstruct
name | @PostConstruct |
---|---|
type | method annotation |
Location | method |
effect | Set this method as the initialization method |
Attributes | none |
Knowledge point 2: @PreDestroy
name | @PreDestroy |
---|---|
type | method annotation |
Location | method |
effect | Set this method as the destroy method |
Attributes | none |