详解Spring

Spring

SSH框架中Struts2:是基于Web层,Hibernate:是基于持久化的,Spring:业务层,管理bean,它是一个容器,List,map, Set这里的内容,是适合已经学过了Spring的人供复习参考的.....
Spring框架的优点: 
  1. Spring是分层的架构,你可以选择使用你需要的层而不用管不需要的部分
  2. Spring是POJO编程,POJO编程使得可持续构建和可测试能力提高
  3. 依赖注入和IoC使得JDBC,Hibernate操作简单化
  4. Spring是开源的免费的
  5. Spring使得对象管理集中化合简单化 

在爽一把前,先要弄懂Spring容器中装的bean--生命周期,如下图,很好的说明了bean的声明周期。

在说Spring的时候,我们先爽一把吧
   1,创建java项目
   2,加入Spring开发相关的jar包
   3,创建业务类
      /**GreetingService*/
       public class GreetingService{
           private String greeting;
           private String greeting2;   //相应的get、set方法
         
           /**buyService*/
           private ByeService bs;
           public void syaGreeting(){
               bs.syeBye();
           }
       }

    /**BuyService*/
      public class ByeService{
          private  String bye;
          public String getBye() {
   return bye;
  }
      public void setBye(String bye) {
   this.bye = bye;
  }
 public void  sayBye(){
                                System.out.println(bye);
         }           
      }
   配置Spring文件
      <?xml version="1.0"?>
<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="greetingService" class="...GreetingService">
                             <property name="greeting">
 <value>hello world</value>
     </property>
              <property name="greeting2">
<value>tom</value>
     </property>
     <property name="bs" ref="byeService" />
 
      </bean>
                      <bean id="byeService" class="....ByeService">
                            <property name="bye">
        <value>later</value>
    </property>
                      </bean>

  创建app
     ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
     GreetingService gs = (GreetingService)ac.getBean("greetingService");
     gs.sayGreeting();
     gs.sayGreeting2();
     
     ByeService bs = (ByeService) ac.getBean("byeService");
     bs.sayBye();

spring:
ioc:inverse of control,反转控制.获得依赖对象的方式被反转了.
1.new.(spring负责对象实例化)
2.组装对象的出发点是反的.
DI:dependency injection,依赖注入.
aop:aspect oriented program,面向方面编程.

oop:面向对象编程.

BeanFactory:实例化bf时,不会实例化任何bean,只有在getBean的时候才会实例化相关的Bean,这样做能够节省资源
ApplicationContext:在实例ac的时候,是会实例单例的bean(在结合struts的时候,管理action,action是原型的,                    所以,在实例化的时候是不会实体化的)
set注入的缺点是无法清晰表达哪些属性是必须的,哪些是可选
    的,构造注入的优势是通过构造强制依赖关系,不可能实例化不
    完全的或无法使用的bean。
自动装配:
        <bean id="foo" class="...Foo" autowire="autowire type">
1.byName:按照名称自动装配,寻找和bean的属性名相一致的bean的id.(bean必须有空的构造,通过set方法注入)
2.byType:按照属性的类型来自动装配,如果找到多个,抛异常.(bean必须有空的构造,通过set方法注入)
3.constructor:按照构造函数参数的类型自动装配,如果找不到或者找多个,都抛异常.
4.autodetact:自动检测,在(2)和(3)之间选择一个.
5.no.
6.default,跟<beans default-autowire属性>保持一致.


分散配置:
把需要在上下文硬编码的属性拿到外部的属性文件中定义,让上下文从外部文件提取值.


自定义编辑器:
将字符串转换成相应的对象,

aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理)
切面:实现的交叉功能.
通知:切面的实际实现.
连接点:应用程序执行过程期间,可以插入切面的地点.
切入点:真正的将通知应用到目标程序中的地点,一定是连接点.切入点是连接点的子集.
引入:为类增加新的属性和方法.
目标对象:被通知的对象.
代理:把通知应用到目标对象以后,产生新的对象,该对象就称为代理对象.
织入:创建代理对象过程.
编译期织入:.java  --> .class,需要特殊的编译器.
类装载期织入:将java字节码载入到jvm时,将通知织入.需要特殊的classloader.
运行期(runtime):
cglib:
aop alliance:aop联盟.


spring aop编程:
1.aop alliance.jar(已经集成在spring.jar中) + cglib.
${spring解压目录}/lib/cglib/*.jar
2.加入aspectj类库
${{spring解压目录}/lib/aspectj/*.jar(aspectjrt.jar + aspectjweaver.jar)
3.创建接口和实现类
public interface WelcomeService {
public void sayName();
}
/**
* 目标类
*/
public class WelcomeServiceImpl implements WelcomeService {
private String name;


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public void sayName() {
System.out.println(name);
}
}
4.创建前置通知.
/**
* 前置通知(方法前通知)
*/
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("hello world");
}
}
5.配置文件.
<?xml version="1.0"?>
<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="myMethodBeforeAdvice" class="....MyMethodBeforeAdvice" />


<!-- 目标对象 -->
<bean id="welcomeServiceTarget" class="......WelcomeServiceImpl">
<property name="name" value="tom" />
</bean>

