spring_(13)通过注解配置之一
在classpath中扫描组件
-
组件扫描(component scanning):Spring 能够从classpath下自动扫描,侦测和实例化具有特定注解的组件.
-
特定组件包括:
- @Component:基本注解,标识了一个受Spring管理的组件
- @Respository:标识持久层组件
- @Service:标识服务层(业务层)组件
- @Controller:标识表现层组件 UserService userService
-
对于扫描到的组件,Spring有默认的命名策略:使用非限定类名,第一个字母小写。也可以在注解中通过value属性值标识组件的名称
-
当在组件类上使用了特定的注解之后,还需要在Spring的配置文件中声明**< context:component-scan >**:
-
base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里及其子包中的所有类
-
当需要扫描多个包时,可以使用逗号分隔.
-
如果仅希望扫描特定的类而非基包下的所有类,可使用resource-pattern属性过滤特定的类,示例:
<context:component-scan base-package="com.spring.beans" resource-pattern="autowire/*.class"/>
-
< context:include-filter >子节点表示要包含的目标类
-
< context:exclude-filter >子节点表示要排除在外的目标类
-
< context:component-scan >下可以拥有若干个< context:include-filter >和< context:exclude-filter >子节点
例子程序
基本结构
-
TestObject.java
package com.spring.beans.annotation;
import org.springframework.stereotype.Component;
@Component
public class TestObject {
}
UserController.java
package com.spring.beans.annotation.controller;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
public void execute(){
System.out.println("UserController execute...");
}
}
UserRepository.java(接口)
package com.spring.beans.annotation.repository;
public interface UserRepository {
void save();
}
UserRepositoryImpl.java
package com.spring.beans.annotation.repository;
import org.springframework.stereotype.Repository;
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {
@Override
public void save() {
System.out.println("UserRepository Save...");
}
}
UserService.java
package com.spring.beans.annotation.service;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void add(){
System.out.println("UserService add...");
}
}
beans-annotation.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">
<!--指定spring IOC 容器扫描的包-->
<!--可以通过resource-pattern指定扫描的资源-->
<!--<context:component-scan
base-package="com.spring.beans.annotation"
resource-pattern="repository/*.class">
</context:component-scan>-->
<!--exclude-filter 子节点指定排除哪些指定表达式的组件-->
<!--context:include-filter 子节点指定包含哪些表达式的组件,该子节点需要 use-default-filters 配合使用-->
<context:component-scan
base-package="com.spring.beans.annotation">
<!-- 只包含时需要添加这个属性use-default-filters="false"-->
<!--不包含<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>-->
<!--只包含<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>-->
<!--不包含这个接口以及接口的实现类<context:exclude-filter type="assignable" expression="com.spring.beans.annotation.repository.UserRepository"/>-->
<!--只包含这个接口以及接口的实现类<context:include-filter type="assignable" expression="com.spring.beans.annotation.repository.UserRepository"/>-->
</context:component-scan>
</beans>
Main.java
package com.spring.beans.annotation;
import com.spring.beans.annotation.controller.UserController;
import com.spring.beans.annotation.repository.UserRepository;
import com.spring.beans.annotation.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");
TestObject to = (TestObject) ctx.getBean("testObject");
System.out.println(to);
UserController userController =(UserController) ctx.getBean("userController");
System.out.println(userController);
UserService userService = (UserService) ctx.getBean("userService");
System.out.println(userService);
UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
System.out.println(userRepository);
}
}