Spring笔记复习
1. spring项目常用依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc-->
<dependency>
<!-- 公司名-->
<groupId>org.springframework</groupId>
<!-- 项目名-->
<artifactId>spring-webmvc</artifactId>
<!-- 版本-->
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.2</version>
<type>jar</type>
</dependency>
</dependencies>
2.xml注解配置
<?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.xsd">
<!-- 自动配置注解的支持,非常重要!!! -->
<context:annotation-config/>
</beans>
3.自动装配注解
- @Nullable
- 字段标记了这个注解,说明这个字段可以为null
- @Autowired
- 根据ByType() 来进行自动装配,如果存在多个相同类型的bean则使用下面的注解来进行自动装配
- @Qualifier(value = “id”)
- 根据ByName() 来进行自动装配
- @Resource
- 先根据ByName() 进行自动装配,如果不成功则通过ByType() 来进行自动装配,连个都不成功,则报错
- @Component
- 放在类上,说明这个类被Spring管理了,就是bean
- @Value()
- 放在属性上,表示数据的注入
4. 使用注解开发
- 在Spring4之后,要使用注解开发,必须要保证aop包的导入
- 使用注解需要导入context约束,增加注解支持
xml <context:annotation-config>
- @Component 属性注入使用@Value(“注入Date‘”)
- @Component 有几个衍生注解,我们在web开发中,会按照mvc三层结构分层,以下的注解,功能都是一样的
- Dao【@Repositiry】
- service【@Service】
- controller【@Controller】
5.小结
- xml与注解:
- xml更加万能,适用于任何场合,维护简单
- 注解不是自己的类使用不了,维护相对困难
- xml与注解最佳实践:
- xml用来管理bean
- 注解只负责完成属性的注入
- 我们在使用的过程中,只需要注意一个问题,必须让注解生效,就需要开启注解的支持
使用Java的方式配置Spring
- 不使用Spring 的xml 配置,全权交给Java来做
//XiaoChengConfig.class
// 这个也会被Spring容器托管,注册到Spring中,因为他本身就是Component
// config代表一个配置类 = xml
@Configuration
@ComponentScan("com.xiaocheng.pojo")
public class XiaoChengConfig {
// <bean id = "getUser" class = "User"/>
@Bean
public User getUser(){
// 返回要注入Bean的对象
return new User();
}
}
// User.class
// 这个注解的意思是这个类被Spring管理了
@Component
public class User {
private String name;
public String getName() {
return name;
}
// 注入数据
@Value("DDDerek")
public void setName(String name) {
this.name = name;
}
}
SpringAOP
1.方式1:Spring
<?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:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 自动配置注解的支持,非常重要!!! -->
<context:annotation-config/>
<bean id = "userService" class="com.xiaocheng.service.UserServiceImpl"/>
<bean id = "log" class = "com.xiaocheng.log.Log"/>
<bean id = "afterLog" class = "com.xiaocheng.log.AfterLog"/>
<bean id = "testService" class="com.xiaocheng.service.TestServiceImpl"/>
<!-- 方式1.使用原生 Spring API 接口-->
<!-- 配置 Aop-->
<aop:config>
<!-- 切入点-->
<!-- expression = 表达式 excution(要执行的位置! * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* com.xiaocheng.service.UserServiceImpl.*(..))"/>
<aop:pointcut id="pointcut1" expression="execution(* com.xiaocheng.service.TestServiceImpl.*())"/>
<!-- 执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut1"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut1"/>
</aop:config>
</beans>
- 方式2:自定义类
<aop:config>
<!-- 自定义切面,ref要引用的类-->
<aop:aspect ref="diy">
<!-- 切入点-->
<!-- execution 表达式-->
<!-- execution() 表达式主体-->
<!-- 第一个* 表示返回类型-->
<!-- 包名,表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包-->
<!-- 第二个* 表示类名,*表示所有的类-->
<!-- 最后的*(..) 表示所有的方法,..表示方法的参数,..表示所有的参数-->
<aop:pointcut id="point" expression="execution(* com.xiaocheng.service.UserServiceImpl.*())"/>
<!-- 通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>