Java mac idea spring的使用01

1.什么是spring

简单来说,Spring 是一个分层的 JavaSE/EEfull-stack(一站式) 轻量级 开源框架。

2.spring的作用

2.1 方便解耦,简化开发

Spring 就是一个大工厂,可以将所有对象创建和依赖关系维护,交给 Spring 管理

2.2 AOP 编程的支持

Spring 提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能

2.3 声明式事务的支持

只需要通过配置就可以完成对事务的管理,而无需手动编程

2.4 方便程序的测试

Spring 对 Junit4 支持,可以通过注解方便的测试 Spring 程序

2.5 方便集成各种优秀框架

Spring 不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz 等)的直接支持

2.6 降低 JavaEE API 的使用难度

Spring 对 JavaEE 开发中非常难用的一些 API(JDBC、JavaMail、远程调用等),都提供了封装,使这些 API 应用难度大大降低

3.spring的使用

3.1 导包

如果是之前使用eclipse进行开发的同学,使用idea应该会觉得非常爽,非常方便。首先创建工程的时候,只需要勾选上需要依赖的框架,那么idea就会为你下载最新的jar包,不用你再进行手动的导包了。


Snip20180727_3.png

3.2 配置spring

先看目录结构
Snip20180727_4.png

1.创建User类

public class User {
    String name;
    Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

2.书写配置文件
2.1 配置文件默认放到src路径下(无强制规定,程序员都放src下),文件名applicationContext.xml(无强制规定,程序员都这么起),自己看着办好伐。

2.2 这里,在idea下,直接就可以进行配置文件的书写,无需像在eclipse里面一样,还要引入约束,相信用过eclipse的同学会有感触。

这里直接将三种创建方式都放上来了,我们只介绍创建方式1,方式2和3并不常用,不详细介绍了。

<?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">

    <!--将user交给spring容器管理-->

    <!-- 创建方式1:空参构造创建  -->
    <!-- name:别名       class:类路径  scope:singleton 单例模式  prototype多例模式-->
    <bean name="user" class="bean.User" scope="singleton"></bean>


    <!-- 创建方式2:静态工厂创建(不常用)
          调用UserFactory的createUser方法创建名为user2的对象.放入容器
     -->
    <bean  name="user2"
           class="bean.UserFactory"
           factory-method="createUser" ></bean>


    <!-- 创建方式3:实例工厂创建(不常用)
         调用UserFactory对象的createUser2方法创建名为user3的对象.放入容器
     -->
    <bean  name="user3"
           factory-bean="userFactory"
           factory-method="createUser2" ></bean>

    <bean  name="userFactory"
           class="bean.UserFactory"   ></bean>

</beans>

4.测试


public class Test01 {

    @Test
    public void testFunc() {
        // 使用spring来创建对象
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 通过 applicationContext 获取对象
        User user = (User) applicationContext.getBean("user");
        User user2 = (User) applicationContext.getBean("user");

        System.out.println(user);
        System.out.println(user == user2);
    }
}

5. 方式2和方式3作为了解

public class UserFactory {

    public static User createUser(){

        System.out.println("静态工厂创建User");

        return new User();

    }

    public  User createUser2(){

        System.out.println("实例工厂创建User");

        return new User();

    }
}

public class Test01 {

    @Test
    public void testFunc() {
        // 使用spring来创建对象
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 通过 applicationContext 获取对象
        User user2 = (User) applicationContext.getBean("user2");
        User user3 = (User) applicationContext.getBean("user3");

        System.out.println(user2);
        System.out.println(user3);
    }
}

4. 生命周期

41. 配置

 <!-- 创建方式1:空参构造创建  -->
    <bean name="user" class="bean.User" scope="singleton" 
      init-method="init" destroy-method="destroy"></bean>

42. 在user中实现方法

public void init() {
        System.out.println("初始化方法调用");
    }

    public void destroy() {
        System.out.println("销毁方法调用");
    }

43. 测试

@Test
    public void testFun2() {
        // 使用spring来创建对象
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 通过 applicationContext 获取对象
        User user = (User) applicationContext.getBean("user");
        System.out.println(user);

        applicationContext.close();
    }
初始化方法调用
bean.User@4f063c0a

Jul 30, 2018 10:38:56 AM org.springframework.context.support.ClassPathXmlApplicationContext doClose
销毁方法调用
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4fccd51b: startup date [Mon Jul 30 10:38:56 CST 2018]; root of context hierarchy

Process finished with exit code 0

5.模块化配置

可以通过引入其他的配置文件,用不同的配置文件专门配置某些类


Snip20180730_10.png

6. 属性注入

6.1 set方法注入

1.创建dog类

public class Dog {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  1. 在User中添加Dog类型的属性
public class User {
    private String name;
    private Integer age;
    private Dog dog;

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }
  1. 书写配置文件


    Snip20180730_9.png

4.打印结果


Snip20180730_12.png

6.2 构造方法注入

1.在User中添加构造方法

public User(String name, Dog dog) {
        this.name = name;
        this.dog = dog;
    }

2.书写配置文件

<!--构造方法注入-->
    <bean name="user2" class="bean.User" scope="singleton">
        <constructor-arg name="name" value="张三"/>
        <constructor-arg name="dog" ref="dog"/>
    </bean>


