使用注解开发

依然使用xml配置文件来管理

项目地址

spring4之后,要使用注解开发,必须保证aop的包导入了

image-20210801135559755

使用注解开发,要导入context约束,提供注解的支持

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config/>
<!-- 指定要扫描的包,即在该包下的注解生效-->
<context:component-scan base-package="com.ajream.pojo"/>

</beans>
  1. bean

    1
    2
    3
    4
    5
    6
    @component
    //组件,在创建的类前面加上这个注解,说明这个类已经被spring管理了,相当于:
    //<bean id="person" class="com.ajream.pojo.Person">
    //没有参数时,只能以getBean("类名的小写字母") 来获取bean
    @component(value="xxx")
    //有参数时,可以用getBean("xxx") 来获取bean

    Person类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    package com.ajream.pojo;

    import org.springframework.stereotype.Component;

    @Component //没有参数时,只能以getBean("person") 来获取bean
    public class Person {
    public String name = "张三";
    }

  2. 属性注入

    1
    2
    @Value("李四")
    //相当于<property name="name" value="李四" />
    1
    2
    3
    4
    5
    6
    7

    @Component
    public class Person {

    @Value("李四") //注入属性值
    public String name;
    }

    也可以在setter方法前使用:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    @Component
    public class Person {
    public String name;

    @Value("李四")
    public void setName(String name) {
    this.name = name;
    }
    }
  3. 衍生注解

    @Component有几个衍生注解,在web中一般按照mvc三层架构划分:

    • dao层:@Repository
    • service层:@Service
    • Controller层:@Controller

    这4个注解功能一样的

  4. 作用域scope

    单例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @Component
    @Scope(value = "singleton")
    public class Person {
    public String name;

    @Value("李四")
    public void setName(String name) {
    this.name = name;
    }
    }

    原型

    1
    @Scope(value = "prototype")  
  5. 小结:

    xml与注解

    • xml:万能

    • 注解:不是自己的类用不了,维护相对复杂

    xml与注解最佳实践:

    • xml用来管理bean

    • 注解只负责属性注入

    • 注意:要让注解生效,必须开启注解的支持

      1
      2
      <context:annotation-config/>
      <context:component-scan base-package="com.ajream.pojo"/>

使用Java的方式配置spring

项目地址

在此前一直都使用beans.xml配置文件来配置spring如属性注入,现在可以不使用xml了,将spring配置全权交由Java来配置

用MyConfig类来代替beans.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.ajream.config;

import com.ajream.pojo.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {

@Bean
//相当于<bean id="getPerson1" class="com.ajream.pojo.Person">
public Person getPerson1(){ //方法名就是bean的id
return new Person();
}
}

Person类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.ajream.pojo;

import org.springframework.beans.factory.annotation.Value;

public class Person {
public String name;

@Value("李四")
public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

MyTest类

image-20210801151413775

这里不使用xml配置文件了,使用Java配置类 MyConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import com.ajream.config.MyConfig;
import com.ajream.pojo.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class MyTest {

@Test
public void test1() {
//加载配置类
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);

Person p = context.getBean("getPerson1", Person.class);

System.out.println(p.getName());

}
}

由于MyConfig类也是配置类,所以它具有beans.xml的功能,比如:

image-20210801155244594