问题
- 1.配置bean有哪两种方式:
- 2.什么叫组件扫描?特定组件包括哪些?
- 3.请回忆如何实现?
- 4.对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称
- 5.读取某个限定的包下的子类
- 6.将某个组件排除在外?
- 7.只包含某一个组件
- 8.只把某个接口包含进来
- 9.除去某个接口
- 10.如何使用bean注解来建立引用关系
- 11.默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false
- 12.默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作.此时问题该如何解决?
- 13.什么叫泛型依赖注入
1.配置bean有哪两种方式:
1.基于XML文件的方式
2.基于注解的方式
1)基于注解配置Bean
2)基于注解来装配Bean的属性
2.什么叫组件扫描?特定组件包括哪些?
1.Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件。
2.
1)@Component: 基本注解, 标识了一个受 Spring 管理的组件
2)@Respository: 标识持久层组件
3)@Service: 标识服务层(业务层)组件
4)@Controller: 标识表现层组件
3.请回忆如何实现?
1.配置包文件
<?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.0.xsd">
<context:component-scan base-package="com.atguigu.spring.beans.annotation"></context:component-scan>
</beans>
2.配置组件
package com.atguigu.spring.beans.annotation;
import org.springframework.stereotype.Component;
@Component
public class TestObject {
}
package com.atguigu.spring.beans.annotation.repository;
public interface UserRepository {
void save();
}
package com.atguigu.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...");
}
}
package com.atguigu.spring.beans.annotation.controller;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
public void execute() {
System.out.println("UserContriller execute...");
}
}
package com.atguigu.spring.beans.annotation.service;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void add() {
System.out.println("UserService add...");
}
}
测试代码:
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);
}
}
运行结果:
com.atguigu.spring.beans.annotation.TestObject@61d6015a
com.atguigu.spring.beans.annotation.controller.UserController@2525ff7e
com.atguigu.spring.beans.annotation.service.UserService@524d6d96
com.atguigu.spring.beans.annotation.repository.UserRepositoryImpl@152aa092
4.对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称
5.读取某个限定的包下的子类
<context:component-scan
base-package="com.atguigu.spring.beans.annotation"
resource-pattern="repository/*.class">
</context:component-scan>
6.将某个组件排除在外?
<context:component-scan base-package="com.atguigu.spring.beans.annotation">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
备注:context:exclude-filter 子节点表示要排除在外的目标类
7.只包含某一个组件
<context:component-scan base-package="com.atguigu.spring.beans.annotation" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
8.只把某个接口包含进来
<context:component-scan base-package="com.atguigu.spring.beans.annotation" use-default-filters="false">
<context:include-filter type="assignable" expression="com.atguigu.spring.beans.annotation.repository.UserRepository"/>
</context:component-scan>
9.除去某个接口
<context:component-scan base-package="com.atguigu.spring.beans.annotation">
<context:exclude-filter type="assignable" expression="com.atguigu.spring.beans.annotation.repository.UserRepository"/>
</context:component-scan>
10.如何使用bean注解来建立引用关系
1.使用 @Autowired 自动装配 Bean
代码详例:
public class Main {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-annotation.xml");
UserController userController=(UserController)ctx.getBean("userController");
System.out.println(userController);
userController.execute();
}
}
package com.atguigu.spring.beans.annotation.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.atguigu.spring.beans.annotation.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
public void execute() {
System.out.println("UserContriller execute...");
userService.add();
}
}
package com.atguigu.spring.beans.annotation.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atguigu.spring.beans.annotation.repository.UserRepository;
import com.atguigu.spring.beans.annotation.repository.UserRepositoryImpl;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public void add() {
System.out.println("UserService add...");
userRepository.save();
}
/*
@Autowired
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
*/
}
package com.atguigu.spring.beans.annotation.repository;
public interface UserRepository {
void save();
}
package com.atguigu.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...");
}
}
总结:构造器, 普通字段(即使是非 public), 一切具有参数的方法都可以应用@Authwired 注解
11.默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false
package com.atguigu.spring.beans.annotation.repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.atguigu.spring.beans.annotation.TestObject;
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository{
@Autowired(required=false)
private TestObject testObject;
@Override
public void save() {
System.out.println("UserRepository Save...");
System.out.println(testObject);
}
}
12.默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作.此时问题该如何解决?
1.使用@Repository(“userRepository”)中的userRepository和UserService中调用的setUserRepository相互一致
package com.atguigu.spring.beans.annotation.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atguigu.spring.beans.annotation.repository.UserRepository;
import com.atguigu.spring.beans.annotation.repository.UserRepositoryImpl;
@Service
public class UserService {
private UserRepository userRepository;
public void add() {
System.out.println("UserService add...");
userRepository.save();
}
public UserRepository getUserRepository() {
return userRepository;
}
@Autowired
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
}
package com.atguigu.spring.beans.annotation.repository;
import org.springframework.stereotype.Repository;
@Repository("userRepository")
public class UserJdbcRepository implements UserRepository{
@Override
public void save() {
System.out.println("UserJdbcRepository Save...");
}
}
package com.atguigu.spring.beans.annotation.repository;
public interface UserRepository {
void save();
}
测试代码:
public class Main {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-annotation.xml");
UserController userController=(UserController)ctx.getBean("userController");
System.out.println(userController);
userController.execute();
}
}
测试结果:
com.atguigu.spring.beans.annotation.controller.UserController@49dc7102
UserContriller execute...
UserService add...
UserJdbcRepository Save...
2.也可以使用@Qualifiter装配
package com.atguigu.spring.beans.annotation.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.atguigu.spring.beans.annotation.repository.UserRepository;
import com.atguigu.spring.beans.annotation.repository.UserRepositoryImpl;
@Service
public class UserService {
@Autowired
@Qualifier("userRepositoryImpl")
private UserRepository userRepository;
/*
@Autowired
@Qualifier("userRepositoryImpl")
public void setUserRepository(UserRepository userRepository){
this.userRepository=userRepository;
}
*/
/*
@Autowired
public void setUserRepository(@Qualifier("userRepositoryImpl")UserRepository userRepository){
this.userRepository=userRepository;
}
*/
public void add() {
System.out.println("UserService add...");
userRepository.save();
}
public UserRepository getUserRepository() {
return userRepository;
}
}
@Authwired 注解也可以应用在数组类型的属性上, 此时 Spring 将会把所有匹配的 Bean 进行自动装配.
@Authwired 注解也可以应用在集合属性上, 此时 Spring 读取该集合的类型信息, 然后自动装配所有与之兼容的 Bean.
@Authwired 注解用在 java.util.Map 上时, 若该 Map 的键值为 String, 那么 Spring 将自动装配与之 Map 值类型兼容的 Bean, 此时 Bean 的名称作为键值
Spring 还支持 @Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似
@Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性
建议使用 @Autowired 注解
13.什么叫泛型依赖注入
示例:
public class Main {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-generic-di.xml");
UserService userService=(UserService)ctx.getBean("userService");
userService.add();
}
}
<?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.0.xsd">
<context:component-scan base-package="com.atguigu.spring.beans.generic.di"></context:component-scan>
</beans>
package com.atguigu.spring.beans.generic.di;
import org.springframework.stereotype.Service;
@Service
public class UserService extends BaseService<User>{
}
public class BaseService<T> {
@Autowired
protected BaseRepository<T> repository;
public void add() {
System.out.println("add...");
System.out.println(repository);
}
}
public class BaseRepository<T> {
}
public class User{
}
@Repository
public class UserRepository extends BaseRepository<User>{
}