<!-- 代理对象 -->
<bean id="welcomeService"                                   class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 代理接口集 -->
<property name="proxyInterfaces">
<list>
<value>.....WelcomeService</value>
</list>
</property>
<!-- 拦截器名集 -->
<property name="interceptorNames">
<list>
<value>myMethodBeforeAdvice</value>
</list>
</property>
<!-- 指定目标对象 -->
<property name="target" ref="welcomeServiceTarget" />
</bean>
</beans>
6.App
ApplicationContext ac = new ClassPathXmlApplicationContext(
"...aop.xml");
WelcomeService ws = (WelcomeService) ac.getBean("welcomeServiceTarget");
ws.sayName();


public interface Pointcut{
ClassFilter getClassFilter();
MethodMatcher getMethodMatcher();
}


Pointcut:切入点
Advice:通知
Advisor:切入点通知,组合体,既包含通知又包含切入点.对原来的通知的包装,增加定义切入点功能


PointcutAdvisor{
Pointcut getPointcut();
Advice getAdvice();
}


引入通知:
1.定义引入通知.

Dao:data access object.数据(数据库的表数据)访问对象.


dto:data transfer object,数据传输对象. struts1(actionform jsp --> action)




集成dao.
1.引入数据源类库
${spring解压目录}/lib/c3p0/*.jar
c3p0-0.9.1.2.jar
2.配置spring配置文件,链接数据源



insert:
conn = ds.getConn
conn.setAutocommit(false);
String sql = "insert into customers(name,age) values(?,?)"  ;
ppst = conn.preparedStatement(sql)
ppst.setString(1,"tom");
//...
ppst.executeUpdate();
conn.commit();


ppst.close();
conn.close();


update:
conn = ds.getConn
conn.setAutocommit(false);
String sql = "update customers set name = ?,age=? where id= ?"  ;
ppst = conn.preparedStatement(sql)
ppst.setString(1,"tom");
//...
ppst.executeUpdate();
conn.commit();


ppst.close();
conn.close();


//select
conn = ds.getConn
String sql = "select * from customers"  ;
ppst = conn.preparedStatement(sql)
ppst.setString(1,"tom");
//...
ppst.executeUpdate();
conn.commit();


ppst.close();
conn.close();


src/hibernate.cfg.xml
connection.driverclass
connection.url
user 
password


hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create|update|drop|create_drop




hibernate:
Configuration conf = new Configuration();
conf.configure();
//缓存:预先生成的sql语句和映射元数据.  二级缓存:缓存插件,
SessionFactory sf = conf.buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTx();
for(){
s.save(c);
}
s.save(c);
tx.commit();
s.close();






<bean id="ws" class="xxx.WelcomeServiceImpl" />


WelcomeService ws = ac.getBean("ws");


MyFB implements FactroyBean{
  getObject(){
return new Customer();
  }
}


<bean id="aa" class="...MyFB" />
Customer c = ac.getBean("aa");




spring整合hibernate:
1.引入hibernate类库.
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
log4j-1.2.15.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.0.jar
2.创建实体类的映射文件.
Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="....Customer" table="customers" lazy="false">
<id name="id" column="id" type="integer">
<generator class="identity" />
</id>
<property name="name" column="name" type="string" />
<property name="age" column="age" type="integer" />
</class>
</hibernate-mapping>
3.






spring:
connection.commit()|rollback


transaction.commit()|rollback.


事务:
a:atomic,原子性.
c:isolation,
i
d
事务管理:
1.编程式,硬编码方式.
2.声明式,




事务属性:
1.传播行为:事务的传递
2.隔离级别:控制并发程度的.
脏读:读未提交.
不可重复读:读不回去.
幻读:读多了.
ansi sql:
1:读未提交.
2:读已提交.
4:可以重复读.
8:串行化.


3.只读:优化.
4.超时:释放资源.
5.回滚规则:
把struts的action交给spring管理
    把action交给spring管理,并同时利用spring的DI功能完成依赖关系的装配.
public class xxxAction extends Action {
  public ActionForward execute(…){
    ….
  }
  private XxxService xxxService ;
  private getXXX() setXXX();//注入依赖
}
在spring上下文中,作为普通bean配置action,但action的不能用id,只能用name,
因为需要struts-config.xml文件中action的path一致.
bean.xml
<bean name="/loginAction" class="..LoginAction">
  <property name="xxxService" ref="xxxService" />
</bean>
注册代在struts-config.xml文件中需要配置请求处理器,负责到spring容器中寻找对应的action实例.
struts-config.xml
<controller processorClass="...DelegatingRequestProcessor" />


action的type属性可以去掉.
<action path="/loginAction"
            name="xxx"
            scope="request"
            validate="false|true" />


注:如果原来使用了特定的请求处理器.则需要改换配置代理action.注释掉控制器.如下:
<action path name 
            type="..DelegationActonProxy">
<!--
  <controller processorClass="..." />
-->理请求处理器
本人喜欢在整合struts2,hibernate的时候用注解的方式,这样使得配置文件大小大大降低了

原文地址:https://www.cnblogs.com/riskyer/p/3325044.html