您的位置:首页 > 编程语言 > Java开发

spring框架核心思想以及注入方式(一)

2016-08-29 23:23 197 查看
1.spring用来整合其它框架和技术

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个实现类的时候就会报错,不知道使用哪个实现类
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: