详解Spring

article/2025/9/15 16:23:15
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();
pst.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的时候用注解的方式,这样使得配置文件大小大大降低了


http://chatgpt.dhexx.cn/article/SAOe1oUx.shtml

相关文章

Spring详解简介

1、Spring的简介 Spring的英文翻译为春天&#xff0c;可以说是给Java程序员带来了春天&#xff0c;因为它极大的简化了开发。我得出一个公式&#xff1a;Spring 春天 Java程序员的春天 简化开发。最后的简化开发正是Spring框架带来的最大好处。 Spring是一个开放源代码的设计…

spring 详细讲解

有人说&#xff0c;“Java程序员都是Spring程序员”&#xff0c;老三不太赞成这个观点&#xff0c;但是这也可以看出Spring在Java世界里举足轻重的作用。 基础 1.Spring是什么&#xff1f;特性&#xff1f;有哪些模块&#xff1f; Spring Logo 一句话概括&#xff1a;Spring…

JAVA框架Spring 全面详解(学习总结)

Spring 1.Spring 全面详解 1.1简介 ◆ Spring&#xff1a;春天 给软件行业带来了春天 ◆ 2002&#xff0c;首次推出了Spring框架的雏形&#xff1a;interface21框架 ◆ Spring框架即是以interface21框架为基础经过重新设计&#xff0c;并不断丰富其内涵&#xff0c;于2004…

Spring的事务详解

事务简介 事务在逻辑上是一组操作&#xff0c;要么执行&#xff0c;要不都不执行。主要是针对数据库而言的&#xff0c;比如说 MySQL。 为了保证事务是正确可靠的&#xff0c;在数据库进行写入或者更新操作时&#xff0c;就必须得表现出 ACID 的 4 个重要特性&#xff1a; 原…

java系列之Spring详解

一、Spring简介 1.1 简介 关于spring的简介&#xff0c;可以查看百度百科&#xff0c;下面内容部分来自百度百科 Spring框架是针对软件开发过程中的复杂性而创建的。其使用javaBean来完成以前只可能由EJB完成的事情。 2002年&#xff0c;Rod Jahnson首次推出了Spring框架雏形…

Spring全面详解

—————版本Spring5.x————— ——编译器IntelliJ IDEA 2020.2.3 —— <-- 该文章有点老旧&#xff0c;停止了更新&#xff0c;请查看Spring5最新文章&#xff0c;目前已经书写完成 --> Spring5全面详解 它会持续更新&#xff0c;你所看到的不是最终版本。 如…

Spring-全面详解(学习总结)

Spring 1.简介 1.1.简介 简介 Spring : 春天 —>给软件行业带来了春天 2002年&#xff0c;Rod Jahnson首次推出了Spring框架雏形interface21框架。 2004年3月24日&#xff0c;Spring框架以interface21框架为基础&#xff0c;经过重新设计&#xff0c;发布了1.0正式版。 …

spring超全面详解

spring概述 Spring 是于2003年兴起的一款轻量级的,非侵入式的IOC和AOP的一站式的java开发框架 为简化企业级应用开发而生. 1.轻量级: 就是指spring核心功能的jar包不大 2.非侵入式: 我们的业务代码不需要继承或实现spring中任何的类或接口 3.IOC: 控制反转 就是把创建…

Spring全面详解(学习总结)

Spring FrameWork一、 前言二、IOC(控制反转)2.1 对于IOC的理解2.2如何使用IOC2.3配置文件的解读2.4IOC容器创建bean的两种方式2.5从IOC容器中取bean2.6bean的属性如果包含特殊字符 三、DI(依赖注入)四、Spring中的bean五、Spring中的继承六、Spring的依赖七、Spring读取外部资…

查看Linux的用户权限(转载)

&#xff08;转&#xff09;Linux查看用户及其权限管理 查看用户 请打开终端&#xff0c;输入命令&#xff1a; $ who am i或者 $ who mom likes输出的第一列表示打开当前伪终端的用户的用户名&#xff08;要查看当前登录用户的用户名&#xff0c;去掉空格直接使用 whoami …

linux查看登录用户

1&#xff0c;w w,显示目前登入系统的用户信息 -f  开启或关闭显示用户从何处登入系统。 -h  不显示各栏位的标题信息列。 -l  使用详细格式列表&#xff0c;此为预设值。 -s  使用简洁格式列表&#xff0c;不显示用户登入时间&#xff0c;终端机阶段作业和程序所耗费…

Linux下查看当前用户和所属用户组方法总结

1、查看当前用户 &#xff08;1&#xff09;whoami &#xff08;2&#xff09;id -un &#xff08;3&#xff09;who -H &#xff08;4&#xff09;who&#xff08;查看当前登陆的所有用户&#xff0c;和who -H功能差不多&#xff09; 2、查看当前用户所属的组 &#xff08…

linux如何查看所有的用户和组信息?

首先打开终端&#xff08;这里是Ubuntu系统&#xff09;&#xff0c;其他的打开命令界面即可 然后输入命令行cat /etc/passwd&#xff0c;直接按下回车键即可 然后这里就会显示很多的信息&#xff0c;所有的用户都在这里面了 然后就是查看所有的组&#xff0c;同样的方法…

linux 查看用户信息

目录 /etc/passwd id命令 whois命令 whoami命令 who命令 w命令 finger命令 vlock命令 /etc/passwd 有的用户信息在根目录 /etc/passwd 文件内&#xff0c;而passwd的所有权限是root用户及root组用户&#xff0c;所有想要查看所有用户&#xff0c;需要root用户登录系统…

linux查看所有用户命令

1、Linux里查看所有用户 (1)在终端里.其实只需要查看 /etc/passwd文件就行了. (2)看第三个参数:500以上的,就是后面建的用户了.其它则为系统的用户. 或者用cat /etc/passwd |cut -f 1 -d : 2、用户管理相关命令 useradd命令 useradd 选项 用户名 -d 目录,指定用户主目录,如…

Linux命令之查看登录用户列表users

概述 users 命令 用于显示当前登录系统的所有用户的用户列表。每个显示的用户名对应一个登录会话。如果一个用户有不止一个登录会话&#xff0c;那他的用户名将显示相同的次数。 注&#xff1a;该命令与 who、w 类似。不过该命令只会显示登录用户名&#xff0c;信息简略。 语法…

Linux 系统中如何查看当前所有登录的用户

导读今天我们简单介绍下在 Linux 系统中列出登录用户的几种方法。 在多用户的 Linux 系统中&#xff0c;有时候会有查询当前已登录到系统中用户的需求。比如因某种原因要​​注销某个用户​​​。 今天我们简单介绍下在 Linux 系统中列出登录用户的几种方法。 我们所介绍的这…

查看linux用户密码

需要root用户 用户名在/etc/passwd这个文件中&#xff1b; 密码在/etc/shadow中 cat /etc/passwd cat /etc/shadow root:$6$1WtyW6O0baQmTkDG$o.YXXTOZSb7hP4HitigzaW/mZS433aSFpancmyNKYxU/59FuPVlIeVNBUKQQVgzx3kszkQAxo6C2wjrRbv0VZ.::0:99999:7::: 格式解释 {用户名}…

linux查看用户名

【步骤一】cat /etc/passwd cat /etc/passwd查看所有的用户信息&#xff0c;详情如下图 【步骤二】cat /etc/passwd|grep 用户名 cat /etc/passwd|grep 用户名&#xff0c;用于查找某个用户&#xff0c;如下图 【步骤三】cat /etc/group cat /etc/group查看所有组信息&#x…

linux如何查看用户

【步骤一】cat /etc/passwd cat /etc/passwd查看所有的用户信息&#xff0c;详情如下图 【步骤二】cat /etc/passwd|grep 用户名 cat /etc/passwd|grep 用户名&#xff0c;用于查找某个用户&#xff0c;如下图 【步骤三】cat /etc/group cat /etc/group查看所有组信息&…