    <bean name="dog" class="bean.Dog" scope="singleton">
        <property name="name" value="旺财"/>
        <property name="age" value="3"/>
    </bean>

添加:index指定构造参数顺序:
如果User中有两个构造方法,并且属性相同,只是顺序不同

public User(String name, Dog dog) {
        this.name = name;
        this.dog = dog;
    }

    public User(Dog dog,String name) {
        this.name = name;
        this.dog = dog;
    }

这时候需要在配置文件中,通过index指定参数的顺序

<!--构造方法注入-->
    <!--constructor-arg:构造参数-->
    <bean name="user2" class="bean.User" scope="singleton">
        <constructor-arg name="name" value="张三" index="0"/>
        <constructor-arg name="dog" ref="dog" index="1"/>
    </bean>


    <bean name="dog" class="bean.Dog" scope="singleton">
        <property name="name" value="旺财"/>
        <property name="age" value="3"/>
    </bean>

添加:type指定构造参数的参数类型:
如果User中有两个构造方法,参数名相同,只是类型不同

public User(String name, Dog dog) {
        this.name = name;
        this.dog = dog;
    }

    public User(Integer name, Dog dog) {
        this.name = name + "";
        this.dog = dog;
    }

这时候需要通过在配置文件中,指定参数的类型

 <!--构造方法注入-->
    <!--constructor-arg:构造参数-->
    <!-- name属性: 构造函数的参数名 -->
    <!-- index属性: 构造函数的参数索引 -->
    <!-- type属性: 构造函数的参数类型-->
    <bean name="user2" class="bean.User" scope="singleton">
        <constructor-arg name="name" value="89757" index="0" type="java.lang.Integer"/>
        <constructor-arg name="dog" ref="dog" index="1"/>
    </bean>


    <bean name="dog" class="bean.Dog" scope="singleton">
        <property name="name" value="旺财"/>
        <property name="age" value="3"/>
    </bean>

6.3 p名称空间注入

1.在配置文件中导入p名称空间

<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.xsd">

2.书写要配置的对象

    <bean name="user3" class="bean.User" p:name="李四" p:age="20" p:dog-ref="dog">
    </bean>

6.4 spel表达式注入

<bean name="user4" class="bean.User" >
        <!--name:赋值user的name  age:赋值user2的age-->
        <property name="name" value="#{user.name}"/>
        <property name="age" value="#{user2.age}"/>

        <!--spel表达式不支持引用类型-->
        <property name="dog" ref="dog"/>
    </bean>

7.复杂类型注入

先创建一个类,包含各种array、list、map

public class CollectionBean {
    private Object[] arr;
    private List list;
    private Map map;

    public Object[] getArr() {
        return arr;
    }

    public void setArr(Object[] arr) {
        this.arr = arr;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    @Override
    public String toString() {
        return "CollectionBean{" +
                "arr=" + Arrays.toString(arr) +
                ", list=" + list +
                ", map=" + map +
                '}';
    }

7.1 array数组注入

1.单个元素注入

<bean name="collectionBean" class="bean.CollectionBean">

        <!--如果数组中只准备注入一个值,那么可以直接使用value|ref-->
        <property name="arr" value="tom"/>

    </bean>

2.多个元素注入

<bean name="collectionBean" class="bean.CollectionBean">
        <!--如果数组中只准备注入一个值,那么可以直接使用value|ref-->
        <!--<property name="arr" value="tom"/>-->
        <property name="arr">
        <array>
        <value>旺财1号</value>
        <value>旺财2号</value>
        <ref bean="user4"/>
        </array>
        </property>
    </bean>

7.2 list注入

和array注入类似
1.单个元素注入

<bean name="collectionBean" class="bean.CollectionBean">

        <!--如果数组中只准备注入一个值,那么可以直接使用value|ref-->
        <property name="list" value="tom"/>

    </bean>

2.多个元素注入

<bean name="collectionBean" class="bean.CollectionBean">
        <!--<property name="list" value="tom"/>-->

        <property name="list">
            <list>
                <value>tom1</value>
                <value>tom2</value>
                <ref bean="user"/>
            </list>
        </property>
    </bean>

7.3 map注入

<bean name="collectionBean" class="bean.CollectionBean">
        <property name="map">
            <map>
                <entry key="url" value="jdbc:mysql:///crm"/>
                <entry key="dog" value-ref="dog"/>
                <entry key-ref="user" value-ref="dog"/>
            </map>
        </property>
    </bean>

7.4 properties注入

<bean name="collectionBean" class="bean.CollectionBean">
        <property name="properties">
            <props>
                <prop key="driverClass">com.jdbc.mysql.Driver</prop>
                <prop key="userName">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>
    </bean>

8. 设置spring生命周期,绑定到Application域

8.1 配置spring

<!--让spring容器岁项目的创建而创建,项目关闭而销毁-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--指定加载spring配置文件的位置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

8.2 从servletContext中获取applicationContext容器

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.apache.struts2.ServletActionContext;
import javax.servlet.ServletContext;



public class Test01 {

    @Test
    public void testFunc1() {

        ServletContext servletContext = ServletActionContext.getServletContext();
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        User user = (User) webApplicationContext.getBean("user");
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容