码迷,mamicode.com
首页 > 编程语言 > 详细

Spring学习之路(一)spring入门

时间:2017-08-31 18:05:27      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:show   ati   assert   实例化   cto   sse   jdb   sys   cat   

1、引入jar包

  spring核心jar包
    spring-beans-4.3.2.RELEASE.jar
    spring-core-4.3.2.RELEASE.jar
    spring-context-4.3.2.RELEASE.jar
    spring-expression-4.3.2.RELEASE.jar
  spring日志jar包
    log4j-1.2.17.jar
    commons-logging-1.1.3.jar
  spring注解jar包
    spring-aop-4.3.2.RELEASE.jar
  spring AOP操作
    aopalliance-1.0.jar
    aspectjweaver-1.8.7.jar
    spring-aop-4.3.2.RELEASE.jar
    spring-aspects-4.3.2.RELEASE.jar
  日志文件
    log4j.properties -》放在src下
  spring对数据库操作
    spring-tx-4.3.2.RELEASE.jar
    spring-jdbc-4.3.2.RELEASE.jar
  c3p0连接池
    c3p0-0.9.2.1.jar
    mchange-commons-java-0.2.3.4.jar

2、创建类,在类中写方法

public class User {
    public void add(){
        System.out.println("add----------");
    }
//    public static void main(String[] args) {
//        //原始做法
//        User user = new User();
//        user.add();
//    }
}

3、配置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.xsd">
<!-- ioc入门 -->
    <!-- bean标签
            ID属性:自定义的值;在获取创建对象的值的时候需要
            class:类的路径 -->
    <bean id="user" class="com.ioc.User"></bean>
</beans>

4、写代码调试测试对象创建

package com.junit;

import static org.junit.Assert.*;

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

import com.ioc.User;

public class Text {

    @Test
    public void test() {
        //加载 spring 配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//        得到配置对象创建的对象 user为 spring配置文件中bean标签ID属性的值
        User user = (User) context.getBean("user");
//        获取到  User 类;执行其中的add方法
        user.add();
    }

}

bean实例化的三种方式

  1、使用类的无参构造方法创建(重点) ——》 上面案例;

  2、使用静态工厂创建

package com.ioc;

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

//
public class bean2 {
    public void add() {
        System.out.println("add--------------");
    }
}

//静态工厂
public class Factory {
//    使用静态的方法返回bean对象
    public static bean2 getBean2(){
        return new bean2();
    }
}

// 测试
@Test
public void test() {
    //加载 spring 配置文件
    ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//    得到配置对象创建的对象 
    bean2 bean2 = (bean2) context.getBean("bean2");
    System.out.println(bean2);
}
技术分享
<?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">
<!-- ioc入门 -->
    <!-- bean标签
            ID属性:自定义的值;在获取创建对象的值的时候需要
            class:类的路径 -->
    <bean id="user" class="com.ioc.User"></bean>
    <!-- destroy-method:指向的是 factory中的静态方法名 -->
    <bean id="bean2" class="com.factory.Factory" factory-method="getBean2"></bean>
</beans>
配置文件

  3、使用实例工厂创建

package com.ioc;

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

//
public class bean2 {
    public void add() {
        System.out.println("add--------------");
    }
}

//实例工厂类
public class Factory {
//    普通方法
    public bean2 getBean2(){
        return new bean2();
    }
}

/**  配置文件
     <!-- 实例工厂 -->
    <bean id="bean2Factory" class="com.factory.Factory" ></bean>
    <bean id="bean2" factory-bean="bean2Factory" factory-method="getBean2"></bean>
 */

//测试
@Test
public void test() {
    //加载 spring 配置文件
    ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//    得到配置对象创建的对象 
    bean2 bean2 = (bean2) context.getBean("bean2");
    System.out.println(bean2);
}

 

Spring学习之路(一)spring入门

标签:show   ati   assert   实例化   cto   sse   jdb   sys   cat   

原文地址:http://www.cnblogs.com/yaowan/p/7459374.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!