IOC容器(基于注解方式)
一、Bean管理之创建
①Spring提供的创建对象提供的注解
(1)@Component;
(2)@Controller;
(3)@Service;
(4)@Repository;
这四个注解都是创建对象的注解,功能完全相同,只是推荐应用的地方不同,如:Controller推荐用在Web层,Service推荐用在Service层,Repository推荐使用在Dao层;
②创建对象的过程
第一步,导入依赖:
第二步,在配置配置文件中打开注解扫描:
首先要配置一个命名空间:
<?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"
xmlns:cotext="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">
再打开注解扫描:
<!-- 开启注解扫描 -->
<cotext:component-scan base-package="com.lazy.annotation.anno1"></cotext:component-scan>
第三步,创建javaBean,并添加注解:
@Component
public class Person {
private String name;
private Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
}
要注意的是没有给@Component设置名称值类似于bean标签中的id属性时,默认的值为,将类名的首字母小写作为该名称值;
第四步,调用:
public void test(){
//加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("anno1.xml");
//获取对象
Person person = context.getBean("person", Person.class);
//调用对象
System.out.println(person);
}
此处仍有配置文件,所以下面介绍完全注解开发
③完全注解开发
完全注解开发时用一个配置类来替代配置文件,并且使用@Configuration注解来标识;
使用@ComponentScan注解来开启注解扫描;
@Configuration
@ComponentScan
public class Config {
}
再调用时,也要注意加载文件的步骤修改为加载配置类的类型类;
public void test2(){
//加载配置文件
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
//获取对象
Person person = context.getBean("person", Person.class);
//调用对象
System.out.println(person);
}
④开启注解扫描时要注意的点
<!--示例 1
use-default-filters="false" 表示现在不使用默认 filter,自己配置 filter
context:include-filter ,设置扫描哪些内容
--><context:component-scan base-package="com.atguigu" use-defaultfilters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--示例 2
下面配置扫描包所有内容
context:exclude-filter: 设置哪些内容不进行扫描
--><context:component-scan base-package="com.atguigu">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
二、Bean管理之属性注入
①Spring提供的创建属性注入的注解(使用自动装配)
(1)@AutoWired:根据属性自动装配
(2)@Qualifier:根据名称自动装配(但是在使用时要和AutoWired一同使用)
(3)@Value:配置普通数据类型;
JavaBean:
@Component
public class Person {
@Value(value = "lazy")
private String name;
@Value(value = "1001")
private Integer id;
@Autowired
@Qualifier(value = "subject1")
private Subject subject;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", id=" + id +
", subject=" + subject +
'}';
}
}
外部bean:
@Component(value = "subject1")
public class Subject {
@Value(value = "Chinese1")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Subject{" +
"name='" + name + '\'' +
'}';
}
}
使用的配置类同上;
(4)@Resource即可根据属性又可以根据名称自动装配,但是是java内部提供的不推荐使用;