Spring IoC
Spring IoC就是一个容纳Bean的容器,并对Bean进行管理,你可以想象Spring IoC就是一个生成Bean、容纳Bean、管理Bean的工厂;BeanFactory是Spring IoC的核心接口,从名称上我们就能了解,这是一个工厂,它的职责包括:实例化、定位、配置应用程序中的对象及建立这些对象间的依赖,简单来说,就是你需要new对象的时候,他能帮助你将对象new出来,并且默认为单例;当你的一个对象依赖于某个对象时(A依赖B),我们可以通过向Spring IoC容器描述它们之间的关系,来建立它们直接的关联关系。
BeanFactory接口设计
1.BeanFactory是IoC容器的最底层接口,也就是说,Spring IoC容器一定要实现这个BeanFactory这个接口。
2.ApplicationContext是IoC容器的高级接口,它可以扩展其他别的接口,以至于能够扩展其他的功能。
BeanFactory的接口方法
这上边我们要重点认识红色的几个方法
//根据名称获取bean
Object getBean(String name) throws BeansException;
//根据名称和类型获取bean
<T>T getBean(String name,Clss<T>requiredType) throws BeansException;
//根据类型获取bean
<T>T getBean(Clss<T>requiredType) throws BeansException;
//是否单例,每次getBean方法都返回同一个Bean对象
boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
//是否原型,既每次getBean方法都返回一个新的Bean
boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
实践Spring IoC容器(使用java配置文件)
1.扫描
(1)@Component :标注需要扫描的类。
(2)@ComponentScan:定义扫描的作用域,默认在当前包范围。
User.java
@Component
public class User {
@Value("1")
private Long id;
@Value("user_name_1")
private String userName;
@Value("note_1")
private String note;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
AppConfig .java
@Configuration //说明为java配置文件
@ComponentScan(basePackages = "com.springboot.chapter3.*") //定义扫描的规则
public class AppConfig {
@Bean(name = "user")
public User initUser() {
User user = new User();
return User ;
}
}
IoCTest .java
public class IoCTest {
public static void main(String[] args) {
//创建一个IoC容器
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
//通过类型获取Bean
User user = (User)ctx.getBean(User.class);
ctx.close();
}
}
这样就实现了User中Bean的实例化;不仅@Component可以标注扫描,还有:
(1)@Component 标注要扫描的class
(2)@Controller 控制器
(3)@Service 服务层
(4)@Repository 数据层
只要标注这些注解,都可以被@ComponentScan扫描到。
@ComponentScan:定义那些扫描,那些不扫描
1)excludeFilters = {@Filter(classes = {})} :排除符合规则的类,Spring IoC将不会扫描。
2)includeFilters = {@Filter(classes = {})} :加入符合规则的类,Spring IoC将会扫描符合该规则的类
2.@Bean :主要用于第三方依赖
AppConfig .java
package com.springboot.chapter3.config;
@Configuration //说明为java配置文件
@ComponentScan(basePackages = "com.springboot.chapter3.*") //定义扫描的规则
public class AppConfig {
@Bean(name = "dataSource")
public DataSource getDataSource() {
Properties props = new Properties();
props.setProperty("driver", "com.mysql.jdbc.Driver");
props.setProperty("url", "jdbc:mysql://localhost:3306/chapter3");
props.setProperty("username", "root");
props.setProperty("password", "123456");
DataSource dataSource = null;
try {
dataSource = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
e.printStackTrace();
}
return dataSource;
}
}
IoCTest .java
public class IoCTest {
public static void main(String[] args) {
//创建一个IoC容器
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
//通过类型获取Bean
DataSource dataSource= ctx.getBean(DataSource.class);
ctx.close();
}
}
此时也可以实现Bean的创建。
3.依赖注入:例子—人依赖动物的服务
@Autowired:按类型去查找Bean,找不到,就按照属性名称查找,再找不到就抛出异常 ;默认不能为空,而且注入的是单例;可以用于属性,参数,构造方法上
Animal接口
public interface Animal {
public void use();
}
Dog .java
public class Dog implements Animal{
public void use(){
System.out.println("依赖动物服务成功");
}
}
Person 接口
public interface Person {
// 使用动物服务
public void service();
// 设置动物
public void setAnimal(Animal animal);
}
BussinessPerson.java
@Component
public class BussinessPerson implements Person {
@Autowired
private Animal animal ;
public void service() {
this.animal.use();
}
public void setAnimal(Animal animal) {
this.animal = animal;
}
}
IoCTest .java
public class IoCTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
Person person = ctx.getBean(Person.class);
person.service();
ctx.close();
}
}
此时也能Spring IoC装配Bean。