为了账号安全,请及时绑定邮箱和手机立即绑定

框架学习总结——Spring_4(学习中)

标签:
Java
Spring
  • 是轻量级的容器和非侵入性的框架
  • 实现了IoC容器
  • 实现了AOP概念
  • 提供对持久层与事务的支持
  • 实现了MVC模式
  • 一个全方位的应用程序框架

Spring官网下载4.0以上版本jar文件,
官网下载地址http://repo.spring.io/release/org/springframework/spring/
在eclipse安装Spring插件,官网下载地址https://spring.io/tools
导包:benas,context,core,expression,commons-logging
新建Spring bean配置文件


Spring IoC

控制反转(Inversion Of Control),原来创建对象的控制权在程序员手里,现在交给Spring,反转了控制权,所以叫控制反转。
依赖注入(Dependence Injection),Spring将依赖对象注入在配置文件中。(其实跟控制反转说的一样,只是一个意思的不同说法)
(附:eclipse中ctrl+t查看类的结构,ctrl+shift+t搜索类型)
例1:用Spring创建User对象(属性注入)

//实体类
package com.spring.entity;

import java.io.Serializable;

public class User implements Serializable {
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "User [name=" + name + "]";
    }
}
//测试类
package com.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.entity.User;
/**
 - Spring创建对象
 - 属性注入
 */
public class Test {
    public static void main(String[] args) {

        //1.读取配置文件,创建Spring的IOC容器对象
        //ApplicationContext IOC容器的类
        //ClassPathXmlApplicationContext 实现ApplicationContext接口
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

        //2.根据bean的id获取实例对象
        User u=(User) ctx.getBean("user");

        //3.调用对象
        System.out.println(u);
    }
}

初始化applicationContext有3种方法:

  • 本地文件
  • classpath
  • web应用,servlet
<!--applicationContext.xml配置文件-->
<?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.xsd">

    <!-- 属性配置bean  id表示对象  class表示全类名  name表示属性方法传参(set方法)  value表示属性值-->
    <bean id="user" class="com.spring.entity.User">
        <property name="name" value="张三丰"></property>
    </bean>
</beans>

例2:用Spring创建List集合对象(属性注入)

    <bean id="clazz" class="entity.Clazz">
        <property name="name" value="Tom"/>
        <property name="students">
            <list>
                <value>Tom0</value>
                <value>Tom1</value>
                <value>Tom2</value>
                <value>Tom3</value>
            </list>
        </property>
    </bean>
    <!--ref应用,clazz来自上面bean的id-->
    <bean id="teacher" class="entity.Teacher">
        <property name="clazz" ref="clazz"/>
    </bean>

例3:用Spring创建Person对象(构造器注入)

//实体类
package com.spring.entity;

import java.io.Serializable;

public class Person implements Serializable {
    private String name;
    private String sex;
    private Integer age;
    private String address;
    public Person(String name, String sex, Integer age, String address) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.address = address;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", sex=" + sex + ", age=" + age + ", address=" + address + "]";
    }
}
//测试类
package com.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.entity.Person;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        Person p=(Person) ctx.getBean("person");
        System.out.println(p);
    }
}
<!--applicationContext.xml配置文件-->
<?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.xsd">

    <!-- 构造方法配置bean  value表示值  index表示位置  type表示类型 -->
    <bean id="person" class="com.spring.entity.Person">
        <constructor-arg value="张三疯" index="0" type="java.lang.String"></constructor-arg>
        <constructor-arg value="男" index="1" type="java.lang.String"></constructor-arg>
        <constructor-arg value="30" index="2" type="java.lang.Integer"></constructor-arg>
        <constructor-arg value="广东惠州" index="3" type="java.lang.String"></constructor-arg>
    </bean>
</beans>

构造方法传参数,三个属性:值、下角标、类型,个人感觉不方便
如果值包含特殊字符,可以用<value>包含<![CDATA[]]>包裹起来

        <constructor-arg index="3" type="java.lang.Srting">
            <value>
                <![CDATA[<广东惠州>]]>
                <!--"<>"就是特殊符号-->
            </value>
        </constructor-arg>
常用属性:

延迟初始化:
Spring,对象加载时就创建,不是要用到时才创建,
如果,需要时才创建,添加属性lazy-init=”true”

原型:
单例模式输出属性,引用的同一个对象,
如果需要每次创建新的对象,添加属性scope=”prototype”

自动装配:将实体类的其他属性的bean自动转配到指定的bean中
多出引用适合自动装配,autowire=”byName”
自动装配的三种方式:
byType:根据类型自动装配
byName:根据名称自动装配
constructor:通过构造器自动装配,不推荐使用

抽象装配:
相同属性名,继承属性值,
父bean添加:abstract=”true”,子bean添加:parent=”父bean的id”


Spring AOP

(Aspect-orented programming)面向切面编程
(OOP是面向对象编程)
代理模式Proxy
{
控制接口开启,接口关闭
日志Logging
安全Security
}
面向切面,切面原来混在代码里面,现在把它们单独提取出来,形成切面。

//Dao层接口
package dao;

public interface IUserDao {
    public void add();
    public void update();
}
//Dao实现类
package dao;

public class UserDaoImpl implements IUserDao {
    <!--增加、更新方法-->
    public void add() {
    System.out.println("...add...");
    }
    public void update() {
        System.out.println("...update...");
    }
}
//将公用的操作放在这一个层,这就是切面,用的是代理设计模式
package dao;

public class ConnHandler {
    public void openConn(){
        System.out.println("打开连接");
    }

    public void closeConn(){
        System.out.println("关闭连接");
    }
}
//测试类
package dao;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext acc=new ClassPathXmlApplicationContext("applicationContext.xml");
        IUserDao userDao=(IUserDao)acc.getBean("userDao");

        userDao.add();
        System.out.println();
        userDao.update();
    }
}

控制台显示,每次操作都会执行打开连接、关闭连接

打开连接
...add...
关闭连接

打开连接
...update...
关闭连接
点击查看更多内容
7人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消