1.基于注解的方式配置Bean,裝配屬性
在classpath中扫描组件
1.组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件
2.特定组件包括
1.@Component 基本注解 表示了一个受Spring管理的组件
2.@Respositroy 标识持久层(Dao层 接口+实现类)组件
3.@Service 标识服务层(业务层)组件
4.@Controller 标识表现层(控制层)组件
3.对于扫描到的组件,Spring有默认的命名策略:使用非限定类名,第一个字母小写,也可以在注解中通过value属性值标识组件的名称,例如:"testObject"
eg
1.基本注解
@Component
public class TestObject {}
2.控制器层
@Controller
public class UserController {
public void execute() {
System.out.println("UserController……");
}
}
3.持久层(接口+实现类)
1.接口
public interface UserRepository {
void save();
}
2.实现类
@Repository("userRepository")//重命名
public class UserRepositoryImpl implements UserRepository {
@Override
public void save() {
// TODO Auto-generated method stub
System.out.println("UserRepositoryImpl……");
}
}
5.服务层
@Service
public class UserService {
public void add() {
System.out.println("UserService add……");
}
}
6.配置annotationBeans.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-4.3.xsd">
<context:component-scan base-package="org.spring.annotation.component"></context:component-scan>
</beans>
7.测试类
public class Test5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("annotationBeans.xml");
TestObject to = (TestObject) ctx.getBean("testObject");
System.out.println("testObject"+to);
UserController userController = (UserController) ctx.getBean("userController");
System.out.println("userController"+userController);
UserRepository userRepository = (UserRepository) ctx.getBean("userRepositoryImpl");
System.out.println("userRepository:"+userRepository);
UserService userService = (UserService) ctx.getBean("userService");
System.out.println("userService:"+userService);
}
}
2.当在组件类上使用了特定的注解之后,还需要在Spring的配置文件中声明context:component-scan:
1.base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里及其子类包中的所有类
2.当选要扫描多个包时,可以使用逗号分隔。
3.如果仅希望扫描特定的类而非基类包下的所有类,可使用rescue=pattern属性过滤特定的类,示例:
1.<context:include-filter>子节点表示要包含的目标类
2.<context:exclude-filter>子节点表示要排除在外的目标类
4.<context:component-scan>下可以拥有若干个<context:include-filter>和<context:exclude-filter>子节点
3.context:include-filter和context:exclude-filter子节点支持多种类型的过滤表达式
1.类型:annotation ;示例:com.atguigu.XxxAnnotation;说明:所有标注了XxxAnnotation 类。该类型采用目标类是否标注了某个注解进行过滤
2.类型:assinable ;示例:com.atguigu.XxxService;说明:所有继承或扩展XxxService的类。该类型采用目标类是否继承或扩展某个特定类进行过滤
3.类型:aspectj ;示例:com.atguigu..*Service+;说明:所有类名以Service结束的类及继承或扩展它们的类。该类型采用AspectJ表达式进行过滤
4.类型:regex;示例:com.\atguigu\.anno\..* 说明所有com.atguigu.anno包下的类。该类型采用正则表达式根据类名进行过滤
5.类型:custom;示例:com.atguigu.XxxTypeFilter 说明:采用XxxTypeFilter通过代码的方式定义过滤的规则。则该类必须实现org.springframework.core.type.TypeFilter接口
eg
1.配置annotationBeans.xml排除Repository(持久层)org.springframework.stereotype.Repository注解类包
<?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.3.xsd">
<context:component-scan base-package="org.spring.annotation.component" resource-pattern="**/*.class">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
</beans>
2.配置annotationBeans.xml只包含Repository(持久层)use-default-filters=“false” 不使用默认都关联
<?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.3.xsd">
<context:component-scan base-package="org.spring.annotation.component" resource-pattern="**/*.class" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
</beans>
3.assignable使用 排除注解 通过类名
<?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.3.xsd">
<context:component-scan base-package="org.spring.annotation.component" resource-pattern="**/*.class">
<context:exclude-filter type="assignable" expression="org.spring.annotation.component.UserRepository"/>
</context:component-scan>
</beans>
4.assignable使用 只包含注解 通过类名
<?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.3.xsd">
<!-- <context:component-scan base-package="org.spring.annotation.component" resource-pattern="**/*.class" use-default-filters="false">
<context:include-filter type="assignable" expression="org.spring.annotation.component.UserRepository"/>
</context:component-scan>
</beans>
5.组件装配
1.<context:component-scan>元素还会自动注册AutowiredAnnotationBeanPostProcessor实例,该实例可以自动装配具有@Autowired @Resource @Inject注解的属性
7.使用@Autowired自动装配Bean
1.@Autowired 注解自动装配具有兼容类型的单个Bean属性
1.构造器,普通字段(即使是非public,一切具有参数的方法都可以应用@Autowired注解
2.默认情况下,所有使用@Autowired注解的属性都需要被设置,当Spring找不到匹配的Bean装配属性时,会抛出异常,若某一属性允许不被设置,可以设置@Autowired注解的required属性为false
3.默认情况下,当IOC容器里存在多个类型兼容的Bean时,通过类型的自动装配将无法工作,此时可以在@Qualifier注解里提供Bean的名称,Spring允许对方法的入参标注@Qualifiter已指定注入Bean的名称
4.@Autowired注解也可以应用在数组类型的属性上,此时Spring将会把所有匹配的Bean进行自动装配
5.@Autowired注解也可以应用在集合属性上,此时Spring读取该集合的类型信息,然后自动装配所有与之兼容的Bean
6.@Autowired注解用在java.util.Map上时,若该Map的键值为Spring,那么Spring将自动装配与之Map值类型兼容的Bean,此时Bean的名称作为键值
Autowired默认先按byType,如果发现找到多个bean,则又按照ByName方式比对,如果还有多个,则抛出异常
eg
1.控制层 @Autowired注解后的属性可以不写getter setter
@Controller
public class UserController {
@Autowired
private UserService userService;
public void execute() {
System.out.println("UserController");
userService.add();
}
}
2.服务层
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public void add() {
System.out.println("UserService");
userRepository.save();
}
}
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {
@Autowired
private TestObject testObject;
@Override
public void save() {
// TODO Auto-generated method stub
System.out.println("UserRepositoryImpl");
System.out.println("testObject:"+testObject);
}
}
4.配置annotationBeans.xml
<context:component-scan base-package="org.spring.annotation.component"></context:component-scan>
5.测试类
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("annotationBeans.xml");
UserController userController = (UserController) ctx.getBean("userController");
userController.execute();
}
注入时切换名字
eg
1.新建UserRepositoryImpl2.java
@Repository("userRepository2")
public class UserRepositoryImpl2 implements UserRepository {
@Autowired(required = false)
private TestObject testObject;
@Override
public void save() {
// TODO Auto-generated method stub
System.out.println("UserRepositoryImpl22222");
System.out.println("testObject:"+testObject);
}
}
@Service
public class UserService {
@Autowired
@Qualifier("userRepositoryImpl2")
private UserRepository userRepository;
public void add() {
System.out.println("UserService");
userRepository.save();
}
}
测试类会执行2