Spring将所有的bean都纳入到IoC中创建、管理和维护,一个系统往往有成千上万的组件,如果需要手动将所有组件都放入spring容器中去管理,这样spring配置文件将变得非常冗余且庞大,管理也将十分不方便。
从Spring2.5开始引入了组件自动扫描机制,你可以不用在xml文件中配置繁琐的bean,Spring会自动扫描、检查你指定包的bean或component。
Spring容器提供了@Component
和@Bean
注解来标记如何向IoC容器添加Bean。在@Component
基础上又派生出
@Controller
, @Service
和@Repository
这三个注解(在其他的Spring工程或包中还有更多的派生)。
@Controller
、@Service
和@Repository
注释是在@Component
基础上添加了角色的组件,它们分别用于表示层、服务层和数据访问层。
下面是一个简单的Spring项目,包括自定义的service和dao层。我们研究一下在Spring中手动声明组件和自动扫描组件之间的区别。
1、手动声明组件
创建EmployeeService
bean ,如下所示。
EmployeeService.java
package com.wxbsht.spring.componentscan.example.service;
import com.wxbsht.spring.componentscan.example.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author HP-susht
* @create 2020/4/27 11:31
*/
public class EmployeeService {
private EmployeeDao employeeDao;
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
public void doSomething(){
System.out.println("Inside EmployeeService's doSomething() method.");
employeeDao.doSomething();
}
}
创建EmployeeDao
bean ,如下所示。
EmployeeDao.java
package com.wxbsht.spring.componentscan.example.dao;
import org.springframework.stereotype.Repository;
/**
* @author HP-susht
* @create 2020/4/27 11:32
*/
public class EmployeeDao {
public void doSomething() {
System.out.println("Inside EmployeeDao's doSomething() method.");
}
}
Bean配置文件(beans.xml), Spring中的普通Bean配置。
<?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">
<bean id="employeeDao" class="com.wxbsht.spring.componentscan.example.dao.EmployeeDao"/>
<bean id="employeeService" class="com.wxbsht.spring.componentscan.example.service.EmployeeService">
<property name="employeeDao" ref="employeeDao"/>
</bean>
</beans>
运行主程序MainApplication.java
package com.wxbsht.spring.componentscan.example;
import com.wxbsht.spring.componentscan.example.config.AppConfig;
import com.wxbsht.spring.componentscan.example.service.EmployeeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author HP-susht
* @create 2020/4/27 11:35
*/
public class MainApplication {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
EmployeeService service = context.getBean(EmployeeService.class);
service.doSomething();
}
}
输出
Inside EmployeeService's doSomething() method.
Inside EmployeeDao's doSomething() method.
2、自动扫描组件
在Spring框架中,我们可以使用以下命令来启用组件自动扫描
-
<context:component-scan/>
基于XML的配置中的元素 -
@ComponentScan
基于Java的配置中的注释 -
AnnotationConfigApplicationContext#scan()
方法
2.1、创建Spring组件
创建EmployeeService
bean ,如下所示。
EmployeeService.java
package com.wxbsht.spring.componentscan.example.service;
import com.wxbsht.spring.componentscan.example.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author HP-susht
* @create 2020/4/27 11:31
*/
@Service
public class EmployeeService {
@Autowired
private EmployeeDao employeeDao;
public void doSomething(){
System.out.println("Inside EmployeeService's doSomething() method.");
employeeDao.doSomething();
}
}
创建EmployeeDao
bean ,如下所示。
EmployeeDao.java
package com.wxbsht.spring.componentscan.example.dao;
import org.springframework.stereotype.Repository;
/**
* @author HP-susht
* @create 2020/4/27 11:32
*/
@Repository
public class EmployeeDao {
public void doSomething() {
System.out.println("Inside EmployeeDao's doSomething() method.");
}
}
2.2、使用<context:component-scan />元素启用组件扫描
要在基于XML的配置中启用自动组件扫描,使用以下<context:component-scan base-package=""/>
元素。
beans.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: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">
<!--<context:component-scan base-package="com.wxbsht.spring.componentscan.example"/>-->
<context:component-scan base-package="com.wxbsht.spring.componentscan.example.service,
com.wxbsht.spring.componentscan.example.dao"/>
</beans>
运行应用程序MainApplication.java
package com.wxbsht.spring.componentscan.example;
import com.wxbsht.spring.componentscan.example.config.AppConfig;
import com.wxbsht.spring.componentscan.example.service.EmployeeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author HP-susht
* @create 2020/4/27 11:35
*/
public class MainApplication {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
EmployeeService service = context.getBean(EmployeeService.class);
service.doSomething();
}
}
输出
Inside EmployeeService's doSomething() method.
Inside EmployeeDao's doSomething() method.
2.3、使用@ComponentScan注解启用组件扫描
要在基于Java的配置中启用自动组件扫描。
AppConfig.java
package com.wxbsht.spring.componentscan.example.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author HP-susht
* @create 2020/4/27 11:37
*/
@Configuration
@ComponentScan(basePackages = { "com.wxbsht.spring.componentscan.example.dao",
"com.wxbsht.spring.componentscan.example.service"})
public class AppConfig {
}
运行应用程序。
MainApplication.java
package com.wxbsht.spring.componentscan.example;
import com.wxbsht.spring.componentscan.example.config.AppConfig;
import com.wxbsht.spring.componentscan.example.service.EmployeeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author HP-susht
* @create 2020/4/27 11:35
*/
public class MainApplication {
public static void main(String[] args) {
// ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
// EmployeeService service = context.getBean(EmployeeService.class);
// service.doSomething();
ApplicationContext context1=new AnnotationConfigApplicationContext(AppConfig.class);
EmployeeService service1 = context1.getBean(EmployeeService.class);
service1.doSomething();
}
}
输出
Inside EmployeeService's doSomething() method.
Inside EmployeeDao's doSomething() method.
2.4、使用上下文的scan()方法启用组件扫描
不使用XML或基于java的配置,我们可以使用以下注释configapplicationcontext #scan()方法启用组件扫描。
MainApplication.java
package com.wxbsht.spring.componentscan.example;
import com.wxbsht.spring.componentscan.example.config.AppConfig;
import com.wxbsht.spring.componentscan.example.service.EmployeeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author HP-susht
* @create 2020/4/27 11:35
*/
public class MainApplication {
public static void main(String[] args) {
// ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
// EmployeeService service = context.getBean(EmployeeService.class);
// service.doSomething();
// ApplicationContext context1=new AnnotationConfigApplicationContext(AppConfig.class);
// EmployeeService service1 = context1.getBean(EmployeeService.class);
// service1.doSomething();
AnnotationConfigApplicationContext context3 =
new AnnotationConfigApplicationContext();
context3.scan("com.wxbsht.spring.componentscan.example.service");
context3.scan("com.wxbsht.spring.componentscan.example.dao");
context3.refresh();
EmployeeService service3 = context3.getBean(EmployeeService.class);
service3.doSomething();
context3.close();
}
}
输出
Inside EmployeeService's doSomething() method.
Inside EmployeeDao's doSomething() method.
2.5、自定义自动扫描组件名称
默认情况下,Spring将组件类名首字母小写后作为组件在容器中的id(employeeService)。您可以使用此名称“employeeService”获取此组件。
EmployeeService service = (EmployeeService)context.getBean("employeeService");
要为组件创建一个自定义名称,您可以按如下代码自定义名称:
@Service("AAA")
public class EmployeeService
...
现在,您可以使用“ AAA”这个名称来获取它
EmployeeService cust = (EmployeeService)context.getBean("AAA");