spring框架核心思想以及注入方式(一)
2016-08-29 23:23
197 查看
1.spring用来整合其它框架和技术
2. 将事务的控制放在配置文件中管理(声明式事务),用来取代编程式事务
3. spring的核心思想
4.spring(微容器):管理 Dao,Service,Hibert,Action等等的生命周期,对这些java类的创建,使用,销毁进行管理(实体类是由hibernate管理)
5. 对java类进行管理 (示例代码)
6.通过spring,可以通过配置文件更换实现,而不用修改java代码
7.bean 中的作用域属性 scope
8.bean中的初始化方法和销毁方法的配置
二:spring的四种注入方式
set方法注入
构造方法注入
直接给属性注入,需要使用注解
自动织入(本质上仍然使用了set的方法注入) Autowired
byName 根据属性名与引用的id进行匹配,名称一致则注入
byType根据类型进行匹配,当同一个接口有2个实现类的时候就会报错,不知道使用哪个实现类
2. 将事务的控制放在配置文件中管理(声明式事务),用来取代编程式事务
3. spring的核心思想
IOC(Inversion of Controller)控制反转 将管理java类的控制权交给了spring容器 控制反转是所有的容器都具有的特性,不能很好区分spring与其他容器的不同,因此后来又提出了依赖注入的概念 DI(Ddpendency Inject)依赖注入 不仅要由spring容器来管理java类的创建,而且要管理类与类之间的依赖关系 AOP(Aspect):面向切面编程
4.spring(微容器):管理 Dao,Service,Hibert,Action等等的生命周期,对这些java类的创建,使用,销毁进行管理(实体类是由hibernate管理)
5. 对java类进行管理 (示例代码)
<?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:tx="http://www.springframework.org/schema/tx" 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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- bean标签用来管理java类 id是唯一标识 calss 是要实例化的类名称(全名) 需要有一个默认的构造方法 --> <bean id="empDao" class="com.lmr.dao.EmpDaoImpl"> </bean> <bean id="empService" class="com.lmr.service.EmpServiceImpl" init-method="init" destroy-method="destory"> <!-- 当spring初始化service的时候回根据property的配置为 empService属性赋值 service引导dao层方法 name 是引用的属性名 ref是引用的bean的id --> <property name="empDao" ref="empDao"></property> </bean> </beans> public class EmpServiceImpl implements EmpService{ //定义一个dao层接口的成员变量,并添加set方法 private EmpDao empDao; public void save(Object obj){ System.out.println("service 的save方法"); empDao.save(obj); } public void init(){ System.out.println("初始化方法"); } public void destory(){ System.out.println("销毁方法"); } public EmpDao getEmpDao() { return empDao; } public void setEmpDao(EmpDao empDao) { this.empDao = empDao; } public class EmpDaoImpl implements EmpDao{ public void save(Object obj) { System.out.println("dao 层 save"); } } public class Test2 { public static void main(String[] args) { //使用spring容器获得对象 创建spring容器 ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); //根据配置类的唯一标识(id)获取它的实例 EmpDao empDao =(EmpDao) ac.getBean("empDao"); EmpDao empDao2 =(EmpDao) ac.getBean("empDao"); System.out.println(empDao==empDao2); empDao.save(new Object()); EmpService empService = (EmpService)ac.getBean("empService"); empService.save(new Object()); } }
6.通过spring,可以通过配置文件更换实现,而不用修改java代码
①:实现类实现接口,当实现类变成另一个实现类的时候不用去修改java代码,而new出来的实现类需要去修改代码 ②:spring简化代码实现类只创建一个实例(默认情况下,一个spring只会创建实现类的一个实例 单例),而在代码中new 实现类 会出现多个实例,多占用内存,且后期不容易维护 ③:通过依赖注入的方式,建立类与类之间的关系 <property name="要注入的属性的名称" ref="要注入的类的id"> 要注入的属性必须实现响应的set方法
7.bean 中的作用域属性 scope
singleton:单例模式 prototype:多例模式
8.bean中的初始化方法和销毁方法的配置
init-method="init" init初始化方法的名称 destory-method="destory" destory销毁方法的名称 ClassPathXmlApplicationContext 中才有正常关闭spring容器的close()方法,测试销毁方法需要创建此对象
二:spring的四种注入方式
set方法注入
public class App1 { //使用传统的方法 /*UserService user = new UserService(); user.setName("XXXX"); user.sayHello();*/ //使用spring //1.得到spring的applicationContext对象(容器对象) //当ClassPathXmlApplication("applicationContext.xml")执行的时候,我们的spring容器对象被创建 //同时applicationContext.xml中配置bean就会被创建(内存) /** * 这是使用ClassPathXmlApplicationContext()创建bean */ @Test public void testClassPathXmlApplicationContext(){ ApplicationContext ac = new ClassPathXmlApplicationContext("com/hsp/service1/beans.xml"); UserService userService = ac.getBean("userService", UserService.class); System.out.println(userService.getName()+"=========="+userService.getByService().getName()); } /** * 这是使用XmlBeanFactory()创建bean */ @Test public void testXmlBeanFactory(){ BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/hsp/service1/beans.xml")); UserService userService = factory.getBean("userService", UserService.class); System.out.println(userService.getName()+"************"+userService.getByService().getName()); } } public class ByService { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void sayByService() { System.out.println("调用ByService的sayByService方法:"+name); } public class UserService { private String name; private ByService byService; public String getName() { return name; } public void setName(String name) { this.name = name; } public ByService getByService() { return byService; } public void setByService(ByService byService) { this.byService = byService; } public void sayUserService() { System.out.println("调用UserService的sayUserService方法:"+name); } <!-- 在容器文件中配置bean(service/dao/domain/action/数据源) --> <!--bean元素的作用是,当我们的spring框架加载的时候,spring就会自动的创建一个bean 放入内存中去 UserService userservice = new UserService() userService.setName("刘明然"); --> <bean id="userService" class="com.hsp.service1.UserService"> <!-- 这里体现注入的概念 --> <property name="name"> <value>XXX</value> </property> <!-- 在userService中引入byService --> <property name="byService" ref="byService"/> </bean> <bean id="byService" class="com.hsp.service1.ByService"> <property name="name" value="XXX1"></property> </bean>
构造方法注入
public class EmpServiceImpl implements EmpService{ //定义一个dao层接口的成员变量,并添加set方法 private EmpDao empDao; public EmpServiceImpl(EmpDao empDao){ this.empDao = empDao; } public void save(Object obj){ System.out.println("service 的save方法"); empDao.save(obj); } public void init(){ System.out.println("初始化方法"); } public void destory(){ System.out.println("销毁方法"); } /*public EmpDao getEmpDao() { return empDao; } public void setEmpDao(EmpDao empDao) { this.empDao = empDao; }*/ } <!-- 利用构造方法注入 index 参数下表 从0开始 ref 引用id 多个参数的时候写多个<constructor-arg> --> <constructor-arg index="0" ref="empDao"/>
直接给属性注入,需要使用注解
自动织入(本质上仍然使用了set的方法注入) Autowired
byName 根据属性名与引用的id进行匹配,名称一致则注入
byType根据类型进行匹配,当同一个接口有2个实现类的时候就会报错,不知道使用哪个实现类
相关文章推荐
- Spring框架的核心思想以及使用的步骤
- Spring框架的核心思想以及使用的步骤
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- (6) Spring 如何装配 集合类 以及 【第二种 依赖注入 方式】使用 构造器 注入
- SSH深度历险记(八) 剖析SSH核心原则+Spring依赖注入的三种方式
- JavaEE – JPA(5):ORM的核心注解 – 访问方式,表映射以及主键生成
- [JavaEE - JPA] 5. ORM的核心注解 - 访问方式,表映射以及主键生成
- Spring框架中的三种注入方式
- Spring中属性注入的几种方式以及复杂属性的注入
- Angular报注入错误,以及解决方式..$injector:unpr]
- View篇——索引列实现思想以及个人优化方式
- 对Spring框架思想中依赖注入的理解
- (5) Spring 如何装配 集合类 以及 【第二种 依赖注入 方式】使用 构造器 注入
- Spring IOC以及三种注入方式
- 7.3.4: spring的核心机制依赖注入---两种注入方式的对比
- spring几种依赖注入方式以及ref-local/bean,factory-bean,factory-method区别联系
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- Spring学习札记:第2节 属性注入方式以及模拟BeanFactory实现
- SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式
- Spring框架(三)依赖注入之注解方式