使用注解开发
依然使用xml配置文件来管理
项目地址
spring4之后,要使用注解开发,必须保证aop的包导入了
![image-20210801135559755](https://gitee.com/ajream/images/raw/master/img/2021-08-0113-56-04_image-20210801135559755.png)
使用注解开发,要导入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>
|
bean
1 2 3 4 5 6
| @component: @component(value="xxx")
|
Person类:
1 2 3 4 5 6 7 8 9
| package com.ajream.pojo;
import org.springframework.stereotype.Component;
@Component public class Person { public String name = "张三"; }
|
属性注入
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; } }
|
衍生注解
@Component有几个衍生注解,在web中一般按照mvc三层架构划分:
- dao层:@Repository
- service层:@Service
- Controller层:@Controller
这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")
|
小结:
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 public Person getPerson1(){ 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](https://gitee.com/ajream/images/raw/master/img/2021-08-0115-14-18_image-20210801151413775.png)
这里不使用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](https://gitee.com/ajream/images/raw/master/img/2021-08-0115-52-49_image-20210801155244594.png)