spring框架总结
2021/9/21 23:42:53
本文主要是介绍spring框架总结,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、反射的概念
1.反射的概念
反射就是在运行期间动态的获取的类的基本信息(类的属性,构造方法,普通方法)动态的调用对象的发现,极大地提高了java代码的灵活性
2.反射的三种创建方式
获取Class的方式: 1.Class c = Class.forName(className); 2.Class c2 = Test.class; 3.Test t = new Test(); Class c = t.getClass();
二、Spring 的概念
1.Spring的概念
Spring是一个轻量级的IoC和AOP容器框架。也是是为Java应用程序提供基础性服务的一套框架
2.Spring的作用
a.提供松耦合 解耦的使用
b.目的是用于简化企业应用程序的开发,它使得开发者只需要关心业务需求。
三、spring的IOC的概念
1.loc 的概念
a.IOC就是控制反转,指创建对象的控制权转移给Spring框架进行管理
b.由Spring根据配置文件去创建实例和管理各个实例之间的依赖关系,对象与对象之间松散耦合,也利于功能的复用。
c.使用java的反射机制,根据配置文件在运行时动态的去创建对象以及管理对象,并调用对象的方法的。
2.loc的注解
注解使用在类的上面
@Service("studentService")//相当于创建了一个对象 public class StudentServiceImpl implements StudentService { }
loc的注解 @Component @Repository(dao) @Service(service) @Controller(servelt)
相当于使用xml的
<bean id="student" class="org.lanqiao.entity.Student"/>
使用bean相当于创建了一个student的对象 由于id已经确定该对象的名字 所以类的名字的变化不会影响创建的对象 也就是降低了对象与对象之间的依赖关系
使用配置类
@Configurable //表明该类是一个配置类 主要是用于配置 用来代替xml @ComponentScan({"org.lanqiao"})//加入扫描,扫描该类的注解 public class AppConfig { /* @Bean //等价于<bean id= class=/> public Student student(){ return new Student(); } @Bean public Grade grade(){ return new Grade(); }*/ }
使用配置类 注意点 我们自己创建的类可以使用@Component来创建对象 但是jar包的我们不能使用注解的方式可以采用@Bean的方式创建对象
测试类的使用
@Test//单元测试 public void test(){ // 使用配置类 AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);// 使用配置类 //使用xml的 配置类和Xml二选一 ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); //使用getBean创建一个 studentService的对象 StudentService studentService = (StudentService) ac.getBean("studentService"); //调用 studentService这的对象的方法 studentService.insertStudent(new Student()); }
总结
1.loc控制反转就是 创建对象的控制权交给spring框架来处理
2.创建对象的方式 xml使用<bean id="student" class="org.lanqiao.entity.Student"/>
注解使用@Component @Repository(dao) @Service(service) @Controller(servelt)
@Lazy 懒加载 只有使用到了才会加载 @Scop Singleton单例模式 prototype多例模式
3.扫描类中使用的注解 xml 使用
<context:component-scan base-package="org.lanqiao"/>
配置类使用
@ComponentScan({"org.lanqiao"})//加入扫描,扫描该类的注解
4.测试类 xml 使用
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
配置类使用
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
四、Spring的DI的概念
Dl依赖注入的前提是ioc创建了对象才能使用
1.使用xml
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="druidDataSource"/>
<bean id="Student" class="org.lanqiao.Student">
<property name="grade" ref="Grade"/>
</bean>
使用注解
@Autowired//bytype 更具类型来注入 @Qualifier("studentService") //若类型相同加这个更据名字来注入 @Primary//优先级 类型才相同 studentDao前加这个 先会被注入 @Resource 更具名字来注入
@Value 基本值注入
1.定义属性文件 info.prop key--value
2.导入属性文件@propertySource("class path:info.prop")
3.使用
@Value("${name}")
String name;
这篇关于spring框架总结的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求