Configuration File
In the previous examples, we used XML bean definition to configure components. In a slightly larger project, there are usually hundreds of components. If these components are configured using XML bean definitions, it will obviously increase the size of the configuration file, making it not very convenient to find and maintain.
Spring 2.5 introduces an automatic component scanning mechanism for us. It can find classes annotated with @Component, @Service, @Controller, and @Repository annotations under the classpath, and incorporate these classes into the Spring container to manage.
Its function is the same as using bean node configuration components in XML files. To use the automatic scanning mechanism, we need to open the following configuration information:
<?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-4.2.xsd"> <context:component-scan base-package="cn.itcast" /></beans>
The <context:component-scan base-package="cn.itcast" /> configuration implicitly registers multiple processors that parse annotations, including the <context:annotation-config/> processor registered with the configuration, that is, if <context:component-scan base-package="cn.itcast" /> configuration is written, there is no need to write <context:annotation-config/> configuration. In addition, the base-package is a package (subpackage) that needs to be scanned.
annotation
@Service is used to annotate the business layer components, @Controller is used to annotate the control layer components (such as the action in Struts2), and @Repository is used to annotate the data access component, that is, the DAO component. @Component refers to components. When components are not easy to classify, we can use this annotation to annotate.
This article is based on the case of @Autowire annotation and automatic assembly.
We first change Spring's configuration file to:
<?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-4.2.xsd"> <context:component-scan base-package="cn.itcast" /></beans>
An example
Then use @Service annotation to annotate the PersonServiceBean class, as follows:
@Servicepublic class PersonServiceBean implements PersonService { private PersonDao personDao; public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } @Override public void save() { personDao.add(); }}Annotate the PersonDaoBean class using @Repository annotation, as follows:
@Repositorypublic class PersonDaoBean implements PersonDao { @Override public void add() { System.out.println("Execute the add() method in PersonDaoBean"); }}Finally, we modify the code of SpringTest class to:
public class SpringTest { @Test public void instanceSpring() { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personServiceBean"); PersonDao personDao = (PersonDao) ctx.getBean("personDaoBean"); System.out.println(personService); System.out.println(personDao); ctx.close(); }}Test the instanceSpring() method and you can see that the Eclipse console prints:
If we want to use the specified name to get it, we can modify the code of the PersonServiceBean class to:
@Service("personService")public class PersonServiceBean implements PersonService { private PersonDao personDao; public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } @Override public void save() { personDao.add(); }}In this way, the code of SpringTest class should be changed to:
public class SpringTest { @Test public void instanceSpring() { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService"); System.out.println(personService); ctx.close(); }}Test the instanceSpring() method and you can see that the Eclipse console prints:
We have learned the scope of Spring-managed beans before, and we can know that the scope of the two Spring-managed beans above is singleton by default. Of course, we can also change the scope of Spring-managed beans, such as changing the code of PersonServiceBean class to:
@Service("personService") @Scope("prototype")public class PersonServiceBean implements PersonService { private PersonDao personDao; public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } @Override public void save() { personDao.add(); }}This means that the scope of the PersonServiceBean managed by Spring has become a prototype. At this time, we modify the code of the SpringTest class to:
public class SpringTest { @Test public void instanceSpring() { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService1 = (PersonService) ctx.getBean("personService"); PersonService personService2 = (PersonService) ctx.getBean("personService"); System.out.println(personService1 == personService2); ctx.close(); }}Test the instanceSpring() method and you can see that the Eclipse console prints:
prototype scope means that every time you get a bean from a Spring container, it is a new object.
If components are included in the Spring container by automatically scanning under the classpath path, how to specify the initialization method and destruction method of the bean? At this time, we need to use two annotations: @PostConstruct and @PreDestroy. For experimentation, we modified the code of PersonServiceBean class to:
@Service("personService")public class PersonServiceBean implements PersonService { private PersonDao personDao; @PostConstruct public void init() { System.out.println("Initialize resource"); } @PreDestroy public void destroy() { System.out.println("Destroy, close resource"); } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } @Override public void save() { personDao.add(); }}Next, we need to modify the code of SpringTest class to:
public class SpringTest { @Test public void instanceSpring() { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService"); ctx.close(); }}In this way, test the instanceSpring() method and the Eclipse console will print:
To view the source code, click to let Spring automatically scan and manage beans to download.
Summarize
The above is the automatic spring scan package introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!