发表于: 2017-04-24 22:09:03
1 1541
今天的任务:
学习了Spring的相关知识。参考资料:
http://study.163.com/course/courseLearn.htm?courseId=215006#/learn/video?lessonId=310049&courseId=215006
http://www.cnblogs.com/jiaqingshareing/p/5677233.html
一、概念介绍:
首先明确了,Spring是一个开源的控制反转(IOC)和面向切面(AOP)的容器框架。
控制反转(IOC):(举例说明)
public class PersonServiceBean {
private PersonDao personDao = new PersonDao();
public void save(Person person){
personDao.save(person);
}
}
控制反转:应用本身不负责依赖对象personDao的创建以及维护,依赖对象的创建以及维护是由外部容器负责的,
这样控制权就由应用转移到了外部的容器,控制权的转移就是反转。
依赖注入(DI):在运行期间由外部容器动态地将依赖对象注入到组件中
改写的PersonServiceBean
public class PersonServiceBean {
private PersonDao persondao;
public PersonServiceBean(PersonDao persondao){
this.persondao=persondao;
}
public void save(Person person){
persondao.save(person);
}
}
相比于最初的代码,降低了组件之间的耦合度
使用spring的好处在于:降低了组件之间的耦合度,不再需要手工控制事物,只要通过声明式的事物属性配置就可以实现业务需求。
二、环境搭建:
首先下载使用spring需要的jar包,如果使用了切面编程(AOP)或JSR-250中的注解,还要添加相应的jar包;
新建java project项目:
1.src中新建xml文件,命名为bean,并拷贝如下到里面:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
2.新建测试单元Junit,实例化spring容器:
主要通过ApplicationContext ctx = newClassPathXmlApplicationContext(“bean.xml”)来实例化
当spring的配置文件有多个时可以改为(new String[]{" "})
在测试单元中新建方法(上面那串东西),来实例化容器。
测试通过,则环境搭建成功。
三、spring第一个例子:
1.抽取的接口
package impls;
public interface PersonService {
public abstract void save();
}
2.接口的实现方法
package service;
import impls.PersonService;
public class PersonServiceBean implements PersonService {
/* (non-Javadoc)
* @see service.impl.PersonService#save()
*/
public void save(){
System.out.println("save function");
}
}
注意:接口与实现的类不要放在同一个包下。
3.配置bean.xml(bean由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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="personService" class="test.PersonServiceBean"></bean>
</beans>
4.此时,已经有了业务bean,下一步交给spring管理,当我们要使用bean时,只需要调用即可:
package test;
import impls.PersonService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public static void setUpBeforeClass(){
}
public static void instanceSpring(){
//根据配置文件,创建一个spring容器的实例
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
//从容器中,根据id,取得业务层对象
PersonService ps =(PersonService)ctx.getBean("personService");
//调用业务层对象的方法
ps.save();
}
}
今天就理清了这么多。。。 明天继续。。。
有哪里不对或者需要注意的请指出~阿里嘎多~~
评论